Chapter-III-Conditions-Student-Copy
Chapter-III-Conditions-Student-Copy
Fundamentals and
Programming
CONDITIONS
Chapter III
1
TABLE OF
1) Overview
CONTENTS
2) Relational Operators, Simple Boolean
Expressions and the Boolean Data Type
3) Logical Operators and Compound
Boolean Expressions
4) If else, else-if, and Multi-if
5) Switch cases
Computer
Fundamentals and
Programming
2
OVERVIEW
In reality, we often encounter situations where we
need to make decisions based on certain conditions.
Chapter III
3
OVERVIEW
In this chapter we introduce the boolean data type, a
primitive type that can hold true or false. We discuss
relational and logical operators and build an
understanding of boolean expressions (conditions),
and introduce conditional statements in Java (if, switch
and the ternary expression).
Chapter III
4
RELATIONAL
OPERATORS
Relational operators are used to check conditions.
They can be used to check whether two values are
equal, not equal, greater than or less than.
Chapter III
5
RELATIONAL
OPERATORS
Differentiating declaration and initialization:
Initialization
boolean x;
x = true;
Declaration
boolean x = true;
Chapter III
6
RELATIONAL
OPERATORS
Chapter III
7
LOGICAL
OPERATORS
Logical operators allow us
to combine two more
conditions.
A compound Boolean
expression consists of two
or more Boolean
expressions joined with
logical operators.
Chapter III
8
LOGICAL
OPERATORS
1) AND -> It is true when both are
2) OR -> It is true when one is true
true
Chapter III
9
LOGICAL
OPERATORS
Chapter III
1
0
LOGICAL
OPERATORS
Chapter III
1
1
LOGICAL
OPERATORS
Chapter III
1
2
LOGICAL
OPERATORS
Chapter III
1
3
LOGICAL
OPERATORS
Chapter III
1
4
LOGICAL
OPERATORS
Chapter III
1
5
LOGICAL
OPERATORS
AND -> It is true when both are true
OR -> It is true when one is true
NEGATION -> It is true if its false
XOR ->It is true if both values are not the
same
Chapter III
1
6
LOGICAL
OPERATORS
AND -> It is true when both are true
OR -> It is true when one is true
NEGATION -> It is true if its false
XOR ->It is true if both values are not the
same
Chapter III
1
7
COMPOUND
EXPRESSIONS
Chapter III
1
9
IF, IF ELSE, MULTI-IF
If statement: The
statements inside the if
block are executed only
if the Boolean expression
evaluates to true,
otherwise the entire
block will be skipped.
Chapter III
2
0
IF, IF-ELSE, MULTI-IF
If-else statement: The statements inside the if block
are executed only if the Boolean expression evaluates
to true, otherwise the statements in the else block
will be executed.
Chapter III
2
1
IF, IF ELSE, MULTI-IF
Multi-if statement: The statements
inside each if block are evaluated
sequentially.
Chapter III
2
3
SWITCH, BREAK &
The switch statement allows a
DEFAULT
choice from a number of options.
Chapter III
2
4
SWITCH, BREAK &
The break statement breaks out of
DEFAULT
the switch statement.
DEFAULT
• The switch expression is evaluated only once.
• The expression value is compared to the value of each case,
if there is a match the code under that case is executed until
a break statement is reached, or the end of switch block is
reached. If there is no match the block under default case is
executed if a default label exists. Notice the default case is
the last case in a switch statement.
Note: If the break is not present, the next case will also be
executed. This allows several case blocks to execute the
Chapter III
same code.
Computer
Fundamentals and
Programming
THANK YOU