473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Help avoid goto

// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?
Jun 27 '08 #1
21 1407
In article <fc**********************************@x19g2000prg. googlegroups.com>,
spasmous <sp******@gmail.comwrote:
>// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:
>Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?
int i = 1;
while( i<n && y[i] != y[0] ) i++;
if (i == n) return;

This can also be written as a for loop.
--
"Let me live in my house by the side of the road --
It's here the race of men go by.
They are good, they are bad, they are weak, they are strong
Wise, foolish -- so am I;" -- Sam Walter Foss
Jun 27 '08 #2
spasmous wrote:
// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?
int i;

for (i = 1; n i; ++i) {
if (y[i] != y[0]) {
break;
}
}
if (i == n || 1 n) {
return;
}
--
pete
Jun 27 '08 #3
spasmous <sp******@gmail.comwrites:
// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?
9 times out of 10, this sort of situation is a reminder that you need
another function. Testing if an array has all elements equal is the
job of a separate function. There are lots of ways to write it:

bool all_equal(T *array, size_t n)
{
for (size_t i = 1; i < n; i++)
if (array[i] != array[0])
return false;
return true;
}

May people prefer this style (I think I do):

bool all_equal(T *array, size_t n)
{
for (size_t i = 1; i < n && array[y] == array[0]; i++)
continue;
return i == n;
}

(Note that they are not the same when n == 0 -- you need to decide what
you mean by that case.)

I had to use T because I don't know the type of the elements of y. If
you don't like using C99isms, move the declaration of i out of the
loop and return int rather than bool (or declare bool yourself).

--
Ben.
Jun 27 '08 #4
On Jun 20, 12:28*pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
>
int i = 1;
while( i<n && y[i] != y[0] ) i++;
if (i == n) return;

This can also be written as a for loop.
Very nice Walter. I went with a for loop variant.

// return early if all points are the same
for(int i=1; y[i]!=y[0]; i++)
if(i == n) return;

Jun 27 '08 #5
spasmous wrote:
On Jun 20, 12:28 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
>int i = 1;
while( i<n && y[i] != y[0] ) i++;
if (i == n) return;

This can also be written as a for loop.

Very nice Walter. I went with a for loop variant.

// return early if all points are the same
for(int i=1; y[i]!=y[0]; i++)
if(i == n) return;
Note that this tests y[n], the (n+1)st array element.

--
Er*********@sun.com
Jun 27 '08 #6
In article <10**********************************@s33g2000pri. googlegroups.com>,
spasmous <sp******@gmail.comwrote:
>On Jun 20, 12:28=A0pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
>>
int i = 1;
while( i<n && y[i] != y[0] ) i++;
if (i == n) return;

This can also be written as a for loop.

Very nice Walter. I went with a for loop variant.

// return early if all points are the same
for(int i=1; y[i]!=y[0]; i++)
if(i == n) return;
If y is defined from index 0 to n-1 then your code will access
y[n] in the termination test, which would be undefined behaviour
under that sizing assumption.

--
"When a scientist is ahead of his times, it is often through
misunderstanding of current, rather than intuition of future truth.
In science there is never any error so gross that it won't one day,
from some perspective, appear prophetic." -- Jean Rostand
Jun 27 '08 #7
On Jun 20, 12:19*pm, spasmous <spasm...@gmail.comwrote:
// return early if all points are the same
for(int i=1; i<n; i++)
* * if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?
{
// ...
for (int i = 1; i < n; i++) {
if (y[i] != y[0]) {
// entire ``SKIP:'' section here
break;
}
}
}

Like, put the conditional code into the body of the conditional
statement which tests that condition? Doh?

The reason you have a goto is that you relocated that logic away from
that construct. In general, you can't arbitrarily relocate control
without using GOTO or additional state variables.
Jun 27 '08 #8
On Jun 20, 1:12*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
9 times out of 10, this sort of situation is a reminder that you need
another function. *Testing if an array has all elements equal is the
job of a separate function. *There are lots of ways to write it:

bool all_equal(T *array, size_t n)
{
* * for (size_t i = 1; i < n; i++)
* * * * if (array[i] != array[0])
* * * * * * return false;
* * return true;

}

May people prefer this style (I think I do):

bool all_equal(T *array, size_t n)
{
* * for (size_t i = 1; i < n && array[y] == array[0]; i++)
* * * * continue;
* * return i == n;

This algorithm is not the same as the first version with the early
return. What if the array size is zero? The ``all equal'' condition is
true then: all elements of an empty sequence are equal to each other,
because it is not the case that there exist two distinct positions x
and y such that the x-th element is equal to the y-th element.

Moreover, the variable i is not in scope of the i == n expression.
Jun 27 '08 #9
On Jun 20, 1:12*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
bool all_equal(T *array, size_t n)
{
* * for (size_t i = 1; i < n && array[y] == array[0]; i++)
* * * * continue;
* * return i == n;
The name i is not in scope here, oops! :)
}

(Note that they are not the same when n == 0 -- you need to decide what
you mean by that case.)
That case means that all elements are equal. For an empty sequence, it
is not the case that there can exist two distinct indices x and y such
that the x-th element is equal to the y-th element. Since you can't
find two unequal elements, you cannot declare that the sequence does
not have all elements equal.

Here is another view. The all-equal property, if true of a sequence S,
should also be true of a subsequence of S, because how could it be the
case that all elements of a large sequence are equal, yet elements of
a subsequence of that sequence are not equal? But the empty sequence E
is a subsequence of every sequence; if the property is declared not
true of E by definition, then this represents a troublesome special
case; there now exists a subsequence of all-equal sequence S for which
the property does not hold.
Jun 27 '08 #10
Kaz Kylheku <kk******@gmail.comwrites:
On Jun 20, 1:12Â*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>9 times out of 10, this sort of situation is a reminder that you need
another function. Â*Testing if an array has all elements equal is the
job of a separate function. Â*There are lots of ways to write it:

bool all_equal(T *array, size_t n)
{
Â* Â* for (size_t i = 1; i < n; i++)
Â* Â* Â* Â* if (array[i] != array[0])
Â* Â* Â* Â* Â* Â* return false;
Â* Â* return true;

}

May people prefer this style (I think I do):

bool all_equal(T *array, size_t n)
{
Â* Â* for (size_t i = 1; i < n && array[y] == array[0]; i++)
Â* Â* Â* Â* continue;
Â* Â* return i == n;


This algorithm is not the same as the first version with the early
return. What if the array size is zero?
I know. Why did you clip the bit where I said that? I explained they
were not the same and when.
Moreover, the variable i is not in scope of the i == n expression.
That is a good point. Thank you.

--
Ben.
Jun 27 '08 #11
spasmous wrote:
// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?
if (isAllEqual(y, n)) return;
....
int isAllEqual(foo *y, int n) {
for (int i = 0; i <n; ++i) {
if (y[n] != y[0]) {
return false;
}
}
return true;
}

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #12

"spasmous" <sp******@gmail.comwrote in message
news:fc**********************************@x19g2000 prg.googlegroups.com...
// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
if(y[i] != y[0])
break;
-Mike
return;
SKIP:

Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?

Jun 27 '08 #13
spasmous wrote:
>
// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that
avoids goto? Without introducing a new variable?
for (i = 1; i < n; i++)
if (y[i] != y[0] break;
if (n == i) return;
else {
/* SKIP is here */
}

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #14
Kaz Kylheku <kk******@gmail.comwrites:
On Jun 20, 1:12Â*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>bool all_equal(T *array, size_t n)
{
Â* Â* for (size_t i = 1; i < n && array[y] == array[0]; i++)
Â* Â* Â* Â* continue;
Â* Â* return i == n;

The name i is not in scope here, oops! :)
Yes, you said that already. Do you get paid for posts? :-)
>}

(Note that they are not the same when n == 0 -- you need to decide what
you mean by that case.)

That case means that all elements are equal.
Yes.
For an empty sequence, it
is not the case that there can exist two distinct indices x and y such
that the x-th element is equal to the y-th element. Since you can't
find two unequal elements, you cannot declare that the sequence does
not have all elements equal.
I agree that there is only one logical return value for the empty
case, but I opted just to suggest that the OP think it over.

