Programming Interview / Viva Q&A: 5 (while loop)


In this video tutorial lets understand the while loop a little better. We’ve posted source code of a C program below which has while loop. You need to guess the output of the program.

Related Read:
while loop in C programming

Guess output of the program

 

int main()
{
    int count = 0;

    printf("Start of program\n");

    while(count <= 5);
    {
        printf("Count = %d\n", count);
        count = count + 1;
    }

    printf("End of program\n");

    return 0;
}

Output:
Start of program

Since above C source code has semicolon(;) immediately after while(count <= 5) the control enters infinite loop or indefinite loop, and no other statements after the semicolon gets executed. And the program doesn’t get terminated with return 0.

C Programming Interview / Viva Q&A: 5 (While Loop)


[youtube https://www.youtube.com/watch?v=fqYykTU-jrA]

YouTube Link: https://www.youtube.com/watch?v=fqYykTU-jrA [Watch the Video In Full Screen.]


If you remove the semicolon(;) after while(count <= 5) then the program will output numbers from 0 to 5 and the “End of program” statement.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *