+ 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
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.)
+ 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.?
+ 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. 😉
+ 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....
+ 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.
+ 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.
+ 2
for (I = 0; I <=10;I++) {} can be also written as for (I =0; I <= 10; I++);
+ 2
Ya absolutely
@karu Hansda
0
you speak french