0% found this document useful (0 votes)
2 views23 pages

05 - Java Control Flow - Selection

The document provides an overview of Java control flow structures, focusing on selection statements such as if, if-else, if-else-if, and switch statements. It explains how these structures allow programmers to control the execution flow based on specified conditions. Additionally, it outlines the syntax and functionality of each selection statement, highlighting their differences and use cases.
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)
2 views23 pages

05 - Java Control Flow - Selection

The document provides an overview of Java control flow structures, focusing on selection statements such as if, if-else, if-else-if, and switch statements. It explains how these structures allow programmers to control the execution flow based on specified conditions. Additionally, it outlines the syntax and functionality of each selection statement, highlighting their differences and use cases.
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

Advance Java Programming

05 – Java Control Flow - Selection

Ghalib University
Computer Science Department

Asrar Ahmad Ehsan


Fall 2017
Agenda
• Control Flow Structure
• Sequencing
• Selection or Decision Making
• if-else-if Statement
• Switch Statement
Control Flow Structure

• Every computer program is a number of instructions to the computer.


• These instructions tell the computer what to do.
• The order in which the instructions are executed are called Flow.
• In general, the instructions are just executed one by one
• The instruction which stands first in the source code is executed first.
• These instructions are not always executed in the same order they are
written in the source code.
• The programmer can change this. If the order of execution is changed, it is
called Flow Control.
Control Flow Structure

 All programming logic can be broken down into one of the four control
structures:
o Sequencing
o Selection or Decision Making
o Repetition
o Branching
Sequencing
 The most basic control structure is sequence.
 In this case, the actions are performed in the sequence until all actions
have successfully completed.
 A sequence in Java is basically a list of commands that computer
executes in order from beginning to end.
Sequencing

1
2

3
4
Selection or Decision Making

 Java provides selection statements that allow the program to choose


between alternative actions during execution.
 The choice is based on criteria specified in the selection statement.
 These selection statement are:
1) Simple if statement
2) if-else statement
3) if-else-if statement
4) switch statement
Selection (Simple if Statement)

 The if statement is a fundamental control statement that allows Java


to make decisions or, more precisely, to execute statements
conditionally.
 The if statement has an associated expression and statement. If the
expression evaluates to true, the interpreter executes the statement.
If the expression evaluates to false, the interpreter skips the
statement.
 The parentheses around the expression are a required part of the
syntax for the if statement.
Selection (Simple if Statement)

 The condition is specified by conditional expression and the action to


be performed is specified by statement .
1) The conditional expression is evaluated first.
2) If its value is true, then statement (called the if block) is
executed and execution continues with the rest of the program.
3) If the value is false, then the if block is skipped and execution
continues with the rest of the program.
Selection (Simple if Statement)
Syntax:
if (condition expression){ true
statement(s) Condition
}

Example:
false Statement(s)

Rest of the code


Selection (if-else Statement)

 It is used to decide between two actions, based on a condition.


 The if-else selection structure performs different action when the condition is
true than when the condition is false.

1) The conditional expression is evaluated first.


2) If its value is true, then statement1 (the if block) is executed and
execution continues with the rest of the program.
3) If the value is false, then statement2 (the else block) is executed and
execution continues with the rest of the program.
Selection (if-else Statement)
Syntax:
if (condition expression){ false true
Condition
statement(s)
}else{
statement(s)
} Statement(s) Statement(s)

Example:

Rest of the code


Selection (if-else-if Statement)
 The statement in the else-clause of an if-else block can be another if-else
structures.
 This cascading of structures allows us to make more complex selections.

1) The conditional expression is evaluated first.


2) If its value is true, then statement1 (the if block) is executed and execution
continues with the rest of the program.
3) If the value is false, then (the else if block) conditional expression is
evaluated.
4) If its value is true, then statement2 (the else if block) is executed and
execution continues with the rest of the program.
5) If the value is false, then statement3 (the else block) is executed and
execution continues with the rest of the program.
Selection (if-else-if Statement)

true Condition false


1

Statement(s) false Condition true


2

Statement(s) Statement(s)

Rest of the code


Selection (if-else-if Statement)
Syntax:

if (condition expression){
statement(s)
}else if (condition expression){
statement(s)
}else{
statement(s)
}
Selection (if-else-if Statement)
Selection (switch Statement)
 It is used when one statement must be chosen from many.
 The switch statement transfers control to one of several statements depending
on the value of an expression.

1) Java first evaluates the switch-expression, and jumps to the case whose
selector matches the value of the expression.
2) The program executes the statements in order from that point on until a
break statement is encountered, skipping then to the first statement after
the end of the switch structure.
3) If none of the cases are satisfied, the default block is executed.

 Take note however, that the default part is optional.


Selection (switch Statement)
The switch statement and its case labels have some important restrictions.

1. The expression associated with a switch statement must have an appropriate type
either byte, char, short, int (or their wrappers), or an enum type or a String.
2. The floating-point and boolean types are not supported, and neither is long, even
though long is an integer type.
3. The value associated with each case label must be a constant value or a constant
expression the compiler can evaluate.
4. The case label values must be within the range of the data type used for the switch
expression.
5. It is not legal to have two or more case labels with the same value or more than
one default label.
Selection (switch Statement)
Case true
Block 1 statements break
Selector 1

false

Case true
Block 2 statements break
Selector 2

false

Case true
Block 3 statements break
Selector 3

false
Default block statements
Selection (switch Statement)
Syntax:
Selection (switch Statement)
Example:
Switch VS . if-else
Basis For IF-ELSE SWITCH
Comparison
Expression if-else statement uses multiple statement switch statement uses single
for multiple choices. expression for multiple choices.

Testing if-else statement test for equality as well switch statement test only for
as for logical expression. equality.

Evaluation if statement evaluates integer, character, switch statement evaluates only


floating-point type or boolean type. character, String or integer value.
Any Questions?

You might also like