Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Object Oriented Programming

Lecture No: 3
Lecture Title: Control Statements
Instructor: Shahzad Khan
Conditional Statements
Conditional statements are used to make decisions.
 Conditional statements enables the programs to make
decisions about which sections of code to execute.
Control statements make their decisions based on a
relational expression that evaluates to the logical
values true or false.
In real life, we make all sorts of decisions based on
criteria such as "am I being offered enough money to
take this job?" If the answer is "yes," then the result is
"take the job." If the answer is "no," then "don't take the
job."
Control Statements
Decision Control Statements
It takes an expression in parenthesis and an statement
or block of statements. if the expression is true then the
statement or block of statements gets executed
otherwise these statements are skipped.
It requires a relational expression in parenthesis
Line(s) of code inside the block if the relational
expression is true.
Line(s) of code inside the block if the relational
expression is false.
Decision Control Statements
If Conditions
If/ Else Conditions
Else if ladder
Switch Statements
Decision Control Statements
If Condition
The if statement evaluates the test expression inside
the parenthesis.
If the test expression is evaluated to true (nonzero),
statements inside the body of if is executed.
If the test expression is evaluated to false (0),
statements inside the body of if is skipped from
execution.
If Condition Example
If Condition Example
If/Else Conditions
The if statement evaluates the test expression inside
the parenthesis.
If the test expression is evaluated to true (nonzero),
statements inside the body of if is executed.
If the test expression is evaluated to false (0),
statements inside the body of else is executed.
If/else Conditions
if (expression)
{
Block of statements;
}
else
{
Block of statements;
}
If/else condition block diagram
If / Else Condition Example
Else/If Ladder Example
Switch Statements
Switch Statement
Is a type of control statement.
Switch statements uses expression.
Unlike if/else it has pre defined options/cases are there to
select from on the basis of the switch expression.
When a case constant is found that matches the switch
expression, control of the program passes to the block of
code associated with that case.
Break is used to transfer the control to the main body of
the program.
Default case is used if a switch expression does not match
a case
Switch Block Diagram
Switch Rules
You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear
at the end of the switch. The default case can be used for performing a
task when none of the cases is true. No break is needed in the default
case.
Switch Statement Syntax
switch(expression)
{
case constant-expression :
statement(s);
break;
/* optional */
case constant-expression :
statement(s);
break;
/* optional */
/* you can have any number of case statements */
default :
/* Optional */
statement(s);
}
Switch Example

You might also like