Conditional Control Structure CH# 04
Questions
and Answers
Short Questions
i.
Differentiate
between if and if-else selection structures.
Ans: Structure
of IF Statement:
The
if statement has the following general form.
If
(condition)
{
Block
statement
}
When
this statement is executed, the condition is evaluated. If the condition is true
then the block of the statements within the braces will be executed. If false
then the control will be transferred to the next statement if exists. If there
is only one statement then braces are not required.
Structure of IF-ELSE Statement:
The
if-else statement has the following general form.
If
(condition)
{
Block
statements
}
else
{
Block
statements
}
When
this statement is executed, the condition is evaluated. If the condition is
true then the block of the statements within the braces will be executed and
else will be skipped. If false then the block of statements following if will
be skipped and else will be executed.
➤➤➤➤➤➤➤➤➤➤➤➤
ii.
Differentiate between else-if and switch
selection structure.
Ans: Structure of ELSE- IF Statement:
The
else-if statement has the following general form.
If
(condition-1)
{
Block
statements
}
else-If (condition-2)
{
Block
statements
}
else-If (condition-3)
{
Block
statements
}
else
{
Block
statements to be executed when
none
of the conditions is true.
}
When
this statement is executed, condition-1 is evaluated, if it is true then the
block of statements following if is executed and if it is false, the next
condition is evaluated. If none of the conditions is true then the block of
statements following else is executed
automatically.
Structure of SWITCH
Statement:
The
switch statement has the following general form.
switch
(expression)
{
Case const-1:
statements;
break;
Case const-2:
statements;
break;
.
.
.
default:
statements;
}
The switch statement is similar to the
else-if, it is used when multiple choices are given and one is to be selected.
1.
When switch statement is executed the
expression is evaluated. The result of the expression is compared with constant
values given after the keyword case.
2.
If the
result matches the constant value then the statement under the case is
executed.
3.
The purpose of break is to exit the body of
switch statement.
4.
If no case is matched then the statements
under the default keyword is executed. Its use is optional.
➤➤➤➤➤➤➤➤➤➤➤➤
iii. What is nested selection structure?
Ans: Nested
Selection Structure:
The selection structure that is within another selection
structure is known as nested selection structure. Sometime in computer
programming it is required. This is also supported in C language.
Example:
#include <stdio.h>
#include <conio.h>
void main (void)
{
Int n;
printf(“\n Enter a number:”);
scanf(“%d ,&n”);
If(n>0)
{
printf(“\n You entered a positive number.”);
If (n%2= = 0)
printf(“\n it is an even number.”);
else
printf(“\n it is an odd number.”);
}
else
Printf(“\nYou entered a negative number or
zero.”);
getch();
}
➤➤➤➤➤➤➤➤➤➤➤➤
Fbise Notes-Computer | Conditional Control Structure Short Questions Ch.No 4
Reviewed by fbisenotes
on
July 24, 2019
Rating:
No comments: