[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

simple) condition within for loop

papoupapa

6/12/2011 10:59:00 PM

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,
13 Answers

Ian Collins

6/12/2011 11:09:00 PM

0

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

Geoff

6/12/2011 11:24:00 PM

0

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.

Ian Collins

6/12/2011 11:27:00 PM

0

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

Geoff

6/12/2011 11:48:00 PM

0

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;
}

Ian Collins

6/13/2011 12:02:00 AM

0

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

John Doe

6/13/2011 1:33:00 AM

0

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).

Geoff

6/13/2011 2:52:00 AM

0

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]);

papoupapa

6/13/2011 1:01:00 PM

0

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?

Willem

6/13/2011 2:40:00 PM

0

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

J. J. Farrell

6/13/2011 9:13:00 PM

0

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.