Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #5929 > unrolled thread
| Started by | papoupapa <papoupapoug@gmail.com> |
|---|---|
| First post | 2011-06-12 15:58 -0700 |
| Last post | 2011-06-14 06:41 +0000 |
| Articles | 14 — 8 participants |
Back to article view | Back to comp.lang.c
(simple) condition within for loop papoupapa <papoupapoug@gmail.com> - 2011-06-12 15:58 -0700
Re: (simple) condition within for loop Ian Collins <ian-news@hotmail.com> - 2011-06-13 11:08 +1200
Re: (simple) condition within for loop Geoff <geoff@invalid.invalid> - 2011-06-12 16:24 -0700
Re: (simple) condition within for loop Ian Collins <ian-news@hotmail.com> - 2011-06-13 11:27 +1200
Re: (simple) condition within for loop Geoff <geoff@invalid.invalid> - 2011-06-12 16:47 -0700
Re: (simple) condition within for loop Ian Collins <ian-news@hotmail.com> - 2011-06-13 12:01 +1200
Re: (simple) condition within for loop Geoff <geoff@invalid.invalid> - 2011-06-12 19:51 -0700
Re: (simple) condition within for loop papoupapa <papoupapoug@gmail.com> - 2011-06-13 06:00 -0700
Re: (simple) condition within for loop Willem <willem@toad.stack.nl> - 2011-06-13 14:39 +0000
Re: (simple) condition within for loop "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-13 22:13 +0100
Re: (simple) condition within for loop Geoff <geoff@invalid.invalid> - 2011-06-13 17:13 -0700
Re: (simple) condition within for loop Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-14 15:58 -0700
Re: (simple) condition within for loop Nobody <nobody@nowhere.com> - 2011-06-13 02:32 +0100
Re: (simple) condition within for loop "Herbert Rosenau" <os2guy@pc-rosenau.de> - 2011-06-14 06:41 +0000
| From | papoupapa <papoupapoug@gmail.com> |
|---|---|
| Date | 2011-06-12 15:58 -0700 |
| Subject | (simple) condition within for loop |
| Message-ID | <582385e0-5f22-49d2-9703-e8f591c9927b@y30g2000yqb.googlegroups.com> |
Hi,
The code below doesn't read the condition that says to stop when the
entry is 0. A while loop might be more convenient....
#include <stdio.h>
#define CMAX 5
int main(void)
{
int Numbers[CMAX] ;
int nbr_in ;
int i, j ;
nbr_in = 1 ;
j = 0 ;
for (i = 0 ; (nbr_in != 0) || (i < CMAX) ; i++, j++)
{
printf("\nValue %d : ", i+1) ;
scanf("%d", &nbr_in) ;
Numbers[i] = nbr_in ;
}
printf("\n\nOUTPUT : \n") ;
for (i = 0 ; ((Numbers[i] != 0) || (i < CMAX)) ; i++)
printf("\n%d. : %d", i+1, Numbers[i]) ;
printf("\n\n") ;
return 0 ;
}
Thanks in advance,
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-06-13 11:08 +1200 |
| Message-ID | <95kv82FrhmU3@mid.individual.net> |
| In reply to | #5929 |
On 06/13/11 10:58 AM, papoupapa wrote:
> Hi,
>
> The code below doesn't read the condition that says to stop when the
> entry is 0. A while loop might be more convenient....
>
>
> #include<stdio.h>
>
> #define CMAX 5
>
> int main(void)
> {
> int Numbers[CMAX] ;
> int nbr_in ;
> int i, j ;
>
> nbr_in = 1 ;
> j = 0 ;
> for (i = 0 ; (nbr_in != 0) || (i< CMAX) ; i++, j++)
This will keep reading until nbr_in == 0 *AND* i >= CMAX!
You wanted && there (ant later), not ||
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2011-06-12 16:24 -0700 |
| Message-ID | <laiav6t16tk5vi9k9n96qs20qnqmrnn1kv@4ax.com> |
| In reply to | #5929 |
On Sun, 12 Jun 2011 15:58:44 -0700 (PDT), papoupapa
<papoupapoug@gmail.com> wrote:
>Hi,
>
>The code below doesn't read the condition that says to stop when the
>entry is 0. A while loop might be more convenient....
>
>
>#include <stdio.h>
>
>#define CMAX 5
>
>int main(void)
>{
> int Numbers[CMAX] ;
> int nbr_in ;
> int i, j ;
>
> nbr_in = 1 ;
> j = 0 ;
> for (i = 0 ; (nbr_in != 0) || (i < CMAX) ; i++, j++)
> {
> printf("\nValue %d : ", i+1) ;
> scanf("%d", &nbr_in) ;
> Numbers[i] = nbr_in ;
> }
>
> printf("\n\nOUTPUT : \n") ;
>
> for (i = 0 ; ((Numbers[i] != 0) || (i < CMAX)) ; i++)
> printf("\n%d. : %d", i+1, Numbers[i]) ;
>
> printf("\n\n") ;
> return 0 ;
>}
>
>Thanks in advance,
Read your text about 'for' loops and the 'break' statement.
Test conditionals with 'if' as in "if (num_in == 0)".
Use the same technique for both loops.
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-06-13 11:27 +1200 |
| Message-ID | <95l0abFrhmU6@mid.individual.net> |
| In reply to | #5934 |
On 06/13/11 11:24 AM, Geoff wrote:
> On Sun, 12 Jun 2011 15:58:44 -0700 (PDT), papoupapa
> <papoupapoug@gmail.com> wrote:
>
>> Hi,
>>
>> The code below doesn't read the condition that says to stop when the
>> entry is 0. A while loop might be more convenient....
>>
>>
>> #include<stdio.h>
>>
>> #define CMAX 5
>>
>> int main(void)
>> {
>> int Numbers[CMAX] ;
>> int nbr_in ;
>> int i, j ;
>>
>> nbr_in = 1 ;
>> j = 0 ;
>> for (i = 0 ; (nbr_in != 0) || (i< CMAX) ; i++, j++)
>> {
>> printf("\nValue %d : ", i+1) ;
>> scanf("%d",&nbr_in) ;
>> Numbers[i] = nbr_in ;
>> }
>>
>> printf("\n\nOUTPUT : \n") ;
>>
>> for (i = 0 ; ((Numbers[i] != 0) || (i< CMAX)) ; i++)
>> printf("\n%d. : %d", i+1, Numbers[i]) ;
>>
>> printf("\n\n") ;
>> return 0 ;
>> }
>>
>> Thanks in advance,
>
> Read your text about 'for' loops and the 'break' statement.
> Test conditionals with 'if' as in "if (num_in == 0)".
> Use the same technique for both loops.
Why? What's wrong with testing more than one condition in a for loop
(assuming the logic is correct!)?
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2011-06-12 16:47 -0700 |
| Message-ID | <eojav65u47b3iklhiolo1vfqc5o9j3h3gp@4ax.com> |
| In reply to | #5936 |
On Mon, 13 Jun 2011 11:27:07 +1200, Ian Collins <ian-news@hotmail.com>
wrote:
>On 06/13/11 11:24 AM, Geoff wrote:
>> On Sun, 12 Jun 2011 15:58:44 -0700 (PDT), papoupapa
>> <papoupapoug@gmail.com> wrote:
>>
>>> Hi,
>>>
>>> The code below doesn't read the condition that says to stop when the
>>> entry is 0. A while loop might be more convenient....
>>>
>>>
>>> #include<stdio.h>
>>>
>>> #define CMAX 5
>>>
>>> int main(void)
>>> {
>>> int Numbers[CMAX] ;
>>> int nbr_in ;
>>> int i, j ;
>>>
>>> nbr_in = 1 ;
>>> j = 0 ;
>>> for (i = 0 ; (nbr_in != 0) || (i< CMAX) ; i++, j++)
>>> {
>>> printf("\nValue %d : ", i+1) ;
>>> scanf("%d",&nbr_in) ;
>>> Numbers[i] = nbr_in ;
>>> }
>>>
>>> printf("\n\nOUTPUT : \n") ;
>>>
>>> for (i = 0 ; ((Numbers[i] != 0) || (i< CMAX)) ; i++)
>>> printf("\n%d. : %d", i+1, Numbers[i]) ;
>>>
>>> printf("\n\n") ;
>>> return 0 ;
>>> }
>>>
>>> Thanks in advance,
>>
>> Read your text about 'for' loops and the 'break' statement.
>> Test conditionals with 'if' as in "if (num_in == 0)".
>> Use the same technique for both loops.
>
>Why? What's wrong with testing more than one condition in a for loop
>(assuming the logic is correct!)?
KISS principle? Readability? Maintainability? Getting the logic right
the first time, every time?
The variable j is unnecessary. The initialization of nbr_in is
unnecessary.
Consider:
for (i = 0; i < CMAX; i++)
{
printf("\nValue %d : ", i+1);
scanf("%d", &nbr_in);
Numbers[i] = nbr_in;
if (nbr_in == 0) /*early termination*/
break;
}
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-06-13 12:01 +1200 |
| Message-ID | <95l2b6FrhmU8@mid.individual.net> |
| In reply to | #5940 |
On 06/13/11 11:47 AM, Geoff wrote:
> On Mon, 13 Jun 2011 11:27:07 +1200, Ian Collins<ian-news@hotmail.com>
> wrote:
>> On 06/13/11 11:24 AM, Geoff wrote:
>>>
>>> Read your text about 'for' loops and the 'break' statement.
>>> Test conditionals with 'if' as in "if (num_in == 0)".
>>> Use the same technique for both loops.
>>
>> Why? What's wrong with testing more than one condition in a for loop
>> (assuming the logic is correct!)?
>
> KISS principle? Readability? Maintainability? Getting the logic right
> the first time, every time?
>
> The variable j is unnecessary. The initialization of nbr_in is
> unnecessary.
>
> Consider:
>
> for (i = 0; i< CMAX; i++)
> {
> printf("\nValue %d : ", i+1);
> scanf("%d",&nbr_in);
> Numbers[i] = nbr_in;
> if (nbr_in == 0) /*early termination*/
> break;
> }
I guess Readability is in the eye of the beholder. I for one don't like
breaks in loops, I prefer to see the termination conditions in one
place. The loop does the work, the loop control is in the for statement.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2011-06-12 19:51 -0700 |
| Message-ID | <feuav6dc8f74q3ash8v9vtrq6qj21limht@4ax.com> |
| In reply to | #5942 |
On Mon, 13 Jun 2011 12:01:42 +1200, Ian Collins <ian-news@hotmail.com>
wrote:
>On 06/13/11 11:47 AM, Geoff wrote:
>> On Mon, 13 Jun 2011 11:27:07 +1200, Ian Collins<ian-news@hotmail.com>
>> wrote:
>>> On 06/13/11 11:24 AM, Geoff wrote:
>>>>
>>>> Read your text about 'for' loops and the 'break' statement.
>>>> Test conditionals with 'if' as in "if (num_in == 0)".
>>>> Use the same technique for both loops.
>>>
>>> Why? What's wrong with testing more than one condition in a for loop
>>> (assuming the logic is correct!)?
>>
>> KISS principle? Readability? Maintainability? Getting the logic right
>> the first time, every time?
>>
>> The variable j is unnecessary. The initialization of nbr_in is
>> unnecessary.
>>
>> Consider:
>>
>> for (i = 0; i < CMAX; i++)
>> {
>> printf("\nValue %d : ", i+1);
>> scanf("%d",&nbr_in);
>> Numbers[i] = nbr_in;
>> if (nbr_in == 0) /*early termination*/
>> break;
>> }
>
>I guess Readability is in the eye of the beholder. I for one don't like
>breaks in loops, I prefer to see the termination conditions in one
>place. The loop does the work, the loop control is in the for statement.
The termination condition is clearly delineated in the if statement
and the block is easily modified without impairing readability. There
is no need for extra control variables and block is succinct.
On the other hand I would agree with you that using the if-break on
the second loop would be less succinct and the output loop would be
perfectly fine as:
for (i = 0; Numbers[i] != 0 && i < CMAX; i++)
printf("\n%d. : %d", i+1, Numbers[i]);
[toc] | [prev] | [next] | [standalone]
| From | papoupapa <papoupapoug@gmail.com> |
|---|---|
| Date | 2011-06-13 06:00 -0700 |
| Message-ID | <8ab97076-12ea-4033-8bdc-fe340f25f3dd@d1g2000yqm.googlegroups.com> |
| In reply to | #5950 |
On 13 juin, 02:51, Geoff <ge...@invalid.invalid> wrote:
> On Mon, 13 Jun 2011 12:01:42 +1200, Ian Collins <ian-n...@hotmail.com>
> wrote:
>
>
>
> >On 06/13/11 11:47 AM, Geoff wrote:
> >> On Mon, 13 Jun 2011 11:27:07 +1200, Ian Collins<ian-n...@hotmail.com>
> >> wrote:
> >>> On 06/13/11 11:24 AM, Geoff wrote:
>
> >>>> Read your text about 'for' loops and the 'break' statement.
> >>>> Test conditionals with 'if' as in "if (num_in == 0)".
> >>>> Use the same technique for both loops.
>
> >>> Why? What's wrong with testing more than one condition in a for loop
> >>> (assuming the logic is correct!)?
>
> >> KISS principle? Readability? Maintainability? Getting the logic right
> >> the first time, every time?
>
> >> The variable j is unnecessary. The initialization of nbr_in is
> >> unnecessary.
>
> >> Consider:
>
> >> for (i = 0; i < CMAX; i++)
> >> {
> >> printf("\nValue %d : ", i+1);
> >> scanf("%d",&nbr_in);
> >> Numbers[i] = nbr_in;
> >> if (nbr_in == 0) /*early termination*/
> >> break;
> >> }
>
> >I guess Readability is in the eye of the beholder. I for one don't like
> >breaks in loops, I prefer to see the termination conditions in one
> >place. The loop does the work, the loop control is in the for statement.
>
> The termination condition is clearly delineated in the if statement
> and the block is easily modified without impairing readability. There
> is no need for extra control variables and block is succinct.
>
> On the other hand I would agree with you that using the if-break on
> the second loop would be less succinct and the output loop would be
> perfectly fine as:
>
> for (i = 0; Numbers[i] != 0 && i < CMAX; i++)
> printf("\n%d. : %d", i+1, Numbers[i]);
If you use Numbers[i] as a testing variable, you have to initialize it
with something different from 0 at first... As declaring an array
makes array values equal to zero...
By the way, is the break statement different from goto in machine
language?
[toc] | [prev] | [next] | [standalone]
| From | Willem <willem@toad.stack.nl> |
|---|---|
| Date | 2011-06-13 14:39 +0000 |
| Message-ID | <slrnivc892.1crt.willem@toad.stack.nl> |
| In reply to | #5971 |
papoupapa wrote:
)> On the other hand I would agree with you that using the if-break on
)> the second loop would be less succinct and the output loop would be
^^^^^^
)> perfectly fine as:
)>
)> for (i = 0; Numbers[i] != 0 && i < CMAX; i++)
)> printf("\n%d. : %d", i+1, Numbers[i]);
)
) If you use Numbers[i] as a testing variable, you have to initialize it
) with something different from 0 at first...
Read again.
) By the way, is the break statement different from goto in machine
) language?
No, but the while statement and the for statement are the same as well.
It's all goto in machine language.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
[toc] | [prev] | [next] | [standalone]
| From | "J. J. Farrell" <jjf@bcs.org.uk> |
|---|---|
| Date | 2011-06-13 22:13 +0100 |
| Message-ID | <it5uhh$sf4$1@dont-email.me> |
| In reply to | #5971 |
papoupapa wrote:
>
> As declaring an array makes array values equal to zero...
Referring to this code:
> #define CMAX 5
>
> int main(void)
> {
> int Numbers[CMAX] ;
How an object gets initialized depends on how and where it is defined
(not declared). In this case the values are uninitialized, they could be
anything including invalid until they get assigned to.
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2011-06-13 17:13 -0700 |
| Message-ID | <9n8dv6dt845thqp101t4r5ljj9panasu1h@4ax.com> |
| In reply to | #5971 |
On Mon, 13 Jun 2011 06:00:46 -0700 (PDT), papoupapa
<papoupapoug@gmail.com> wrote:
>> On the other hand I would agree with you that using the if-break on
>> the second loop would be less succinct and the output loop would be
>> perfectly fine as:
>>
>> for (i = 0; Numbers[i] != 0 && i < CMAX; i++)
>> printf("\n%d. : %d", i+1, Numbers[i]);
>
>If you use Numbers[i] as a testing variable, you have to initialize it
>with something different from 0 at first... As declaring an array
>makes array values equal to zero...
At that point in your code the Numbers[i] has already received your
input values. Your error was to use || instead of && to control the
loop as Ian has already pointed out.
Declaring an array does not initialize its members to zero. Some
operating systems might do this but you can't depend on it. It's not
required by the language. Microsoft's debugger for example, puts
specific non-zero values into uninitialized memory to make spotting
programmer errors easier.
As written in your original post, your output loop will spew garbage
until it finds a zero in the Numbers array values.
>By the way, is the break statement different from goto in machine
>language?
Functionally, it's a goto without the need for the programmer to
specify a target line. The break statement says "terminate the
execution of the nearest enclosing do, for, switch, or while statement
and pass control to the statement that follows the terminated
statement." What the compiler generates to implement this is
implementation dependent.
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2011-06-14 15:58 -0700 |
| Message-ID | <kfnei2wf2bc.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #5940 |
Geoff <geoff@invalid.invalid> writes:
> On Mon, 13 Jun 2011 11:27:07 +1200, Ian Collins <ian-news@hotmail.com>
> wrote:
>
>>On 06/13/11 11:24 AM, Geoff wrote:
>>> On Sun, 12 Jun 2011 15:58:44 -0700 (PDT), papoupapa
>>> <papoupapoug@gmail.com> wrote:
>>>
>>>> Hi,
>>>>
>>>> The code below doesn't read the condition that says to stop when the
>>>> entry is 0. A while loop might be more convenient....
>>>>
>>>>
>>>> #include<stdio.h>
>>>>
>>>> #define CMAX 5
>>>>
>>>> int main(void)
>>>> {
>>>> int Numbers[CMAX] ;
>>>> int nbr_in ;
>>>> int i, j ;
>>>>
>>>> nbr_in = 1 ;
>>>> j = 0 ;
>>>> for (i = 0 ; (nbr_in != 0) || (i< CMAX) ; i++, j++)
>>>> {
>>>> printf("\nValue %d : ", i+1) ;
>>>> scanf("%d",&nbr_in) ;
>>>> Numbers[i] = nbr_in ;
>>>> }
>>>>
>>>> printf("\n\nOUTPUT : \n") ;
>>>>
>>>> for (i = 0 ; ((Numbers[i] != 0) || (i< CMAX)) ; i++)
>>>> printf("\n%d. : %d", i+1, Numbers[i]) ;
>>>>
>>>> printf("\n\n") ;
>>>> return 0 ;
>>>> }
>>>>
>>>> Thanks in advance,
>>>
>>> Read your text about 'for' loops and the 'break' statement.
>>> Test conditionals with 'if' as in "if (num_in == 0)".
>>> Use the same technique for both loops.
>>
>>Why? What's wrong with testing more than one condition in a for loop
>>(assuming the logic is correct!)?
>
> KISS principle? Readability? Maintainability? Getting the logic right
> the first time, every time?
>
> The variable j is unnecessary. The initialization of nbr_in is
> unnecessary.
>
> Consider:
>
> for (i = 0; i < CMAX; i++)
> {
> printf("\nValue %d : ", i+1);
> scanf("%d", &nbr_in);
> Numbers[i] = nbr_in;
> if (nbr_in == 0) /*early termination*/
> break;
> }
Doesn't match the exit conditions of the original for
early termination. Here is a more exact refactoring:
i = 0;
do {
printf( "\nValue %d : ", i+1 );
scanf( "%d", &Numbers[i] );
} while( Numbers[ i++ ] != 0 && i < CMAX );
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-06-13 02:32 +0100 |
| Message-ID | <pan.2011.06.13.01.32.19.938000@nowhere.com> |
| In reply to | #5936 |
On Mon, 13 Jun 2011 11:27:07 +1200, Ian Collins wrote: >> Read your text about 'for' loops and the 'break' statement. >> Test conditionals with 'if' as in "if (num_in == 0)". >> Use the same technique for both loops. > > Why? What's wrong with testing more than one condition in a for loop > (assuming the logic is correct!)? Nothing. Except that the loop condition determines whether the loop should continue while if-break determines whether it should terminate. If you know the termination condition, using it as such in an if-break may be less error-prone than determining the equivalent loop condition (if there even is such a condition, which may not be the termination test isn't at the end of the loop body).
[toc] | [prev] | [next] | [standalone]
| From | "Herbert Rosenau" <os2guy@pc-rosenau.de> |
|---|---|
| Date | 2011-06-14 06:41 +0000 |
| Message-ID | <wmzsGguTDN6N-pn2-csTvd5nm643m@JUPITER.PC-ROSENAU.DE> |
| In reply to | #5947 |
On Mon, 13 Jun 2011 01:32:49 UTC, Nobody <nobody@nowhere.com> wrote: > On Mon, 13 Jun 2011 11:27:07 +1200, Ian Collins wrote: > > >> Read your text about 'for' loops and the 'break' statement. > >> Test conditionals with 'if' as in "if (num_in == 0)". > >> Use the same technique for both loops. > > > > Why? What's wrong with testing more than one condition in a for loop > > (assuming the logic is correct!)? > > Nothing. Except that the loop condition determines whether the loop should > continue while if-break determines whether it should terminate. If you > know the termination condition, using it as such in an if-break may be > less error-prone than determining the equivalent loop condition (if > there even is such a condition, which may not be the termination test > isn't at the end of the loop body). > No, it doesn't matter to have an "If", a "for" or a "while" when the condiditions you use are simply wrong. A nested if is nothing else than a logical and, so it would not give the same conditions as in that the OP gave. -- Tschau/Bye Herbert Visit http://www.ecomstation.de the home of german eComStation eComStation 2.0 ist da!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web