for loop incrementing
-
can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?
-
can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?
chaitanya22 wrote:
such as for(i=1; i<=100;i+2)..
should be :
for(int i=1; i<100; i+=2){}
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there! -
can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?
chaitanya22 wrote:
such as for(i=1; i<=100;i+2)..
Hello chaitanya
for(inintializer;condition;incrementer)
Here in place of incrementer you can put any statement regards of assigment statement. It is not require that you use only a variable that is use in initializer place. C design a very richfor
loop as you never seen before.
Divyang Mithaiwala System Engineer & Software Developer
-
can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?
You can even have multiple items in your loop, such as: for (int i = 2, j = 3; i < 5 && j < 10; i++, j+=3) da Big_R
-
can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?
If you do not want to make your for loop look anything other than the ordinary, but still want to skip processing the evens, use the continue keyword...
for( i = 1; i <= 100; i++ ) { if( i % 2 == 0 ) continue; //.... }
Koushik Biswas -
You can even have multiple items in your loop, such as: for (int i = 2, j = 3; i < 5 && j < 10; i++, j+=3) da Big_R
or...
int b=0,c=0,q=60,_=q;for(float i=-20,o,O=0,l=0,j,p;j=O*O,p=l*l,
(!_--|(j+p>4)?fputc(b?q+(_/3):10,(i+=!b,p=j=O=l=0,c++,stdout)),
_=q:l=2*O*l+i/20,O=j-p+o),b=c%q,c<2400;o=-2+b*.05); -
or...
int b=0,c=0,q=60,_=q;for(float i=-20,o,O=0,l=0,j,p;j=O*O,p=l*l,
(!_--|(j+p>4)?fputc(b?q+(_/3):10,(i+=!b,p=j=O=l=0,c++,stdout)),
_=q:l=2*O*l+i/20,O=j-p+o),b=c%q,c<2400;o=-2+b*.05);Chris Losinger wrote:
int b=0,c=0,q=60,_=q;for(float i=-20,o,O=0,l=0,j,p;j=O*O,p=l*l,(!_--|(j+p>4)?fputc(b?q+(_/3):10,(i+=!b,p=j=O=l=0,c++,stdout)),_=q:l=2*O*l+i/20,O=j-p+o),b=c%q,c<2400;o=-2+b*.05);
Ohh God! What will Happen to parsers:)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
If you do not want to make your for loop look anything other than the ordinary, but still want to skip processing the evens, use the continue keyword...
for( i = 1; i <= 100; i++ ) { if( i % 2 == 0 ) continue; //.... }
Koushik BiswasWhile this code works, it might not be as intuitive, and more importantly, efficient as:
for (int i = 0; i < 100; i += 2) { // do something }
Might not be important for small loops, but an extra if and % op can add up in big loops. - delete that; -
can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?
Hi chaitanya, I hope the below link helps u. http://www.its.strath.ac.uk/courses/c/ Chetan. Helping others satisfies you...