(simple) condition within for loop

P

papoupapa

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

printf("\n\nOUTPUT : \n") ;

for (i = 0 ; ((Numbers != 0) || (i < CMAX)) ; i++)
printf("\n%d. : %d", i+1, Numbers) ;

printf("\n\n") ;
return 0 ;
}

Thanks in advance,
 
I

Ian Collins

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 ||
 
G

Geoff

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

printf("\n\nOUTPUT : \n") ;

for (i = 0 ; ((Numbers != 0) || (i < CMAX)) ; i++)
printf("\n%d. : %d", i+1, Numbers) ;

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

Ian Collins

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

printf("\n\nOUTPUT : \n") ;

for (i = 0 ; ((Numbers != 0) || (i< CMAX)) ; i++)
printf("\n%d. : %d", i+1, Numbers) ;

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!)?
 
G

Geoff

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

printf("\n\nOUTPUT : \n") ;

for (i = 0 ; ((Numbers != 0) || (i< CMAX)) ; i++)
printf("\n%d. : %d", i+1, Numbers) ;

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 = nbr_in;
if (nbr_in == 0) /*early termination*/
break;
}
 
I

Ian Collins

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 = 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.
 
N

Nobody

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

Geoff

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 = 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 != 0 && i < CMAX; i++)
printf("\n%d. : %d", i+1, Numbers);
 
P

papoupapa

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 = 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 != 0 && i < CMAX; i++)
printf("\n%d. : %d", i+1, Numbers);


If you use Numbers 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?
 
J

J. J. Farrell

papoupapa said:
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.
 
G

Geoff

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 != 0 && i < CMAX; i++)
printf("\n%d. : %d", i+1, Numbers);


If you use Numbers 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 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.
 
H

Herbert Rosenau

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!
 
T

Tim Rentsch

Geoff said:
On Sun, 12 Jun 2011 15:58:44 -0700 (PDT), papoupapa

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

printf("\n\nOUTPUT : \n") ;

for (i = 0 ; ((Numbers != 0) || (i< CMAX)) ; i++)
printf("\n%d. : %d", i+1, Numbers) ;

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 = 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 );
} while( Numbers[ i++ ] != 0 && i < CMAX );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top