80% found this document useful (5 votes)
3K views22 pages

Chapter 4 Computer Science 10 Class Federal Board

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
80% found this document useful (5 votes)
3K views22 pages

Chapter 4 Computer Science 10 Class Federal Board

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

4 Conditional Control Structure 3

4.1 CONTROL STRUCTURE

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

Fig.4-1 Program to demonstrate the use of if statement


4 Conditional Control Structure 5
4 Conditional Control Structure
When if statement of this program is executed, the condition inside the brackets is
evaluated.
If the condition is true, that is, the marks entered are greater than 32, then the two statements
following the keyword if are executed.
If the condition is false, that is, the marks entered are less than 33, then the following two
statements will be skipped and the program will terminate and there will be no output.
The output of the program is shown in Fig.4-2, if the marks entered are more than 32.

Fig.4-2 Output of Program 1


Program 2: The program in Fig.4-3 prints square of a number.

Fig.4-3 Program to print square of a number


6
When the above program is
executed, it will prompt the user
to enter a number.
If the user enters a number
greater than zero, it will print the
square of the number as shown
in Fig.4-4. Fig.4-4 Output of Program 2

4.1.5 STRUCTURE OF IF-ELSE STATEMENT


The if-else statement is used in situation where some code is to be executed if a condition is
true and some other code is to be executed if the condition is false.
The if-else statement has the following general form.
if (condition)
{
Block of statements
}
else
{
Block of statements
}
When if-else statement is executed, the condition is evaluated.
If the condition is true then the block of statements following if will be executed and the
block of statements following else will be skipped.
If the condition is false then the block of statements following if will be skipped and th e
block of statements following else will be executed.
If a single statement is to be executed after if or else then braces are not required.
4.1.6 USE OF IF-ELSE STATEMENT
Program 3: The program in Fig.4- 5, reads marks and prints the message whether the student
passed or failed.

Fig.4-5 Program to demonstrate the use of if-else statement


4 Conditional Control Structure 7
4 Conditional Control Structure
In this program, if the marks entered are above 32 then the statements following if are
executed and the message shown in Fig.4-6 (a) will be printed.
If the marks entered are below 33 then the statement following else are executed and the
message shown in Fig.4-6 (b) will be printed.
Also note that, there is a single statement to be executed if the condition is false. Therefore,
braces are not required after else.

Fig.4-6 (a) Output when marks are grater than 32


(b) Output when marks are less than 33

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.

Fig.4-7 Program to demonstrate the use of if-else statement

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).

Fig.4-8 (a) Execution of program when value of n is 14.

Fig.4-8 (b) Execution of program when value of n is 25.

4.1.7 STRUCTURE OF IF-ELSE-IF STATEMENT


The else-if is a type of conditional statement that combines more than two conditions. It
allows the programmer to make a decision based on several conditions.
The else-if statement has the following general form.
if(condition-1)
{
Block of statements
}
else if(condition-2)
{
Block of statements
}
4 Conditional Control Structure 9
4 Conditional Control Structure
else if(condition-3)
{
Block of statements
}
.
.
.
else
{
Block of 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 any condition is true then the following block of statements is executed.
If none of the conditions is true then the block of statements following else is executed
automatically.
If a single statement is to be executed after if, else if or else, instead of a set of statements
then the braces are not required.
4.1.8 USE OF ELSE-IF STATEMENT
Program 5: The program in Fig.4-9 perform
s an arithmetic operation based on the choice of user.

Fig.4-9 Program to demonstrate the use of else-if statement


10
When this program is executed, it will display four choices and prompt the user to enter his
choice. User’s choice will be stored in the variable choice.
After this, it will again prompt the user to enter two numbers which will be stored in variables
x and y.
Now, the if-else-if statement will be executed. First condition will be checked first, if it is
true, that is, the value stored in the variable choice is 1 then sum of x and y will be
evaluated and printed.
If the first condition is false then the second condition will be checked and if it is true, the n
the difference (x-y) will be evaluated and printed and so on.
If all the conditions are false then the statement following else will the executed and the
message “Invalid Command” will be printed.
In this program, the format specifier %6.2f is used for printing floating-point value. Here, 6
is the total field width for printing the number and reserves 6 spaced for printing it. The digit 2
means 2 digits are to be printed after the decimal point. If the floating-point number to be printed
requires less than 6 spaces then it will be right justified and extra spaces will appear at the left
side of the number.
The execution of this program is shown in Fig.4-10, when user choice is 3.

Fig.4-10 Execution of Program 5

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

Fig.4-11 Program to find Grade

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.

Fig.4-12 Execution of Program 6


4 Conditional Control Structure 81
4.1.9 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 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.

In switch statement, it is allowed to use a variable within the parenthesis instead of an


expression based on which statements under a case can be 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.

The expression should be of type int, char but not float.


82 4 Conditional Control Structure
Program 7: The program in Fig.4-13 uses a variable of type integer as switch variable.

Fig.4-13 Program to demonstrate use of switch statem ent

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

Fig.4-14 Program to print a message based on grade

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.

Fig.4-15 Program to create a simple calculator

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.

Fig.4-16 Using conditional operator in a program

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

Fig.4-17 Execution of Program 10

4.1.10 NESTED SELECTION STRUCTURE


The selection structure that is within another selection structure is known as nested
selection structure. Sometimes, in computer programming, it is required to use a selection
structure within another selection structure. This is also supported in C language. In C language,
the programmer can have a selection structure ( if, if-else, else-if or switch statement) within
another selection structure.
Program 11: The program in Fig.4-18 demonstrates the use of nested if-else statement within
another if-else statement.
When this program is executed, it reads an integer number, stores it in the variable n and
then the condition (n>0) is evaluated.
If it is true, it prints the message that the entered number is a positive number and then the
nested if-else statement is executed.
The nested if-else statement prints whether n is an even number or an odd number.
If the condition (n>o) is false then the statements following the first if are skipped and the
last statement after the else is executed which prints the message that the user entered a
negative number or zero.
4 Conditional Control Structure 87

Fig.4-18 Using nested if-else statement in a program

Execution of the program is shown in Fig.4-19.

Fig.4-19 Execution of Program 11


88

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

Q1. Select the best answer for the following MCQs.


i. For which purpose if structure is used in programming?
A. Repetition B. Selection
C. Sequence D. Input of data
4 Conditional Control Structure 89

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.

You might also like