Thread: while loop little confusing
int main()
{
int a=5;
while(a==5);
printf("a not five");
}
int main()
{
int a=5;
while(a!=5){
printf("a not five");break;}
}
difference between both while loops?
while(condition)
{
}
,
while(condition);
xxxxxxxx;
xxxxxxx;
getting little confuse
the first 1 infinite loop ... never terminates ... never prints anything, because while statement not have begin "{" , end "}" of block. squiggly brackets while execute in block long while condition true.
it's infinite loop because initialize a=5 in declaration ... while statement true because value of "a" never changes ... , there no block change value of "a" ... example: a++;
second while statement be infinite loop ... except 2 reasons:
1. "break" forces out of while statement. never changes value of "a" , there begin "{" , end "}" of block .... again never changes , "break" thing saves it.
2. never executes while loop because "a" initialized 5 , while execute when not equal 5 ... never executes.
int main()
{
int a=1;
while(a!=5){
printf("a not five\n");
a++;
}
}
Forum The Ubuntu Forum Community Ubuntu Official Flavours Support New to Ubuntu [ubuntu] while loop little confusing
Ubuntu
Comments
Post a Comment