Different Ways of Infinite While Loop
Way 1 : Semicolon at the end of While
#include<stdio.h>
void main()
{
int num=300;
while(num>255); //Note it Carefully
printf("Hello");
}
Output :
It won't Print anything
How ?
- Condition is specified in the While Loop
- Semicolon at the end of while indicated while without body .
- As num doesn't get incremented , condition remains true forever.
- As Loop is body less , It won't print anything
Way 2 : Non-Zero Number as a Parameter
#include<stdio.h>
void main()
{
while(1)
printf("Hello");
}
Output :
Infinite Time "Hello" word
How ?
- Note : We can specify any Non-Zero Positive Number inside while
- Non Zero is specified in the While Loop
- Non-Zero Number is nothing but TRUE condition in the while,if etc
- As condition inside loop doesn't get changed ,condition inside while remains true forever.
Way 3 : Subscript Variable Remains the same
#include<stdio.h>
void main()
{
int num=20;
while(num<10)
{
printf("Hello");
printf(" C ");
}
}
Output :
Infinite Time "Hello C" word
How ?
- Condition is specified in while Loop ,but Terminating Condition is not specified
- Also Subscript Variable [Variable used to Repeat action] is also not either incremented or decremented
- so while remains true forever.
Way 4 : Character as a Parameter in While Loop
#include<stdio.h>
void main()
{
while('A')
printf("Hello");
}
Output :
Infinite Time "Hello" word
How ?
- Character is Represented in integer in the form of ASCII internally.
- Any Character is Converted into Non-zero Integer ASCII value
- Any Non-zero ASCII value is TRUE condition , that is why Loop executes forever
Bookmark & Share
1 Comment:
in way3...if 20<10....the ans is 0...so the result after its execution should be a blank screen..it should print nothing.........please check with it...the ans is given as infinite loop printing "Hello C"
Post a Comment
Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email