--
Ben.
Jun 27 '08 #15
On Jun 20, 4:04*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Kaz Kylheku <kkylh...@gmail.comwrites:
The name i is not in scope here, oops! :)

Yes, you said that already. *Do you get paid for posts? :-)
Alas, I deleted that post very soon after submitting it, literally
within a minute or two.
Jun 27 '08 #16


spasmous wrote:
// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?
for(int i=1; i<n; i++)
if(y[i] == y[0]) return;
w..
Jun 27 '08 #17
Walter Banks <wa****@bytecraft.comwrites:
spasmous wrote:
>// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?

for(int i=1; i<n; i++)
if(y[i] == y[0]) return;
That's not equivalent, consider the case where y[1] matches while y[2]
doesn't.

This might work:

int i;
for(i=1; i<n; i++)
if(y[i] != y[0]) break;
if (i=n)
return;
--
... __o Øyvind
... _`\(, http://www.darkside.no/olr/
... (_)/(_) ... biciclare necesse est ...
Jun 27 '08 #18
"Walter Banks" <wa****@bytecraft.comwrote in message
news:48***************@bytecraft.com...
spasmous wrote:
Assuming 'n' = 5 and y[0] = 9, y[1] = 9, y[2] = 9, y[3] = 99, y[4] = 9
>// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:
This will not return. It will goto SKIP when i = 3.
>Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?

for(int i=1; i<n; i++)
if(y[i] == y[0]) return;
But this will return when i = 1.

- Bill

Jun 27 '08 #19
"Øyvind Røtvold" <or******@gmail.comwrote in message
news:v6*************@jjj.jjj...
That's not equivalent, consider the case where y[1] matches while y[2]
doesn't.

This might work:

int i;
for(i=1; i<n; i++)
if(y[i] != y[0]) break;
if (i=n)
return;
Well, that's not equivalent either, though, since it changes the scope of
"i".

And I'm assuming "if (i=n)" is a typo for "if (i==n)". :)

- Bill

Jun 27 '08 #20
Walter Banks wrote:
spasmous wrote:
>// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

Can someone help me with an alternative to this snippet that
avoids goto? Without introducing a new variable?

for(int i=1; i<n; i++)
if(y[i] == y[0]) return;
No. This treats (y[1] == y[0]) as the non-SKIP case.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #21
All, quite correct missed the extent of the for loop. My bad.

w..
Bill Leary wrote:
"Walter Banks" <wa****@bytecraft.comwrote in message
news:48***************@bytecraft.com...
spasmous wrote:

Assuming 'n' = 5 and y[0] = 9, y[1] = 9, y[2] = 9, y[3] = 99, y[4] = 9
// return early if all points are the same
for(int i=1; i<n; i++)
if(y[i] != y[0]) goto SKIP;
return;
SKIP:

This will not return. It will goto SKIP when i = 3.
Can someone help me with an alternative to this snippet that avoids
goto? Without introducing a new variable?
for(int i=1; i<n; i++)
if(y[i] == y[0]) return;

But this will return when i = 1.

- Bill
Jun 27 '08 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: pertheli | last post by:
I am in a situation where only "goto" seems to be the answer for my program logic where I have to retry calling some repeated functions. Can anybody help in the usage of goto and its effect in...
37
by: Tim Marshall | last post by:
From http://www.mvps.org/access/tencommandments.htm 9th item: Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTo be part of an OnError process) for these will lead you...
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
1
by: Line 1 | last post by:
Hello!! Can you help me with my CSS problems? I have created a page that overlays text on an image. the problem is that it is leaving all this space at the bottom of background image that I also...
7
by: steve marchant | last post by:
trying to learn VB6. Simple counting loop which counts to 8 in 1 sec intervals, then starts from 1 again and repeats. Have two Command buttons on the form. Cmd1 starts the counting, and I need to...
3
by: John Smith | last post by:
Hi All, I have a script which reads a data file, reads the characters one by one and if a certain character is meet it does something else, at the moment it echos the fact that it meet a certain...
11
by: =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= | last post by:
Hello, I have a question about the infamous GOTO statement and the way to return a result from a sub: I have a sub that has to make some calls to external COM methods, and because these...
59
by: raashid bhatt | last post by:
why are GOTO's not used they just a simple JMP instructions what's bad about them
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.