0
I need help to understand for loops
9 odpowiedzi
+ 2
Nouman Ashraf see statement am telling in c cpp and java python loop is little bit different you have not mentioned language so i consider you know c cpp .
1st part 2nd. 3rd part
for(int i=0; i<10; i++)
{
cout<<i<<endl;
}
See here in condition i have written i<10 and i starting from zero so how many times condition can be true see
i<10 //when( i) initial zero
means 0<10here this is true so print statement will execute then after condition i have written increment so i will be increased by 1
Now i value is 1
i<10 = 1<10 condition true then print statement will execute
Then i++-- > 2
2<10
Condition true again rest of code will execute
.......
.........
9<10 condition true
10<10 here condition false so rest of statement will not execute
for(i=5; i<7;i++)
5<7 true then statement then increment
Then again condition then statement then increment
......
6<7
7<7 so see how many times here loop working total two times
+ 1
Nouman Ashraf first write for loop and see the statement which u have written inside bracket
Initialisation ,condition and increment/decrement
First initialisation part will be execute the statement then condition then increment
Then again condition then statement then increment decrement.
Again condition then statement then increment decrement
Same process will be repeat till until your condition failed .
So dry run means to analyse your program output and flow without running code.
Note: for better understanding you can refer online tutorials
0
Dry run is best way to understand things
0
What is a dry run
0
Google has nearly two million results for "for loops" and over twenty million for "for loop" ...
0
But how do should i teach myself to use the right condition and statement and increment
0
Everyone has already explained very well.
But let me tell you that you are gonna use "i" as counter variable in almost all loops which you will make in your future.