Right Click to Search

Friday, February 12, 2025

Different Ways of Infinite While Loop




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 ?
  1. Condition is specified in the While Loop
  2. Semicolon at the end of while indicated while without body .
  3. As  num doesn't get incremented , condition remains true forever.
  4. 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 ?
  1. Note : We can specify any Non-Zero Positive Number inside while 
  2. Non Zero is specified in the While Loop
  3. Non-Zero Number is nothing but TRUE condition in the while,if etc
  4. 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 ?
  1. Condition is specified in while Loop ,but Terminating Condition is not specified
  2. Also Subscript Variable [Variable used to Repeat action] is also not either incremented or decremented
  3. 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 ?
  1. Character is Represented in integer in the form of ASCII internally.
  2. Any Character is Converted into Non-zero Integer ASCII value
  3. Any Non-zero ASCII value is TRUE condition , that is why Loop executes forever

Bookmark & Share

Tags / Keywords : | ,

Stumble
Delicious
Technorati
Twitter
Facebook

1 Comment:

shri said...

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

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates