+ 9

Something Wrong.😨

in the description of for loop they said that for(int i=1;i<=10;i++); will return the value of i as 1 2 3 4 5... but when I run it on my PC compiler it should only 10 the coding is same but the outputs are different I want all all perform 1 to 10 in PC compiler

5th Jan 2018, 3:56 PM
Sarthak Gupta
Sarthak Gupta - avatar
4 Answers
+ 3
You have typed a semicolon(;) after the for loop’s statement. So the loop ends here. The cout<<i; part executes only after the completion of the loop and after completion, the value for I would be 10. So it displays only 10. You have to include the output statement within the loop. So instead of terminating the for loop by a ; in the end, add your cout statement with {} before the ; and for the ‘for’ loop you actually don’t need the semicolon. So instead of for(i=1; i<=10;i++); cout<<i; try for(i=1; i<=10; i++) { cout<<i; } I hope you understood whatv explained. If yes and can encourage me for this, an upvote would do good.
10th Jan 2018, 4:16 PM
Bhubesh Hackster
Bhubesh Hackster - avatar
+ 12
try this on your pc #include <iostream> using namespace std; int main() { for(int i=1;i<=10;i++) cout<<i<<endl; return 0; }
5th Jan 2018, 4:16 PM
⛎Ⓜ️ €⚡♓
+ 3
better try this one for(i=1;i<=10;++I) { count<<i;}
6th Jan 2018, 4:52 PM
Bharath Kalyan K B
Bharath Kalyan K B - avatar
+ 2
NOTE THAT WHEN U USE SEMICOLON AFTER FOR LOOP IT WILL DISPLAY THE LAST ANSWER FOR QHICH THE LOOP IS TRUE AND IT WILL NOT PRINT ANY OTHER CASES
6th Jan 2018, 4:49 PM
Bharath Kalyan K B
Bharath Kalyan K B - avatar