Conditional Control structure ,Short Questions, Computer Science
Chapter # 4 Conditional Control structure
SHORT QUESTIONS
Question 1. What is control statement?
Ans: A control statement is an instruction which determine the sequence of
execution of other statements. In other words, it controls the flow of
execution of program of program statements.
Ans: A conditional statement is an instruction in a programming language
that contains a condition. When a
conditional statement is executed, first the condition is evaluated then based
on the result (True or False) a particular statement or a set of statements is
executed.
Conditional
statements of c language are: if, if-else,
else-if and switch statements.
Ans:
The if statement has the following general form.
if(condition)
{
Block of statements
}
When this statement is executed the condition is
evaluated. If the condition is true, then the block of statements within the braces will be executed. If the
condition is false, then the block of statements within the braces will be skipped the control will be
transferred to the next statement if any exists. If there is only one statement to be executed if the condition is true,
then braces are not required.
Program:
#include<stdio.h>
#include<conio.h>
int main(int)
{
int
marks;
printf(“\nEnter
your marks:”);
scanf(“%d”,
&marks);
if(marks>32)
{
Printf(“\nCongratulations”);
Printf(“\nYou
have passed”);
}
getch();
}
Output:
Enter your marks:54
Congratulations
You have passed_
Question 4. What is the purpose of switch () statement? Explain with the help of one example.
Ans:
The
switch statement has the following general form.
Switch
(expression)
{
Case
const-1:
Statement;
Break;
Case
const-2:
Statement;
Break;
.
.
.
.
default:
statement;
}
·
The switch statement is similar to the else-if
statement.it is sued when multiple choices are given and one choice is to be
selected.
·
When switch statement is executed the expression is
evaluated. Based on the result of expression one of the cases in the switch
statement is executed.
·
In switch statement, it is allowed to use a variable
within the parentheses instead of an expression based on which statements under
a case can be executed.
·
Program
# 1.
#include<stdio.h>
#include<conio.h>
int main(int)
{
int
n;
printf(“\nEnter
an interger. N: ”);
scanf(“%d”,
&n);
switch(n)
{
case
1:
printf(“N
is 1”);
break;
case 2:
printf(“N
is 2”);
break;
default;
printf(“N
is not 1 or 2”);
}
getch();
}
Question
5. What will be the output of the following code?
char ch;
ch=’c’:
switch(ch)
{
Case ‘a’:
Printf(“\n
Good Morning! “); break;
Case ‘b’:
Printf(“\n
Have a Nice Day! “); break;
Case
‘c’:
Case ‘d’:
Case ‘e’:
Printf(“\n Good Bye! “); break;
Comments
Post a Comment