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

Selection Statements Loops: OOP Using Java Mulugeta M

The document discusses selection statements and loops in Java. It describes the different types of selection statements including if-else statements and switch statements. It also covers the different types of loops in Java - while loops, do-while loops, and for loops. The key aspects of each type of loop like syntax, flow, and examples are explained.

Uploaded by

Kene Birhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Selection Statements Loops: OOP Using Java Mulugeta M

The document discusses selection statements and loops in Java. It describes the different types of selection statements including if-else statements and switch statements. It also covers the different types of loops in Java - while loops, do-while loops, and for loops. The key aspects of each type of loop like syntax, flow, and examples are explained.

Uploaded by

Kene Birhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Selection Statements

Loops

OOP using java Mulugeta


M
Selection Statements

OOP using java Mulugeta


M
Selection Statements
• Often in a program
– you need to compare two values, such as whether i
is greater than j.
– Java provides six comparison operators (also
known as relational operators)
• used to compare two values.
• The result of the comparison is a Boolean value: true or
false.
– Example
System.out.println(1 < 2);

OOP using java Mulugeta


M
The six comparison operators

OOP using java Mulugeta


M
…comparison…
• You can also compare characters.
• Comparing characters is the same as comparing
the Unicodes of the characters.
• For example, 'a' is larger than 'A' because the
Unicode of 'a' is larger than the Unicode of 'A.‘
• Caution!
– The equality comparison operator is two equal signs
(==), not a single equal sign (=).
• The latter symbol is for assignment.

OOP using java Mulugeta


M
Boolean Variable
• A variable that holds a Boolean value is
known as a Boolean variable.
• The boolean data type is used to declare
Boolean variables.
• The domain of the boolean type consists of
two literal values: true and false.
• For example, the following statement assigns
true to the variable lightsOn:
boolean lightsOn = true;
OOP using java Mulugeta
M
Boolean Operators
• Boolean operators, also known as logical
operators, operate on Boolean values to
create a new Boolean value

OOP using java Mulugeta


M
Boolean Operators…
• not (!) operator
– The not (!) operator negates true to false and false to true.
• and (&&) operator
– The and (&&) of two Boolean operands is true if and only
if both operands are true.
• or (||) operator
– The or (||) of two Boolean operands is true if at least one of
the operands is true
• exclusive or (^) operator
– The exclusive or (^) of two Boolean operands is true if and
only if the two operands have different Boolean values.
• Consider the program that checks whether a number is divisible by 2 and
3, whether a number is divisible by 2 or 3, and whether a number is
divisible by 2 or 3 but not both

OOP using java Mulugeta


M
Selection Statements
• Java has several types of selection
statements:
– simple if statements
– if ... else statements
– nested if statements
– switch statements, and

OOP using java Mulugeta


M
Simple if Statements
• A simple if statement executes an action if and only if the
condition is true.
• The syntax for a simple if statement is shown below:
if (booleanExpression)
{
statement(s);
}
Example

OOP using java Mulugeta


M
if ... else Statements
• A simple if statement takes an action if the
specified condition is true.
• If the condition is false, nothing is done.
• But what if you want to take alternative
actions when the condition is false?
• You can use an if ... else statement.
• The actions that an if ... else statement
specifies differ based on whether the condition
is true or false.
OOP using java Mulugeta
M
if ... else Statements…

OOP using java Mulugeta


M
if ... else Example

• If radius >= 0 is true, area is computed and


displayed
• if it is false, the message "Negative input" is
printed.
OOP using java Mulugeta
M
If…else if…else Statement
• Syntax
Example
if (booleanExpression)
{
statement(s)-for-the-true-case;
}
else if (booleanExpression)
{
statement(s)-for-the-true-case;
}

else
{
statement(s)-for-the-false-case;
}

OOP using java Mulugeta


M
switch Statements
• Java provides a switch statement to handle multiple
conditions efficiently.
• the full syntax for the switch statement:

• The switch statement observes the following rules:


OOP using java Mulugeta
M
switch…
• The switch-expression must yield a value of char,
byte, short, or int type and must always be enclosed
in parentheses.
• The value1, . . ., and valueN must have the same data
type as the value of the switch-expression.
– Note that value1, . . ., and valueN are constant expressions,
meaning that they cannot contain variables in the
expression, such as 1 + x.
• When the value in a case statement matches the value
of the switch-expression, the statements starting from
this case are executed until either a break statement
or the end of the switch statement is reached.

OOP using java Mulugeta


M
switch…
• The keyword break is optional.
– The break statement immediately ends the switch
statement.
• The default case, which is optional, can be used to
perform actions when none of the specified cases
matches the switch-expression.
• The case statements are checked in sequential order
• Caution !!
– Do not forget to use a break statement when one is needed.
– Once a case is matched, the statements starting from the
matched case are executed until a break statement or the
end of the switch statement is reached.
OOP using java Mulugeta
M
Loops

OOP using java Mulugeta


M
Loops
• Java provides a powerful control structure called a
loop that controls how many times an operation or a
sequence of operations is performed in succession
• Loops are structures that control repeated executions
of a block of statements.
• The concept of looping is fundamental to
programming.
• Java provides three types of loop statements:
– while loops
– do-while loops and
– for loops.

OOP using java Mulugeta


M
The while Loop
• The syntax for the while loop is as follows:
while (loop-continuation-condition)
{
// Loop body
Statement(s);
}

• The part of the loop that contains the statements to be repeated


is called the loop body.
• A one-time execution of a loop body is referred to as an
iteration of the loop.
• Each loop contains a loop-continuation condition, a Boolean
expression that controls the execution of the body.
• It is always evaluated before the loop body is executed.
– If its evaluation is true, the loop body is executed;
– if its evaluation is false, the entire loop terminates and the program
control turns to the statement that follows the while loop.

OOP using java Mulugeta


M
The while Loop -- Flowchart

OOP using java Mulugeta


M
The while Loop…
• Example
int count = 0;
while (count < 100)
{
System.out.println("Welcome to Java!");
count++;
}

Flowchart

OOP using java Mulugeta


M
The do-while Loop
• The do-while loop is a variation of the while
loop.
• Its syntax is given below:
do
{
// Loop body;
Statement(s);
}
while (loop-continuation-condition);
Flowchart

OOP using java Mulugeta


M
do-while Loop…
• The loop body is executed first.
• Then the loop-continuation-condition is evaluated.
• If the evaluation is true,
– the loop body is executed again;
• if it is false,
– the do-while loop terminates.
• The major difference between a while loop and a do-
while loop is the order in which the loop-
continuation-condition is evaluated and the loop body
executed.

OOP using java Mulugeta


M
do-while Loop…
• Example

int count = 0;
do
{
System.out.println("Welcome to Java!");
count++;
}
while (count < 100);

OOP using java Mulugeta


M
The for Loop
• Often you write a loop in the following common form:

OOP using java Mulugeta


M
The for Loop-- example
• Example
for (int count = 0; count<100;count++)
{
System.out.println("Welcome to Java!");
}
• If the loop-continuation-condition in a for loop is omitted, it is implicitly
true.
– Thus the statement given below in (a), which is an infinite loop, is
correct.
– Nevertheless, it is better to use the equivalent loop in (b) to avoid
confusion:

OOP using java Mulugeta


M

You might also like