C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - General Questions (Q.No. 2)
2.
How many times the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
        printf("%c %d\n", j, j);
        j++;
    }
    return 0;
}
Infinite times
255 times
256 times
254 times
Answer: Option
Explanation:

The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.

Discussion:
59 comments Page 3 of 6.

Ahmedation said:   1 decade ago
It will be infinite number of loops truly but if only the variable type was signed/unsigned char.

DEEPak said:   1 decade ago
In question 1, while loop will execute 256 times. 1 time also for false condition. But the body will execute only 255 times only.

Amandeep Kaur said:   1 decade ago
Answer is infinite times, how it will be execute 255 times?

Amol wavare said:   1 decade ago
Answer is finite times. How it will be 256>255?

Pankaj said:   1 decade ago
Its signed so how 255 could be the answer?

Abhishek said:   1 decade ago
char i;
for(i=0;i<255;i++)
{
printf("%c",i);
}

I want to ask that after 255. What will happen? It will go to 0 and continue to run from 1, 2 or will run infinite times?

Vibhan said:   10 years ago
Sorry friends but get excited 256 because it check while loop at the time when j value is 256. So while statement execute 256 times.

Tara said:   10 years ago
The loop will execute for infinity times, like 0 to 128.

Then -128 to -1 again from 0-128, the loop repeats.

Sowjanya said:   10 years ago
Thank you all for your suggestions.

Vishal zadafiya said:   9 years ago
Loop will execute infinite time because int by default signed ao its range is -127 to 128 first it's execute 1 to 128 then 0 to 127 and again start 1 to 128.


Post your comments here:

Your comments will be displayed after verification.