Title: Java Selection Statements
1Java Selection Statements
2Summary
- Operators
- Algorithm Design Languages
- Selection Statements
- single selection statement
- double selection statement
- multiple selection statement
3Operators
- An expression consists of operand(s) and
operator(s) (also called arguments) that specify
the operation between the operands to execute the
expression - The process of applying the operators to the
operands is called expression evaluation, e.g.
for numeric expressions expression evaluation is
equivalent to calculating the result value of the
expression - An expression has a result value type which
depends on the operands types and operators that
take part in the expression
4Operators (2)
- Examples of numeric expressions in Java
- 43 7 3.5/7 5.68.9 ( 9-7 ) (
35-27 ) 9-735-27 - There are 3 types of operators depending on the
number of the operands - unary - one operator, one operand
- binary- one operator, two operands
- ternary- one operator, three operands
5Operators (2)
- Precedence is the order in which an expression is
evaluated - Some operators have precedence higher than
others e.g. you evaluate the multiplication first
and then the addition - You can use parentheses to change the order of an
expression evaluation - Associativity
- left to right or right to left
- Depending on the data type you can have different
operations - Examples
- Numeric data types - / etc.
- Boolean data types !
6Operators Types
- Depending on the operands types and the operator
between them there are 4 operators types - Arithmetic Operators
- Relational Operators
- Logical Operators
- Conditional Operator
7Relational Operators
8Relational Operators - Examples
-
- int i1 15 // first operand
- int i2 6 // second operand
- boolean result // the result
-
- result i1 i2
- result i1 ! i2
- result i1 lt i2
- result i1 lt i2
- result i1 gt i2
- result i1 gt i2
-
-
9Relational Operators - Examples
-
- int i1 15 // first operand
- int i2 6 // second operand
- boolean result // the result
-
- result i1 i2 // result false
- result i1 ! i2 // result true
- result i1 lt i2 // result false
- result i1 lt i2 // result false
- result i1 gt i2 // result true
- result i1 gt i2 // result true
-
-
10Relational Operators Precedence
11Characters
- A char variable stores a single character from
the Unicode character set - A character set is an ordered list of characters,
and each character corresponds to a unique number - Unicode is an international character set,
containing symbols and characters from many world
languages - The Unicode character set uses 16 bits per
character, allowing for 65,536 unique characters - Character literals are enclosed in single quotes
- A' c' 3' '' .' '\n
- For more information go to http//unicode.org
12Characters (2)
- You might have heard about the ASCII character
set - ASCII is older and smaller than Unicode, but is
still quite popular - The ASCII characters are a subset of the Unicode
character set, including - uppercase letters A, B, C,
- lowercase letters a, b, c,
- digits 0, 1, 2,
- special symbols , , , ?,
- control characters backspace \b
- (escape sequences) new line \n,
- For more information go to http//www.asciitable.c
om/
13Escape Sequences in Java
14Booleans
- Represent two values switched on, switched off
- Two reserved words for boolean values true and
false
15Logical Operators
- Logical Operators - a Table
- Truth Tables for Logical Operators
- Arithmetic, Relational Logical Operators
Precedence - Logical Operators - an Example
16Logical Operators a Table
17Truth Table for Logical Operators
18Truth Tables for Logical Operators
19Relational Logical Operators Precedence
20Logical Operators - an Example
int i1 20, i2 7, i3 7
boolean result result !(i1 i2)
result i1 ! i2 // same as
!(i1 i2) result i1 i3
result i1 gt i2 i1 i3
result i1 gt i2 i1 i3
21Logical Operators - an Example
int i1 20, i2 7, i3 7 //
operands boolean result result
!(i1 i2) // result true
result i1 ! i2 // result true
result i1 i3 // result
false result i1 gt i2 i1 i3 //
result false result i1 gt i2 i1 i3
// result true
22Conditional Operator
- Ternary if-else operator
-
false - booleanExpr ? expr1 expr2
- Example true
-
- int i 7
- int result
- result i lt 10 ? i 100 i 10
- // result i 100 700
-
23Algorithm
- An algorithm is a set of steps (rules) for
solving a problem in terms of - actions to be executed
- the order of actions execution
- We are using programming languages to specify the
steps in an algorithm - actions are called statements
- the order of statement execution is called
program control
24Algorithm Design Methods
- Algorithm design - special languages to help the
programmer develop algorithms - The program performing the algorithm steps can
then be written in any programming language - Algorithm Design Languages
25Algorithm Design Languages
- Program Design Language (PDL) Structured
English Pseudocode - artificial and informal language that helps
programmers develop algorithms - Flowcharts
- graphical language for representation of
algorithms
26PDL Notation
27Flowcharts
28Flow of Control
- The order of statement execution in a program or
method is called the flow of control - Unless specified otherwise, the order of
statement execution is linear one statement
after the other in sequence - Some programming statements modify that order,
allowing us to - decide whether or not to execute a particular
statement, or - repeat a statement execution over and over
- These decisions are based on conditions in the
program that evaluates to true or false
29Selection Statements
- A selection statement lets us choose which
statement will be executed next - Selection statements are sometimes called
conditional statements - Selection statements give us the power to make
decisions and branch the program execution - Java's selection statements are
- the if statement
- the if-else statement
- the switch statement
30Selection Statements
- Single Selection
- the if statement
- Double Selection
- the if/else statement
- Multiple Selection
- the switch statement
31Single Selection Statement
- if ( booleanExpr ) statement
- Example
- It is raining outside. Take your umbrella.
- Flowchart
32The if Statement Execution
- First the condition (represented by booleanExpr)
is evaluated. - If it is true, the statement is executed.
- If the condition is false - the next statement
after the if is executed.
33The if statement (3) - Examples
- if ( countNumbers 10 ) System.out.println( )
- if ( grade gt 90 )
- System.out.println( You are an excellent
student! ) - if ( account lt creditLimit )
- System.out.pritnln( You have insufficient
credit! ) - if ( dayOfTheWeeek gt 5 )
-
- System.out.println( Time to relax )
- System.out.println( Weekend.)
34Double Selection Statement
- if( booleanExpr )statement1 else statement2
- Example
- If you are hungry - go for lunch, otherwise take
a cup of coffee. - Flowchart
35The if/else Statement Execution
- First the condition (represented by booleanExpr)
is evaluated. - If it is true, the statement1 is executed.
- If the condition is false - the statement2
(after else) is executed. - The next statement after the if is executed.
36The if/else Statement Examples
- if ( dayOfTheWeek gt 5 )
-
- System.out.println( Weekend.)
- System.out.println( Time to relax )
-
- else
-
- System.out.println( Workday.)
- System.out.println( Go to work! )
-
-
37Embedded if/else Statement
- char grade A
- if ( grade A)
- System.out.println( Excellent student)
- else
- if ( grade B )
- System.out.println( Good student)
- else
- if ( grade C )
- System.out.println( Average student)
- else
- if ( grade D )
- System.out.println( Bad student)
- else System.out.println(Failed student)
38Multiple Selection Statement
- switch ( Expression )
-
- case value1 statement1 break
- case value2 statement2 break
-
- case valueN statementN break
- default statementD break
-
39Multiple Selection Statement Flowchart
40The switch Statement Execution
- First the expression is evaluated.
- The value of the expression is compared with the
value in the first case (value1). If they are
equal, the statement1 is executed. - If they are not equal than the value of the
expression is compared with the value in the
second case (value2). If they are equal, the
statement2 is executed and so on. - If the value of the expression is not equal to
any of the values (in cases) the statementD (the
default case) is executed. - The next statement after the switch is executed.
41The switch Statement Example
- switch ( grade )
-
- case A System.out.println( Excellent
student) - break
- case B System.out.println( Good student)
- break
- case C System.out.println( Average
student) - break
- case D System.out.println( Bad student)
- break
- default System.out.println(Failed
student) - break
-