You are on page 1of 10

Programs on if and switch statements

Write the output:


1. Program 2
#include <stdio.h>
int main()
{
int num = 2;
switch (num + 2)
{
case 1:
printf("Case 1: ");
case 2:
printf("Case 2: ");
case 3:
printf("Case 3: ");
default:
printf("Default: ");
}
return 0;
}
Output:
Default:
Explanation: In switch, an expression “num+2” where num value is 2 and after addition the
expression result is 4. Since there is no case defined with value 4 the default case got
executed.

2. Program 2

#include<stdio.h>
void main()
{
int movie = 1;
switch (movie << (2 + movie))
{
default:
printf(" Traffic");
case 4:
printf(" Sultan");
case 5:
printf(" Dangal");
case 8:
printf(" Bahubali");
}
}
Output:
Bahubali
Explanation: We can write case statement in any order including the default case. That
default case may be first case, last case or in between the any case in the switch case
statement. The value of expression “movie << (2 + movie)" is 8.
3. Program 3
filter_none

#include<stdio.h>
#define L 10
void main()
{
auto a = 10;
switch (a, a*2)
{
case L:
printf("ABC");
break;

case L*2:
printf("XYZ");
break;

case L*3:
printf("PQR");
break;

default:
printf("MNO");

case L*4:
printf("www");
break;
}
}
Output:
XYZ
Explanation: In C, comma is also operator with least precedence. So if
x = (a, b);
Then x = b
Note: Case expression can be macro constant.
4. Program 4

#include<stdio.h>
void main()
{
switch(2)
{
case 1L:
printf("No");
case 2L:
printf("%s","GEEKS");
goto Love;

case 3L:
printf("Please");

case 4L:Love:
printf("FOR");
}
}
Output:
GEEKSFOR
Explanation: It is possible to write label of goto statement in the case of switch case
statement.

What will be the output of the C program?


#include<stdio.h>
int main()
{
int i = 5, j = 6, k = 7;
if(i > j == k)
printf("%d %d %d", i++, ++j, --k);
else
printf("%d %d %d", i, j, k);
return 0;
}

A. 5 7 6
B. 5 6 7
C. 6 6 6
D. 5 7 7

Answer: Option B

Explanation
The if statement condition is (i > j == k) becomes (5 > 6 == 7) its looks like
(expression1 == expression2), expression1 becomes 0 and expression2 is
a non-zero value so expression2 returns 1. Finally the condition is False
(0 == 1) so else block gets executed.

What will be the output of the C program?


#include<stdio.h>
int main()
{
int i = 2;
if(i == (1, 2))
printf("Hai");
else
printf("No Hai");
return 0;
}

A. Compilation Error
B. Runtime Error
C. Hai
D. No Hai

Option: C

Explanation
The condition if(i == (1, 2)) is if(expression1 == (expression2,
expression3)). The expression3 is evaluated last so the if block gets
executed.

#include<stdio.h>
int main()
{
if(printf("0"))
printf("inside if block");
else
printf("inside else block");
return 0;
}

A. inside if block ✘
B. inside else block
C. 0inside else block
D. 0inside if block ✔

What will be the output of the C program?


#include<stdio.h>
int main(){
int i = 5, j = 4;
if(!printf(""))
printf("%d %d", i, j);
else
printf("%d %d", i++, ++j);
return 0;
}

A. 6 5
B. 5 5✘
C. 5 4✔
D. 6 4

Option: C

Explanation
The if(!printf("")) is in a format if(!expression). The expression(printf("")
returns 0 because it prints nothing) and the condition becomes true
because of ! operator. So the if block gets executed.

What will be the output of the C program?


#include<stdio.h>
int main()
{
int i = 1, j = 0 ;
if(i-- == j)
printf("i = %d", --i);
else
printf("j = %d", ++j);
return 0;
}

A. i = 1
B. i = -1
C. i = 0
D. j = 1✔

Option: D

Explanation
The if(i-- == j) will be if(1 == 0). It uses post decrement operator so, first
the value i is used then it decrementd by one. i.e) if(1 == 0) condition is
false so it executes the else block

What will be the output of the C program?


#include<stdio.h>
int main()
{
int i = 5, j = 5;
if(i == j);
printf("Equal");
else
printf("Not Equal");
return 0;
}

A. Compilation Error ✔
B. Runtime Error
C. Equal
D. Not Equal

Option: A
Explanation
Compilation Error: Misplaced else
else block needs to follow directly after the statement of if block. In the
above program, the if statement ends with semicolon so there is no block
of statements under if block. The printf statement after if is normal one. So
else block is misplaced in this program.

What will be the output of the C program?


#include<stdio.h>
int main()
{
int i = 25;
if(i == 25);
i = 50;
if(i == 25)
i = i + 1;
else
i = i + 1;
printf("%d", i);
return 0;
}

A. 50
B. 51 ✔
C. 26 ✘
D. 27

Option: B

Explanation
Sorry. No Explanation.

What will be the output of the C program ?


#include<stdio.h>
int main(){
if("May I Get in")
printf("yes, Get in");
else
printf("No");
return 0;
}

A. Compilation Error
B. No
C. yes, Get in ✔
D. None of above

Option: C

Explanation
The expression inside if returns true. So the if block gets executed.

What will be the output of the C program?


#include<stdio.h>
int main()
{
if(sizeof(0))
printf("Hai");
else
printf("Bye");
return 0;
}

A. Bye
B. Hai ✔
C. Compilation Error
D. None

Option: B

Explanation
The sizeof(0) will returns size of integer, So the condtion is true.
What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 1;
switch(i)
{
case i:
printf("case 1 executed");
break;
case i + 1;
printf("case 2 executed");
break;
default:
printf("default block executed");
break;
}
return 0;
}

A. Compilation Error
B. case 1 executed ✘
C. case 2 executed
D. default block executed

Option: A

Explanation
Compilation Error: Constant expression required. The case statement
accepts only a constant expression.

Write the output

#include <stdio.h>
int main()
{
int x;
x = 5 > 8 ? 10 : 1 != 2 < 5 ? 20 : 30;
printf("Value of x:%d", x);
return 0;
}

Output:
Value of x:30

You might also like