for and while loop in C
Consider the two below program:
#include<stdio.h>
int main()
{
while(){
printf("Coder Planet Pro\n");
}
return 0;
}
What do you think, what will be the output of this program?
Let's see another program.
#include<stdio.h>
int main()
{
for(){
printf("Coder Planet Pro\n");
}
return 0;
}
What is your output for both program? Have you got any error?
The syntax of for loop and while loop isn't right for both program.
while loop must need at least an argument to run. Otherwise, for loop must need at least two semicolones inside parentheses to run.
while loop syntax:
while(condition){ //if argument is true then inside of while loop will executestatements; //otherwise, program will not enter inside while loop
}
for loop syntax:
for(start;stop;increment/decrement){statements;
}
Correct program of while loop:
#include<stdio.h>
int main()
{
while(0){ //0 denotes false and 1 denotes true
printf("Coder Planet Pro\n");
}
return 0;
}
Correct program of for loop:
int main()
{
for(;;){ // it will create infinite loop
printf("Coder Planet Pro\n");
}
return 0;
}
Play with while and for loop to discover more interesting things!
If you don't understand anything, please let me know.
Image taken from: https://www.tutorialswebsite.com/loops-in-c/
for and while loop in C
Reviewed by Ikram
on
12/11/2019 10:03:00 AM
Rating:

No comments: