You are on page 1of 28

COMPUTER SCIENCE

12
(MS Access and C)

CHAPTER 11: Decision Constructs

Copyright @ IT Series www.itseries.com.pk


Topics
• Control Structure
• if Statement
• if-else Statement
• if-else-if Statement
• Nested if Statement
• Switch Statement
• Difference between if-else-if and switch Statement
• Conditional Operator

Copyright @ IT Series www.itseries.com.pk


 Control structures control the flow of execution in a program
 The control structures in C language are used to combine individual instruction into a single logical unit
 The logical unit has one entry point and one exit point
 There are three kinds of execution flow:
• Sequence possible to perform mathematical operation on character values
• Selection
• Repetition

Copyright © IT Series
Sequence
 Executes the statements in the same order in which they are written in the program
Flowchart
Entry point

possible to perform mathematical operation on character values

Exit point

Copyright @ IT Series www.itseries.com.pk


Selection
 A control structure which chooses alternative to execute
 Executes a statement or set of statements based on a condition
 Also called decision-making structure or conditional structure.
Entry point
 possible
Different types of selection to perform
structures in Cmathematical
language: operation on character values
 If Flowchart

 if-else
 if-else-if
 switch

Exit point

Copyright @ IT Series www.itseries.com.pk


Repetition
 A control structure which executes a statement or set of statements repeatedly for a specific number of times
 Also called iteration structure or loop
 Different types of loops available in C language: Entry point
 while loop possible Flowchart
to perform mathematical operation on character values
 do-while loop
 for loop

Exit point

Copyright @ IT Series www.itseries.com.pk


if is a keyword in C
language. It is always
written in lowercase.
 The if statement is the primary selection control structure
 It is used to execute or skip a statement or set of statements by checking a condition
 The condition is given as a relational expression. e. g marks >=40
Syntax / General Form
The syntax of if statement for single statement:
possible to perform mathematical operation on character values
if (condition) Do not place ; after (condition)

statement;
The syntax for compound statements:
if (condition)
{ Block of statements
statement 1; inside the braces is
called the body of the if
statement 2; statement. If there is
 only 1 statement in the
body, the { } may be
statement N; omitted.
}

Copyright @ IT Series www.itseries.com.pk


If Programs

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright
Copyright @©ITITSeries
Series www.itseries.com.pk
 The simplest selection structure but it is very limited in its use
 Statement or set of statements is executed if the condition is true
 But if the condition is false then nothing happens (no alternate action is performed)
 A user may want to:
Execute one statement or set of statements if the condition is true
possible to perform mathematical operation on character values
Execute other statement or set of statements if the condition is false
 In this situation, simple if statement cannot be used effectively
 Solution  ‘if-else’ structure can be used to handle this kind of situation effectively
Example
A program should display Pass! if the student gets 40 or more marks
It should display Fail! if the student gets less than 40 marks
Simple if statement cannot be used to handle this situation

Copyright @ IT Series www.itseries.com.pk


 Used to make two-way decisions
 It executes one block of statement(s) when the condition is true and the other when it is false
 In any situation, one block is executed and the other is skipped
 Both blocks of statement can never be executed
 Both blocks of statements can never be skipped
Syntax / General Form
possible
The syntax of if-else statement to perform
for single mathematical
and compound operation on character values
statements:

Condition

Statement

Copyright © IT Series
If-else Programs

possible to perform mathematical operation on character values

Copyright
Copyright @©ITITSeries
Series www.itseries.com.pk
possible to perform mathematical operation on character values

Copyright
Copyright @©ITITSeries
Series www.itseries.com.pk
 It is used when there are many options and only one block of statements should be executed on the basis of
a condition

possible to perform mathematical operation on character values

Working of if-else-if
 The condition 1 is evaluated and the first block of statements is executed if the condition 1 is true
 The condition 2 is evaluated if the condition 1 is false. The second block of statements is executed if the
condition 2 is true
 The block of statements after the last else is executed if all conditions are false

Copyright © IT Series
if-else-if Programs

possible to perform mathematical operation on character values

Copyright
Copyright @©ITITSeries
Series www.itseries.com.pk
possible to perform mathematical operation on character values

Copyright © IT Series
possible to perform mathematical operation on character values

Copyright © IT Series
possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 An if statement within an if statement is called nested if statement
Syntax / General Form

possible to perform mathematical operation on character values

Working of Nested if
 the condition of outer if is evaluated first. If it is true, the control enters the inner if block
 If the condition is false, the inner if is skipped and control directly moves to the else part of outer if
 If outer if is true, the control enters the inner if statement
 The inner if evaluated according to simple if statement

Copyright © IT Series
Nested if Programs

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 Select one of several alternatives when selection is based on the value of a single variable or an expression
 In C, the value of this expression may be of type int or char
 The switch statement is a better way of writing a program when a series of if-else-if occurs
Syntax / General Form

Copyright @ IT Series www.itseries.com.pk


Rules of using switch case in C program
• The case label must integer or character
• Each case label must be unique
• A switch statement can have only one default label
• The default label can be used anywhere in switch statement
• The case label must end with colon. The default label is optional

Copyright @ IT Series www.itseries.com.pk


switch Programs

possible to perform mathematical operation on character values

Copyright
Copyright @©ITITSeries
Series www.itseries.com.pk
possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 Conditional operator is a decision-making or selection control structure
 It can be used in place of simple if-else structure
 It is also called ternary operator as it uses three operands
Syntax
(condition) ? true : false;
condition The condition is specified as relational or logical expression
true It is executed if expression evaluates to true
false It is executed if expression evaluates to false
Example
X = (A>50) ? 1 : 0;
The above statement can be written using if-else statement:
if (A>50)
X = 1;
else
X = 0;

Copyright @ IT Series www.itseries.com.pk


Conditional Operator Programs

possible to perform mathematical operation on character values

Copyright
Copyright @©ITITSeries
Series www.itseries.com.pk

You might also like