0% found this document useful (0 votes)
6 views

Chapter-III-Conditions-Student-Copy

This document covers fundamental concepts in computer programming, focusing on decision-making through conditional statements in Java. It introduces the boolean data type, relational and logical operators, and explains the use of if-else statements and switch cases for controlling program flow. The chapter emphasizes the importance of evaluating conditions to determine the execution path of code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter-III-Conditions-Student-Copy

This document covers fundamental concepts in computer programming, focusing on decision-making through conditional statements in Java. It introduces the boolean data type, relational and logical operators, and explains the use of if-else statements and switch cases for controlling program flow. The chapter emphasizes the importance of evaluating conditions to determine the execution path of code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Computer

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.

Some of the notable situations include are whether the


student is passing or failing, numeric to non-numeric
grade classification, determining whether the student
is eligible to graduate with honors based form his/her
GPA and many more.

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.

Boolean expressions use relational operators to check


conditions and to compare variables against certain
values; thus, evaluating whether its true or false.

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

3) NEGATION -> It is true if its false


4) XOR ->It is true if both values are not
the same

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.

If a boolean expression evaluates to


true, the corresponding block is
executed, and subsequent if
conditions are not checked.

If no if condition is true, and anIIIelse


Chapter
block is present, the else block is
executed.
2
2
SWITCH, BREAK &
A switch statement is multi-branch
DEFAULT
statement, in which the execution
path is based on a value of a
variable or an expression.

Unlike the if statement, a switch


statement can have several
execution paths.

Chapter III
2
3
SWITCH, BREAK &
The switch statement allows a
DEFAULT
choice from a number of options.

It will test a number of cases


against a variable of type char,
byte, short, or int.

Chapter III
2
4
SWITCH, BREAK &
The break statement breaks out of
DEFAULT
the switch statement.

Without the break, the program


would continue to execute the
code block of the next case
statement.

A break statement should normally


be used in each case. Chapter III
2
5
SWITCH, BREAK &
The default option should be the
DEFAULT
last option in a switch statement.

If none of the cases match, the


default block is executed.

This course will not use the braces


in the switch statement, although
it is allowable.
Chapter III
2
6
SWITCH, BREAK &
How it works?

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

You might also like