0
Prefix/Postfix in a for-loop difference?
For(int i = 1; i < 5; i++){ Cout << i << endl;} //output: 1,2,3,4 For(int i = 1; i<5; ++i){ Cout << i << endl;} Question: why the output not like this: //output: 2,3,4? Explain please
5 Answers
+ 1
int i=2;
cout<<++i<<endl;
//will increment the value, then display it (3)
cout<<i<<endl;
//will display the incremented value (3)
i=2;
cout<<i++<<endl;
//will display the value then increment it. (2)
cout<<i<<endl; (3)
// will display the incremented value. Try it out.
+ 1
Syntax:
for ( init; condition; increment ) {
statement(s);
}
The init step is executed first, and does not repeat.
Next, the condition is evaluated, and the body of the loop is executed if the condition is true.
In the next step, the increment statement updates the loop control variable.
Then, the loop's body repeats itself, only stopping when the condition becomes false.
0
Do not capitalize for and cout
0
Was the output an error or different number?
0
Ah ok I understand, it increause just the loop, ty