Chapter - 5: The Case Control Structure

What will be the output of the following programs:


A
Sections
1
Exercises

A

B

C

D

1


(a)

#include<stdio.h>
int main()
{
	char suite = 3 ;
	switch ( suite )
	{
	case 1 :
		printf ( "\nDiamond" ) ;
	case 2 :
		printf ( "\nSpade" ) ;
	default :
		printf ( "\nHeart") ;
	}
	printf ( "\nI thought one wears a suite" ) ; 
	return 0;
}

Output:

Heart
I thought one wears a suite.


(b)

#include<stdio.h>
int main()
{
	int c = 3 ;
	switch ( c )
	{
	case '3':
		printf("You never win the silver prize.\n");
		break;
	case 3:
		printf("You always loose the gold prize\n");
		break;
	default:
		printf("Of course provided you win a prize.\n");
	}
	return 0;
}

Output: You always lose the gold prize.


(c)

#include<stdio.h>
int main()
{
	int i = 3;
	switch ( i )
	{
	case 0 :
		printf ( "\nCustomers are dicey" ) ;
	case 1+0 :
		printf ( "\nMarkets are pricey" ) ;
	case 4/2 :
		printf ( "\nInvestors are moody" ) ;
	case 8%5 :
		printf ( "\nAt least employees are good" ) ;
	} 
	return 0;
}

Output: At least employees are good.


(d)

#include<stdio.h>
int main()
{
	int k;
	float j = 2.0;
	switch ( k = j + 1 )
	{
	case 3 :
		printf ( "\nTrapped" ) ;
		break ;
	default :
		printf ( "\nCaught!" ) ;
	} 
	return 0;
}

Output: Trapped


(e)

#include<stdio.h>
int main()
{
	int ch = 'a' + 'b' ;
	switch ( ch )
	{
	case 'a' :
	case 'b' :
		printf ( "\nYou entered b" ) ;
	case 'A' :
		printf ( "\na as in ashar" ) ;
	case 'b' + 'a' :
		printf ( "\nYou entered a and b" ) ;
	} 
	return 0;
}

Output: You entered a and b


(f)

#include<stdio.h>
int main()
{
	int i = 1 ;
	switch ( i - 2 )
	{
	case -1 :
		printf ( "\nFeeding fish" ) ;
	case 0 :
		printf ( "\nWeeding grass" ) ;
	case 1 :
		printf ( "\nMending roof" ) ;
	default :
		printf ( "\nJust to survive" );
	} 
	return 0;
}

Output: 

Feeding fish
Weeding grass
Mending roo
Just to survive


© 2020 Garbage Valuegarbage value logo