+ 1

Why the following loop prints 1 infinite time

#include <stdio.h> int main() { int i; for( i=1;i<=5;printf("%d\n",i)); i++; return 0; }

14th Jul 2020, 1:39 PM
Suman Singh
Suman Singh - avatar
3 Réponses
+ 1
Suman Singh Only reason is that ' ; ' in c indicates termination. Like wise here also ; at end of for loop indicates no more to execute inside the block. so First i=1 since I<=5 laterpart will be executed that is printf function. since no increment infinite 1 will be printed Hope you understand!
14th Jul 2020, 1:46 PM
uday kiran
uday kiran - avatar
+ 1
Yes. Remove semicolon after for loop statement. @Uday khan I think there is typo in: "Since no increament," Make "Since on increament....."
14th Jul 2020, 1:54 PM
Jayakrishna 🇮🇳
0
Suman Singh In simple words while iterating in for loop atlast in the incrementing section you have not mentioned by how many steps loop should get iterated. Instead it has printf() statement which means compiler doesnt know when to stop ,it just goes on printing 1
14th Jul 2020, 1:48 PM
Varun N
Varun N - avatar