Prefix/Postfix in a for-loop difference? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

29th Dec 2016, 11:57 PM
Igor
Igor  - avatar
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.
30th Dec 2016, 12:23 AM
Pylotis
+ 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.
30th Dec 2016, 7:25 AM
Slawomir Maciejewski
0
Do not capitalize for and cout
30th Dec 2016, 12:14 AM
Nathan
Nathan - avatar
0
Was the output an error or different number?
30th Dec 2016, 12:15 AM
Nathan
Nathan - avatar
0
Ah ok I understand, it increause just the loop, ty
30th Dec 2016, 12:15 AM
Igor
Igor  - avatar