Lect Six
Lect Six
OF PROGRAMMING
BRANCHING and LOOPS
CONTENTS
§ Branching Statement
§ IF Statement
§ Switch Statement
qLoops
qCondition Tested Loops
qCounted Loops
q Endless Loops
q FOR Loop
q WHILE Loop
q DO-WHILE Loop
BRANCHING STATEMENT
§A program consists of a number of statements which are usually
executed in sequence.
§Programs can be much more powerful if we can control the order
in which statements are run.
§The Branching Statements or Control Statements let the program
makes a decision about what to do next.
IF STATEMENT
§ The IF Statement is a powerful decision making statement
and is used to control the flow of execution of statements.
§ It is basically a two-way decision statement and is used in
conjunction with an expression.
IF STATEMENT
§ It allows the computer to evaluate the expression first and then,
depending on whether the value of the expression (relation or
condition) is ' true ' (non-zero) or ' false ' (zero), it transfers the
control to a particular statement.
§ This point of program has two paths to follow, one for the true
condition and the other for the false condition.
§ The if statement may be implemented in different forms depending
on the complexity of the conditions to be tested.
IF STATEMENT
§IF Statement
§Two types of IF Statement:
–Simple IF Statement
–IF-ELSE Statement
IF STATEMENT
§Simple IF Statement
üThe statement-block may be a single statement or a group of
statements.
ü If the test expression is true, the statement-block will be
executed; otherwise the statement-block will be skipped and the
execution will jump to statement-x.
ü Remember, when the condition is true both the statement-
block and the statement-x are executed in sequence.
IF STATEMENT
§Simple IF Statement
IF STATEMENT
§IF-ELSE Statement
ü The if....else statement is an extension of the simple if
statement.
ü If the test expression is true , then the true-block statement(s),
immediately following the if statement are executed; otherwise
the false-block statement(s) are executed.
ü In either case, either true-block or false-block will be executed,
not both.
IF STATEMENT
§IF-ELSE Statement
IF STATEMENT (EXAMPLE 1)
§Draw the flow chart shows a program which ask a user to enter a
number, and then compare the number given either it is bigger or
smaller than 5.
IF STATEMENT (SOLUTION FOR EXAMPLE
1)
IF STATEMENT (SOLUTION FOR EXAMPLE
2)
§Draw the flow chart shows a program to check whether
the student “
Passed”or “Failed”.
IF STATEMENT (SOLUTION FOR EXAMPLE
2)
SWITCHSTATEMENT
§ In computer programming, a Switch Statement (also known as
Case Statement) is a type of control statement that exists in most
modern imperative programming languages.
§This is another form of the multi way decision.
§ A switch statement is a selection statement that lets you transfer
control to different statements within the switch body depending on
the value of the switch expression.
SWITCHSTATEMENT
§ SWITCH Statement
§If the value of the switch expression equals the value of one of the
case expressions, the statements following that case expression are
processed.
§ If not, the default label statements, if any, are processed.
SWITCHSTATEMENT
§s w i t c h structure: alternate to if-else
§s w i t c h (integral) expression is evaluated first
§Value of the expression determines which corresponding action is
taken
§Expression is sometimes called the selector
SWITCHSTATEMENT (SYNTAX)
REPETITION (LOOPS)
Everyday Life: Every second, we are engaged in one or more control structures.
EVERYDAY LIFE: EVERYSECOND, WE ARE ENGAGED IN ONE OR MORE CONTROL
STRUCTURES.
I smile
WHILE I I sleep I rest WHILE
am happy. WHILE I I am sick.
am tired.
I yell
WHILE I
am mad.
WHYIS REPETITION NEEDED?
§ Repetition allows you to efficiently use variables
§ Can input, add, and average multiple numbers using a limited number of variables
§ For example, to add five numbers:
§ Declare a variable for each number, input the numbers and add the
variables together
§ Create a loop that reads a number into a variable and adds it to a variable
that contains the sum of the numbers
LOOP (REPETITION)
§ A loop is a sequence of instructions that is continually repeated
until a certain condition is reached.
§Loops allow for the same statement to be executed a number of
times in succession.
TYPES OF LOOPS
§FOR Loop
§WHILE Loop
§DO …WHILE Loop
FOR LOOP
§A FOR loop is a loop that repeats a specified number of times.
§ The loop uses a counter to tell it how many times to run the same
sequence of activities.
§The counter has the following three numeric values:
–Initial counter value
–Condition
–Increment/ Decrement (the amount to add to the counter each
time the loop runs)
FOR LOOP
§ The loop ends when the counter reaches the condition, or,
if there is an associated test condition, when the test
condition is true.
FOR LOOP (SYNTAX)
§ FOR (initial counter value, condition, increment/ decrement)
Statement (Do Something)
FOR LOOP (PSEUDOCODE)
FOR x times
Do Something
Increment/ Decrement
FOR LOOP (FLOW CHART)
FOR LOOP (EXAMPLE 1)
for (x=1, x<5, x++)
PRINT “ Hello World”
FOR LOOP (EXAMPLE 2)
for (x=1, x<=4, x++)
PRINT x
FOR LOOP (EXAMPLE 3)
for (x=5, x>0, x--)
PRINT x
WHILE LOOP
§ A WHILE loop is a loop that repeats while some condition is
satisfied.
§ The WHILE loop tests its condition at the beginning of every loop.
§ If the condition is false from the start, the sequence of activities
contained in the loop never runs at all.
WHILE LOOP SYNTAX
WHILE (Condition)
Statement (Do Something)
WHILE LOOP (PSEUDOCODE)
WHILE condition
Do Something
WHILE LOOP (FLOW CHART)
WHILE LOOP (EXAMPLE 1)
WHILE (x < 5)
PRINT “Hello World”
x++
WHILE LOOP (EXAMPLE 2)
WHILE (key != Esc)
PRINT “
Hello World”
DO-WHILE LOOP
§Like a while loop, a do-while loop is a loop that repeats while some
condition is satisfied.
§ Unlike a while loop, a do-while loop tests its condition at the end of
the loop.
§ This means that its sequence of activities always runs at least once.
DO-WHILE LOOP (SYNTAX)
DO
Statement
WHILE (Condition)
DO-WHILE LOOP (PSEUDOCODE)
Do
something
WHILE condition
DO-WHILE LOOP (FLOW CHART)
DO-WHILE (EXAMPLE 1)
x=1
DO
PRINT “
Hello World”
x++
WHILE (x<5)
DO-WHILE (EXAMPLE 2)
x=1
DO
PRINT “
Hello World”
x++
WHILE (x>5)
ASSIGNMENT
Draw flowchart diagram for the following programs using loop:
1. A program that display number 1to 20
2. A program that display a person name 5 times.
END