Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6663 > unrolled thread

simple for loop i find not coherent

Started bypapoupapa <papoupapoug@gmail.com>
First post2011-06-12 16:05 -0700
Last post2011-06-13 11:12 +1200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.c++


Contents

  simple for loop i find not coherent papoupapa <papoupapoug@gmail.com> - 2011-06-12 16:05 -0700
    Re: simple for loop i find not coherent Ian Collins <ian-news@hotmail.com> - 2011-06-13 11:12 +1200

#6663 — simple for loop i find not coherent

Frompapoupapa <papoupapoug@gmail.com>
Date2011-06-12 16:05 -0700
Subjectsimple for loop i find not coherent
Message-ID<ab6caee2-a808-4158-9602-fc51935307eb@d1g2000yqk.googlegroups.com>
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[i] = nbr_entry ;
	}

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

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


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

[toc] | [next] | [standalone]


#6664

FromIan Collins <ian-news@hotmail.com>
Date2011-06-13 11:12 +1200
Message-ID<95kvfnFrhmU4@mid.individual.net>
In reply to#6663
On 06/13/11 11:05 AM, papoupapa wrote:
> Hi

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[i] = nbr_entry ;
> 	}
>
> 	cout<<  "\n\nNumbers entered : \n" ;
>
> 	for (i = 0  ;  i<  j   ; i++)
> 		cout<<  "Value "<<  i+1<<  ": "<<  Numbers[i]<<  "\n" ;
>
>
> 	cout<<  "\n\n" ;
> 	system("pause") ;
> 	return 0 ;
> }


-- 
Ian Collins

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web