Transcript lecture

Conditional statements and
Boolean expressions
The if-statement in Java (1)
• The if-statement is a conditional statement
• The statement is executed only if a given condition is satisfied
• An if-statement makes sure that the correct
condition is satisfied before a certain action is performed
The if-statement in Java (2)
• Syntax of the if-statement:
• The keyword if announces (to the Java compiler) that
we started an if-statement
• A conditional clause ( CONDITION ) follows
the keyword if
• Following the condition clause, you can write
(only) one statement
The condition clause of the ifstatement (1)
• The condition clause of the if-statement is an expression
that evaluates to true or false
• Expressions that evaluates to true or false are known
in Computer Science as: Boolean expressions
• Example:
a<0
The condition clause of the ifstatement (2)
Multiple statements in the "then"part of the if-statement (1)
• Block = a pair of "{" and "}" braces that groups
components in a Java program together
Multiple statements in the "then"part of the if-statement (2)
• Statement block = multiple statements grouped together
by braces { .... }
• A statement block in Java is considered as one (single)
statement
Representation techniques:
Flow chart (1)
• A flow chart is a diagram that represents a computer
algorithm.
• It uses a rectangular box to represent an assignment
statement
• It uses a diamond box to represent a condition
• Lines are used to indicate the flow of the steps in the
algorithm
Representation techniques:
Flow chart (2)
Representation techniques:
Structure diagram(1)
• A structure diagram is another type of diagram to represent
a computer algorithm.
• It uses different shapes of boxes to represent assignment
statements and conditions
• The structure diagram does not use lines
Representation techniques:
Structure diagram(2)
The Boolean (logical) data
type boolean
• The boolean data type is a built-in (primitive) data
type of Java
• is used to represent the logical values
• There are 2 logical values: true and false
• Encoding scheme used in the boolean data type:
0 represents false
1 represents true
• uses 1 byte of memory (to store 0 or 1)
Boolean literals
• There are 2 boolean literals (= logical constants) in Java:
• These 2 words are keywords (reserved words) in Java
Defining boolean typed variables (1)
• Syntax to define an boolean typed variable:
boolean NameOfVariable ;
• The keyword boolean announces the variable definition
clause
• The NameOfVariable is an identifier which is the name of
the variable.
• The variable definition clause is must be ended with a semicolon ";“
• A boolean typed variable can store true (1) or false (0)
Defining boolean typed variables (2)
Operations that return
a boolean result
1. Compare operators:
• A compare operator compares 2 numerical
expressions and return a Boolean result.
2. Logical operators:
• A logical operator compares 2 Boolean (logical)
expressions and return a Boolean result
Compare operators (1)
• A compare operator will return the value true if the test is
successful
• A compare operator will return the value false if the test is
unsuccessful
• The same automatic conversion rules used for arithmetic
operators apply for compare operators
Compare operators (3)
• you can assume that all compare operators have the same
priority
Compare operators (4)
Logical operators (1)
Logical operators (2)
Logical operators (3)
Logical operators (4)
Programming trick: test if a number
is between 2 numbers (1)
The program prints "yes" when 10 ≤ a ≤ 20
It is illegal to use the comparison operator "<=" on
a Boolean value and a number
Programming trick: test if a number
is between 2 numbers (2)
Programming trick: test if a number
is between 2 numbers (3)