Open In App

Output of C programs | Set 59 (Loops and Control Statements)

Last Updated : 31 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : Control Statements
Q.1 What is the output of this program? 
 

CPP




#include <iostream>
using namespace std;
int main()
{
    unsigned char counter = 0;
    for (counter = 0; counter <= 255; counter++) {
        printf("%d ", counter);
    }
    return 0;
}


Options 
a) 0 1 2 … infinite times 
b) 0 1 2 … 127 
c) 0 
d) 1 
 

ans:- d 

Explanation : Before entering into the for loop the CHECK CONDITION is “evaluated”. Here it is evaluated to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).
Q.2 What is the output of this program? 
 

CPP




#include <iostream>
using namespace std;
int main()
{
    int count = 0;
    for (;;) {
        if (count == 10)
            break;
        printf("%d ", ++count);
    }
    return 0;
}


Options
a) Compiler Error 
b) 12345geeksforgeeks.org 
c) 1234geeksforgeeks.org 
d) 1geeksforgeeks.org 2geeksforgeeks.org 3geeksforgeeks.org 
4geeksforgeeks.org 5geeksforgeeks.org 
 

ans:- a

Explanation : Compiler error: Undefined label ‘print’ in function main. Labels have functions scope; in other words, the scope of the labels is limited to functions. The label ‘print’ is available in function fun(). Hence it is not visible in function main.
Q.3 What is the output of this program? 
 

CPP




#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int count;
    for (count = 0; count < 10; ++count) {
        printf("#");
        if (count > 6)
            continue;
        printf("%d", count);
    }
    return 0;
}


Options 
a) 0 1 2 … infinite times 
b) 0 1 2 … 255 
c) compilation error 
d) run time error 
 

ans:- a

Explanation : The range of unsigned char is 0 to 255 and when the value of var will cross over 255, value will be 0 and again same process will happen.
Q.4 What is the output of this program? 
 

CPP





Options 
a) 0 1 2 3 4 5 6 7 8 9 10 
b) 0 1 2 3 … infinite times 
c) 1 2 3 4 5 6 7 8 9 10 
d) 1 2 3 4 5 6 7 8 9
 

ans:- c

Explanation : for(;;) it is possible in C, there is no need to place condition with in the for(), you can place condition with in the body of the loop.
Q.5 What is the output of this program? 
 

CPP




#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int count;
    for (count = 0; count < 10; ++count) {
        printf("#");
        if (count > 6)
            continue;
        printf("%d", count);
    }
    return 0;
}


Options 
a)#0#1#2#3#4#5#6### 
b)#0#1#2#3#4#5#6#7#8#9#10 
c)#0#1#2#3#4#5##7#8#9#10 
d)#0#1#2#3#4#5# 
 

ans:- a

Explanation : prints # and then value of count until its value becomes 6. After count is greater than 6, loop continues and prints # for the left iterations(i.e. for 7 8 and 9). Now the condition becomes false so loop terminates.

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads