papoupapa
6/13/2011 1:01:00 PM
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?