simple for loop i find not coherent

P

papoupapa

Hi

I reay think // A should be (nbr_entry != 0) || (i < NMAX) instead
of (nbr_entry != 0) && (i < NMAX)
As it's working, to my understanding, the second body of the for loop
is being read from the left to the right.

int main()
{
const int NMAX = 5 ;
vector<int>Numbers(NMAX) ;
int nbr_entry = 1 ;
int i, j ;

cout << "\nThis program reads a sequence of numbers \n" ;

j = 0 ;
nbr_entry = 1 ;
for (i = 0 ; (nbr_entry != 0) && (i < NMAX) ; i++, j++) // A
{
cout << "\nValue " << i+1 << " : " ;
cin >> nbr_entry ;
Numbers = nbr_entry ;
}

cout << "\n\nNumbers entered : \n" ;

for (i = 0 ; i < j ; i++)
cout << "Value " << i+1 << ": " << Numbers << "\n" ;


cout << "\n\n" ;
system("pause") ;
return 0 ;
}
 
I

Ian Collins


You asked almost the same question on c.l.c, very confusing!
I reay think // A should be (nbr_entry != 0) || (i< NMAX) instead
of (nbr_entry != 0)&& (i< NMAX)

Why? You want to keep reading while both conditions are true, not just one.
As it's working, to my understanding, the second body of the for loop
is being read from the left to the right.

int main()
{
const int NMAX = 5 ;
vector<int>Numbers(NMAX) ;
int nbr_entry = 1 ;
int i, j ;

cout<< "\nThis program reads a sequence of numbers \n" ;

j = 0 ;
nbr_entry = 1 ;
for (i = 0 ; (nbr_entry != 0)&& (i< NMAX) ; i++, j++) // A
{
cout<< "\nValue "<< i+1<< " : " ;
cin>> nbr_entry ;
Numbers = nbr_entry ;
}

cout<< "\n\nNumbers entered : \n" ;

for (i = 0 ; i< j ; i++)
cout<< "Value "<< i+1<< ": "<< Numbers<< "\n" ;


cout<< "\n\n" ;
system("pause") ;
return 0 ;
}
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top