sometimes i don't understand FOR loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

sometimes i don't understand FOR loop

for ( x = 0 ; x < 5 ; x++ ) what is x++ means here i mean what will be its value in each iteration , how can i print the value and why there is a need of using x++ here can we skip it or use it inside the for loop statements ? for ( condition ) { x++; // here } and sometimes we also use like this for ( x=0 ; x <5 ; x++ ) // here { x++ ; // here } what if we don't use it like in while and do while we mostly increment inside the loops . i hope you guys understand my question..

16th Jul 2016, 5:17 AM
Karan Luther
Karan Luther - avatar
8 Answers
+ 2
Okay. So with the for loop, I believe you can leave out the 'x++'. I Believe you just have to remember the semicolons. you must put it inside the loop though, unless you want an endless loop... you can check to see if I am correct in the code playground, I do really hope this helps. Here's an example of what the heck in talking about. for(x=0; x < 5; ;) { //do something x++; }
16th Jul 2016, 5:22 AM
Autumn S
Autumn S - avatar
+ 1
13579
16th Jul 2016, 8:05 AM
Nishant
+ 1
No it will be simple first increment with 1 bcz of loop simple 1,2,3,....9.... K.. Then bcz of i++ afterwards it will increase the result by 1.. So as soon as the result i.e 1,3,5,7,9 becomes more than 10 the loop will be terminated.. 1 i=1 then again enter for loop then i++ means it becomes 2 after then again i++ it becomes 3 k.. Similarly then I =3 now i++ of for loop it becomes 4 and again i++ it bcms 5 so now i=5...
16th Jul 2016, 8:55 AM
Nishant
0
It's a syntax of for loop. Bcz the syntax of for loop is for(value;condition;increment/decrement) It's better to use increment and decrement within the for loop..
16th Jul 2016, 7:46 AM
Nishant
0
and when that increment works ? can anyone explain me with an example please
16th Jul 2016, 7:47 AM
Karan Luther
Karan Luther - avatar
0
Increment works when you have to increase the value.. Like you want to print the value from 0-9.. for(int i=0;i<10;i++) To reverse the order use i--.. And use i=10and put condition
16th Jul 2016, 7:51 AM
Nishant
0
and what if we use increments like this... for(int i=0; i<10;i++) { i++ ; } what will be the answer ?
16th Jul 2016, 7:55 AM
Karan Luther
Karan Luther - avatar
0
do you mean its like this ? for ( i= 0 ; i< 10; i++)// here i will be 1 3 5 7 9 { i++ ; // 2 4 6 8 }
16th Jul 2016, 8:17 AM
Karan Luther
Karan Luther - avatar