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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top