+ 7

If we give ';' after for loop in C like for( ; ;); any condition within.. What will happen..??

if I give for(i=0;i<=10;i++); it is not showing error for semicolon besides this it shows output 11. how this happens.?? explain it. https://code.sololearn.com/cSaTRnYSv4L0/?ref=app

13th Dec 2017, 1:35 AM
Anamika Roy
Anamika Roy - avatar
9 Answers
+ 7
i is incremented until 10, but when i is 10 it fulfills the loop once more, incrementing it to 11. Hence the loop executes 11 times, hence i is 11. (I don't see anything wrong with the semicolons; it just means there is nothing in the loop.)
13th Dec 2017, 1:57 AM
blackcat1111
blackcat1111 - avatar
+ 5
see the code.. if I don't put ; after for loop, output displays 1 to 10. when I give ; after for loop it gives output 11. I learned in c we do not put ; after for loop it will give error. then how it happens that it give output 11 only.?
13th Dec 2017, 2:06 AM
Anamika Roy
Anamika Roy - avatar
+ 5
Because you have ended the loop with the semicolon, then outputted the value of i. The computer goes through the loop, incrementing i until 11, then exits the loop, and outputs 11. However, without the ending semicolon, the computer goes through the loop, outputting all numbers from 1-10, and stops at 11. That one semicolon changes the entire meaning of the loop! The semicolon is technically not wrong, but with or without it has different meanings. You should be careful and take note of what it is the loop is trying to do. 😉
13th Dec 2017, 2:16 AM
blackcat1111
blackcat1111 - avatar
+ 4
Actually semicolon is used for ending single line statements or commands... So it considers for loop as a single line statement and that for loop is executed till condition is satisfied....
31st Dec 2017, 12:50 PM
Sumit Patil
Sumit Patil - avatar
+ 4
sumit patil I agree with you Actually semicolon is used for ending single line statements or commands... So it considers for loop as a single line statement and that for loop is executed till condition is satisfied.... condition is satisfied (); now printf is out side of loop printf print the i value is 11 that compiler compute i value and it is absolutely right.
31st Dec 2017, 1:31 PM
ᱠᱟᱚᱩ ᱊ᱟᱱᱫᱟ
ᱠᱟᱚᱩ ᱊ᱟᱱᱫᱟ - avatar
+ 3
; after the for is a statement. It is the statement that gets used for your looping. Your printf is now outside of the loop and executes once after the loop ends.
13th Dec 2017, 2:18 AM
John Wells
John Wells - avatar
+ 2
for (I = 0; I <=10;I++) {} can be also written as for (I =0; I <= 10; I++);
13th Dec 2017, 3:05 AM
$±𝐎â‚č𝔭!𝐹𝓝
$±𝐎â‚č𝔭!𝐹𝓝 - avatar
+ 2
Ya absolutely @karu Hansda
31st Dec 2017, 1:40 PM
Sumit Patil
Sumit Patil - avatar
0
you speak french
6th Jan 2018, 7:35 AM
Pedro Mae