0% found this document useful (0 votes)
83 views16 pages

Prog0101 CH06

This document discusses branching statements in programming. It covers the IF statement, which allows a program to execute different code based on a condition being true or false. The IF statement can take the form of a simple IF statement or an IF-ELSE statement. The document also discusses the SWITCH statement, which allows executing different code based on the value of an expression and matches it to case values. Examples are provided to illustrate IF and SWITCH statements.

Uploaded by

toshugo
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
0% found this document useful (0 votes)
83 views16 pages

Prog0101 CH06

This document discusses branching statements in programming. It covers the IF statement, which allows a program to execute different code based on a condition being true or false. The IF statement can take the form of a simple IF statement or an IF-ELSE statement. The document also discusses the SWITCH statement, which allows executing different code based on the value of an expression and matches it to case values. Examples are provided to illustrate IF and SWITCH statements.

Uploaded by

toshugo
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/ 16

PROG0101 Fundamentals of Programming

PROG0101
FUNDAMENTALS OF PROGRAMMING

Chapter 6
Branching

1
PROG0101 Fundamentals of Programming
Branching

Topics
• Branching Statement
• IF Statement
• Switch Statement

2
PROG0101 Fundamentals of Programming
Branching

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.

3
PROG0101 Fundamentals of Programming
Branching

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.

4
PROG0101 Fundamentals of Programming
Branching

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.

5
PROG0101 Fundamentals of Programming
Branching

IF Statement
• Two types of IF Statement:
– Simple IF Statement
– IF-ELSE Statement

6
PROG0101 Fundamentals of Programming
Branching

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.

7
PROG0101 Fundamentals of Programming
Branching

Simple IF Statement

8
PROG0101 Fundamentals of Programming
Branching

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.

9
PROG0101 Fundamentals of Programming
Branching

IF-ELSE Statement

10
PROG0101 Fundamentals of Programming
Branching

Example 1
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.

11
PROG0101 Fundamentals of Programming
Branching

Example 2
The flow chart shows a program to check whether the
student “Passed” or “Failed”.

12
PROG0101 Fundamentals of Programming
Branching

SWITCH Statement
• 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.

13
PROG0101 Fundamentals of Programming
Branching

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.

14
PROG0101 Fundamentals of Programming
Branching

Example 1

15
PROG0101 Fundamentals of Programming
Branching

Example 2

16

You might also like