Decision Constructs

(a) What is the use of “else-if” statement?

else-if statement is another type of if statement. It executes one block of statements when the given condition is true and the other when it is false. In any situation, one block of statements is executed and the other block of statements is skipped.

(b) Why should programmer use a default label in a switch statement?

The default label appears at the end of all case labels in switch statement. It is executed only when the result of expression does not match with any case label. Its use is optional. The position of default label is not fixed. It may be placed before the first case statement or after the last one.

(c) Write the syntax of if-else-if statement.

if (condition)
{ Block 1; }
else if (condition)
{ Block 2; }
else if (condition)
{ Block 3; } . . . }
else
{ Block N; }

(d) What is the output of the following code:
int a=1;
int b=5;
if(a+b<6)
printf(“%d”,a);
else
printf(“%d”,b);
printf(“%d”,a+b);

The Output is: 56

(e) Write a program that inputs a number from user and finds whether it is even or odd using if else structure.

#include
#include
void main()
{
int n;
printf(“Enter a number: “);
scanf(“%d”,&n);
if(n%2==0)
printf(“%d id even.”,n);
else
printf(“%d is odd.”,n);
getch();
}

(f) Write a program to input a number and check if it is prime or not prime.

#include
#include
void main()
{
int n, p, j;
printf(“Enter a number:”);
scanf(“%d”,&n);
p=1;
for(j=2;j if(n%j==0)
{
p=0;
break;
}
if(p==1)
printf(“Prime number”);
else
printf(“Not prime number”);
getch();
}

(g) Write a note on if-else statement.

if-else statement is another type of if statement. It executes one block of statements when the condition is true and the other when it is false. In any situation, one block is executed and the other is skipped. In if-else statement:
Both blocks of statements can never be executed.
Both blocks of statements can never be skipped.
Syntax
Its syntax is as follows:
if(condition)
{
statement;
}
else
{
statement;
}

(h) Differentiate between if-else-if statement and switch statement.

if-else-if Statement
if-else-if statement can be used to choose one block of statements from many blocks of statements. It is used when there are many options and only one block of statements should be executed on the basis of condition.

Switch Statement
The switch statement is another conditional structure. It can be used easily when there are many choices available and only one should be executed. Switch statement compares the result of a single expression with multiple cases.

(i) Write a program that inputs a character from the user and checks whether it’s a vowel or a consonant, using switch statement.

#include
#include
void main()
{
char c;
printf(“Enter an alphabet: “);
scanf(“%c”, &c);
switch(c)
{
case’a’:
case’A’:
printf(“You entered vowel.”);
break;
case’e’:
case’E’:
printf(“You entered vowel.”);
break;
case’i’:
case’I’:
printf(“You entered vowel.”);
break;
case’o’:
case’O’:
printf(“You entered vowel.”);
break;
case’u’:
case’U’:
printf(“You entered vowel.”);
break;
default:
printf(“You entered a consonant.”);
}
getch();
}

(j) Write a program that inputs a character from the user and checks whether it’s a vowel or a consonant, using switch statement.

#include
#include
void main()
{
char c;
printf(“Enter an alphabet: “);
scanf(“%c”, &c);
switch(c)
{
case’a’:
case’A’:
printf(“You entered vowel.”);
break;
case’e’:
case’E’:
printf(“You entered vowel.”);
break;
case’i’:
case’I’:
printf(“You entered vowel.”);
break;
case’o’:
case’O’:
printf(“You entered vowel.”);
break;
case’u’:
case’U’:
printf(“You entered vowel.”);
break;
default:
printf(“You entered a consonant.”);
}
getch();
}