Chapter 4 Computer Science 10 Class Federal Board
Chapter 4 Computer Science 10 Class Federal Board
friend is at
picnic.
4 Everybody makes different decisions in the daily life. For example, if my
home then I will visit him. If the weather is good tomorrow, I will go for
Similarly, computer programmers also have to make decisions and
perform some operations depending on the user input to the program. Control
structures are used in programs to implement decisions.
4.1.1 CONROL STATEMENT
A control statement is an instruction which determines the sequence of execution of other
statements. In other words, it controls the flow of execution of program statements.
4.1.2 CONDITIONAL STATEMENT
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 and 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.
4.1.3 STRUCTURE OF IF STATEMENT
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 and 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.
4.1.4 USE OF IF STATEMENT
Program 1: The program in Fig.4-1, demonstrates the use of if statement.
4
Program 4: The program in Fig.4-7 reads a number from the keyboard and prints the message
whether it is an even or an odd number.
When this program is executed it prompts the user to enter the value of n.
The remainder operator % is used to divide the number by 2 and store the remainder in
variable rem.
The value of rem is checked. If it is equal to 0 then the number n, is an even number as
shown in Fig.4-8 (a).
8
If the value of rem is not equal to 0, in other words, it is equal to 1 then the number n is an
odd number as shown in Fig.4-8 (b).
Program 6: The following program reads marks of a student and printshis letter grade according
to the following scheme.
Marks Grade
80~100 A
70~79 B
60~69 C
50~59 D
Below 50 F
80 4 Conditional Control Structure
When the above program is executed, it will prompt the user to enter marks.
It will check in which range the marks fall and then accordingly print the grade.
If none of the conditions is true then the statement followingelse will be executed and it will
print the message “Grade F”.The execution of the program is shown in Fig.4- 12, if the marks
entered by the user are 66. Since 66 falls in the range 60 to 69, so the grade displayed is C.
The switch statement is similar to the else-if statement. It is used 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. The result of
expression is compared with the constant values given after the keyword case . If the
result matches the constant value after any case then the statements under that case
are executed.
The purpose of break statement is to exit the body of the switch statement after executing
the statements under a case and transfer control to the first statement following the end of
the switch statement.
If no case is matched then the statements under thedefault keyword are executed. Its use
is optional. If it is not used then the control exits from the body of theswitch statement and
goes to the first statement following the end of the switch statement.
When this program is executed, the switch variable must have an integer value. The value
of switch variable n is compared with the constant values following the case keyword and
if it matches, control is transferred to the statements following that particular case.
If the switch variable does not match any of thecase constants, control goes to the keyword
default which is at the end of the switch statement.
Notice the use of break statement in this program. It terminates theswitch statement when
the body of the statements in a particular case has been executed.
Program 8: The program in Fig.4-14 prompts the user to enter a grade and prints appropriate
message. It uses a variable of type character as switch variable.
4 Conditional Control Structure 83
This program prompts the user to enter a grade, that is, ‘A’, ‘B’, ‘C’, ‘D’ or ‘F’ and stores it
in the variable grade. It will print the message “Excellent” for grade ‘ A’ and “ Well Done ”
for grade ‘B’.
If the user enters grade ‘ C’ or ‘D’, the same message “ Satisfactory” will be printed. In the
absence of statements after a case, the control falls right through one case to the case
below and this makes it easy for several values of switch variable to execute the same
code.
Finally, if the user enters grade ‘F’, the program will print the message “Poor
Performance”.
In this program there is no default keyword. Therefore, the whole switch statement simply
terminates when there is no match.
84 4 Conditional Control Structure
Program 9: The program in Fig.4-15 creates a simple calculator to perform addition, subtraction,
multiplication or division.
When the above program is executed, it will ask the user to enter the type of operation to
be performed on two numbers. Then it will ask for two numbers to be entered.
Based on the operator, it will perform addition, subtraction, multiplication or division and
print the result.
Advantage and Limitation of switch Statement
The switch statement allows a variable to be compared against a list of constant values.
When there is a match to a case, the statements following that case will execute until a
break statement is reached. This makes the logic of program simple and easy to
understand.
The switch statement has a limitation. It is not allowed to use relational operators in the
expression of switch statement. For example, to check for passing marks, the condition,
4 Conditional Control Structure 85
(marks>32) cannot be used in switch statement. It is also not possible to check for a range
such as ((marks>=70)&&(marks<=80)) in a switch statement, for this, the programmer has
to use if, if-else or else-if statement.
Using Conditional Operator in Program
Program 10: The program in Fig.4-16 reads marks and pr ints the message “PASS” or “FAIL”,
using conditional operator instead of if-else statement. Conditional operator was explained in
the previous unit.
When this program is executed, it will ask the user to enter the marks and store it in the
variable marks.
The condition (marks>32) will be evaluated.
If the condition is true, that is, marks are greater than 32 then the first print statement will
be executed and the message “PASS” will be printed.
If the condition is false then the second print statement will be executed and the message
“FAIL” will be printed.
Execution of the program is shown in Fig.4-17.
86 4 Conditional Control Structure
Key Points
• In a programming language, a control statement is an instruction which determines the
sequence of execution of other statements in a program.
• A conditional statement is an instruction in a programming language that contains a
condition based on which flow of execution of program statements is controlled.
• The if statement is a control statement. When it is executed, the condition is evaluated. If
it is true then the block of statement within the braces will be executed otherwise control
will be transferred to the next statement.
• The if-else statement is a control statement. When it is executed, the condition is evaluated.
If it is true then the block of statements following if will be executed otherwise the block of
statements following else will be executed.
• The if-else-if statement is a control statement. When it is executed, the first condition is
evaluated. If it is true then the block of statements following if will be executed. It the
condition is false then the second condition after else if will be evaluated. If it is true, then
the block of statements following else if will be executed. If any condition is true the block
of statements following that else if will be executed. If none of the conditions is true then
the block of statements following else will be executed automatically if it exists otherwise
the control will be transferred to the next statement.
• The switch statement is used when multiple choices are given and one choice is to be
selected. It is similar to else-if statement.
• A selection structure within another selection structure is known as nested selection
structure.
Exercise
ii. Which statement is suitable to use in a situation where there are only two choices based
on a condition?
A. if statement B. if-else-if statement
C. if-else statement D. switch statement
90
iii. Which statement can be used in place of switch statement?
A. if statement B. if-else statement
C. if-else-if statement D. conditional operator
iv. Which statement can be used in place of conditional operator?
A. if statement B. if-else statement
C. else-if statement D. switch statement
v. Which statement is used to exit from the body of switch statement?
A. default B. continue
C. exit D. break
vi. Which of the following is a multiple selection statement?
A. if statement B. if-else statement
C. if-else-if statement D. none of these
vii. Which of the selection structures tests only for equality?
A. if statement B. if-else statement
C. else-if statement D. switch statement
viii. What will be printed when the following code is executed?
x=1;
switch(x)
{ case 1:
case 2:
case 3:
printf(“\n x is a positive number”);
break;
default
printf(“\n value of x is 1”);
}
A. value of x is 1 B. x is a positive number
C. Nothing will be printed D. It will give error
Short Questions
Q2. Give short answers to the following questions.
i. Differentiate between if and if-else selection structures.
ii. Differentiate between else-if and switch selection structures.
iii. What is nested selection structure?
iv. Write the following statement using if-else statement.
k = (a+b>20)? a+3*b: a-b;
90 4 Conditional Control Structure
v. Write the following statement using conditional operator.
if (x>y)
z=(x+y)/3;
else
z=x-5*y;
vi. What will be the output of the following code?
int n, count, sum;
n=28; count=15; sum=30;
if (n<25)
{ count=count+5;
printf(“\nCount=%d”,count); }
else
{ count=count-5;
sum=sum+n;
printf(“\nCount=%d”,count);
printf(“\nSum=%d”,sum); }
vii. 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;
}
Extensive Questions
Q3. What is control structure? Explain conditional control structure with examples.
Q4. What is the purpose of switch() statement? Explain with the help of one example.