0% found this document useful (0 votes)
2 views59 pages

ch 3java

Chapter 3 discusses various types of statements in Java, including declaration, expression, and control flow statements. It elaborates on decision-making statements like if and switch statements, as well as loop control structures such as while, do...while, and for loops. The chapter also covers arrays and ArrayLists, highlighting their characteristics, advantages, and common methods.

Uploaded by

Ketema Deba
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)
2 views59 pages

ch 3java

Chapter 3 discusses various types of statements in Java, including declaration, expression, and control flow statements. It elaborates on decision-making statements like if and switch statements, as well as loop control structures such as while, do...while, and for loops. The chapter also covers arrays and ArrayLists, highlighting their characteristics, advantages, and common methods.

Uploaded by

Ketema Deba
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/ 59

Chapter 3: Decision and

Repetition Statements

Daniel Tesfay (MSc)


Master of Engineer in ICT Convergence

OOP Prepared By: Daniel Tesfy 1


Overview of Java statements

– Types of Statements in Java


– A statement specifies an action in a Java program, such
as assigning the sum of x and y to z, printing a message
to the standard output, writing data to a file, etc.
– Statements in Java can be broadly classified in to three
categories:
1. Declaration Statement
2. Expression Statement
3. Control flow Statement
OOP Prepared By: Daniel Tesfy 2
Declaration Statement

• A declaration statement is used to declare a variable.


For example
– int num;
– int num2=100;
– String str;
– float area;

OOP Prepared By: Daniel Tesfy 3


Expression Statement

• An expression with a semicolon at the end is called


an expression statement. For example,
– num++; // increment expression
– num=100; // assignment expression
– someMethod(par1, par2); // Method invocation
expressions

OOP Prepared By: Daniel Tesfy 4


Control Flow Statement

– By default, all statements in a Java program are


executed in the order they appear in the program.
– Sometimes you may want to execute a set of
statements repeatedly for a number of times or as
long as a particular condition is true.
– All of these are possible in Java using control flow
statements. If block, while loop, and for loop
statements are examples of control flow statements.
OOP Prepared By: Daniel Tesfy 5
Decision Making Statements

• There are two types of decision making statements in


Java. They are:
– If statements
– Switch statements
• The IF Statement
– An if statement consists of a Boolean expression
followed by one or more statements.
OOP Prepared By: Daniel Tesfy 6
Syntax:
if(Boolean_expression)
{
// Statements will
execute if the
Boolean expression is
true
}
OOP Prepared By: Daniel Tesfy 7
If statement example

OOP Prepared By: Daniel Tesfy 8


The if----else Statement

– An if statement can be followed by an optional


else statement , which executes when the
Boolean expression is false.
– Syntax

OOP Prepared By: Daniel Tesfy 9


OOP Prepared By: Daniel Tesfy 10
If----else example

OOP Prepared By: Daniel Tesfy 11


The if...else if...else Statement

• An if statement can be followed by an optional else if...else statement,


which is very useful to test various conditions using single if...else if
statement.
• When using if , else if , else statements there are few points to keep in
mind.
– An if can have zero or one else's and it must come after any else if’s.
– An if can have zero to many else if's and they must come before the
else.
– Once an else if succeeds, none of the remaining else if's or else's will
be tested.
OOP Prepared By: Daniel Tesfy 12
• Syntax :

OOP Prepared By: Daniel Tesfy 13


OOP Prepared By: Daniel Tesfy 14
if ---else if----else example

OOP Prepared By: Daniel Tesfy 15


Nested if...else Statement:

– It is always legal to nest if-else statements which means


you can use one if or else if statement inside another if or
else if statement.
– Syntax

OOP Prepared By: Daniel Tesfy 16


OOP Prepared By: Daniel Tesfy 17
Nested If statement example

OOP Prepared By: Daniel Tesfy 18


The switch Statement

– A switch statement allows a variable to be tested for


equality against a list of values.
– Each value is called a case, and the variable being switched
on is checked for each case.
– Syntax:

OOP Prepared By: Daniel Tesfy 19


• The following rules apply to a switch statement:
– The variable used in a switch statement can only be a byte, short, int, or char.
– You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
– The value for a case must be the same data type as the variable in the switch
and it must be a constant or a literal.
– When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
– When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
OOP Prepared By: Daniel Tesfy 20
OOP Prepared By: Daniel Tesfy 21
Switch Statement Demo

• It takes an integer user


input
• It returns the string
equivalent of the given
numeric month

OOP Prepared By: Daniel Tesfy 22


– Not every case needs to contain a break. If no break
appears, the flow of control will fall through to
subsequent cases until a break is reached.
– A switch statement can have an optional default case,
which must appear at the end of the switch.
– The default case can be used for performing a task when
none of the cases is true.
– No break is needed in the default case
OOP Prepared By: Daniel Tesfy 23
Loop Control

– There may be a situation when we need to execute


a block of code several number of times, and is
often referred to as a loop.
– Java has very flexible three looping mechanisms.
You can use one of the following three loops:
1. while Loop
2. do...while Loop
3. for Loop
OOP Prepared By: Daniel Tesfy 24
The while Loop
– A while loop is a control structure that allows you to repeat a task a
certain number of times.
– Syntax :

– When executing, if the boolean_expression result is true, then the


actions inside the loop will be executed.
– This will continue as long as the expression result is true.

OOP Prepared By: Daniel Tesfy 25


OOP Prepared By: Daniel Tesfy 26
– key point of the while loop is that the loop might
not ever run.
– When the expression is tested and the result is
false, the loop body will be skipped and the first
statement after the while loop will be executed.

OOP Prepared By: Daniel Tesfy 27


While loop example

OOP Prepared By: Daniel Tesfy 28


The do...while Loop

• A do...while loop is similar to a while loop, except that a


do...while loop is guaranteed to execute at least one time.
• Syntax:

• Notice that the Boolean expression appears at the end of


the loop, so the statements in the loop execute once before
the Boolean is tested.
OOP Prepared By: Daniel Tesfy 29
OOP Prepared By: Daniel Tesfy 30
• If the Boolean expression is true, the flow of control
jumps back up to do, and the statements in the loop
execute again. This process repeats until the Boolean
expression is false.

OOP Prepared By: Daniel Tesfy 31


The for Loop
– A for loop is a repetition control structure that allows you to efficiently write a
loop that needs to execute a specific number of times.
– A for loop is useful when you know how many times a task is to be repeated.
– Syntax

OOP Prepared By: Daniel Tesfy 32


OOP Prepared By: Daniel Tesfy 33
• flow of control in a for loop
 The initialization step is executed first, and only once.
 Next, the Boolean expression is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and flow of control
jumps to the next statement past the for loop.
 After the body of the for loop executes, the flow of control jumps back up to the
update statement.
 This statement allows you to update any loop control variables.
 The Boolean expression is now evaluated again. If it is true, the loop executes and
the process repeats itself (body of loop, then update step, then Boolean
expression). After the Boolean expression is false, the for loop terminates.
OOP Prepared By: Daniel Tesfy 34
For loop example

OOP Prepared By: Daniel Tesfy 35


Enhanced for loop in Java

– As of Java 5, the enhanced for loop was introduced. This is mainly used for Arrays
– Syntax

– Declaration: The newly declared block variable, which is of a type compatible with
the elements of the array you are accessing. The variable will be available within the
for block and its value would be the same as the current array element.
– Expression: This evaluates to the array you need to loop through. The expression
can be an array variable or method call that returns an array.
OOP Prepared By: Daniel Tesfy 36
Enhanced For Loop example

OOP Prepared By: Daniel Tesfy 37


The break Keyword

– The break keyword is used to stop the entire loop.


– The break keyword must be used inside any loop
or a switch statement.
– The break keyword will stop the execution of the
innermost loop and start executing the next line of
code after the block.
– Syntax:
OOP Prepared By: Daniel Tesfy 38
OOP Prepared By: Daniel Tesfy 39
Break Test

OOP Prepared By: Daniel Tesfy 40


The continue Keyword

• The continue keyword can be used in any of the loop


control structures. It causes the loop to immediately
jump to the next iteration of the loop.
 In a for loop, the continue keyword causes flow of control to immediately jump to
the update statement.
 In a while loop or do/while loop, flow of control immediately jumps to the Boolean
expression.
 Syntax
OOP Prepared By: Daniel Tesfy 41
OOP Prepared By: Daniel Tesfy 42
Continue

OOP Prepared By: Daniel Tesfy 43


Java array

Array and Array List

OOP Prepared By: Daniel Tesfy 44


Java Array
– An array is a collection of similar type of elements that have a
contiguous memory location.
– Java array is an object which contains elements of a similar data type .
It is a data structure where we store similar elements .
– We can store only a fixed set of elements in a Java array.
– Array in java is index-based

OOP Prepared By: Daniel Tesfy 45


• Advantages
– Code Optimization: It makes the code optimized, we
can retrieve or sort the data efficiently.
– Random access: we can get any data located at an
index position.
• Disadvantage
– Size Limit: we can store a fixed size of elements in an
array. It doesn’t grow its size at runtime.
OOP Prepared By: Daniel Tesfy 46
Types of array in Java
– One Dimensional Array
– Multidimensional Array
• One Dimensional Array in Java

OOP Prepared By: Daniel Tesfy 47


Example

OOP Prepared By: Daniel Tesfy 48


Example 2

OOP Prepared By: Daniel Tesfy 49


Multidimensional Array

OOP Prepared By: Daniel Tesfy 50


Multidimensional Array

OOP Prepared By: Daniel Tesfy 51


ArrayList
– Dynamic sized arrays in Java that implement List interface.
– It is part of collection framework in java.
– ArrayList has a set of Methods to access elements and modify them.
– Syntax

OOP Prepared By: Daniel Tesfy 52


Package statement

– import java.util.List; // import just the List


interface
– import java.util.ArrayList; // import just the ArrayList
class
– Another option is to import everything at the same
level in a package using import packageName.*.
– import java.util.*;
• // import everything including List and ArrayList
OOP Prepared By: Daniel Tesfy 53
common methods of class ArrayList<T>.

OOP Prepared By: Daniel Tesfy 54


add() method of ArrayList

– add(E e): The add(E e) method is used to add the specified element e in
the ArrayList.
– add(int index, E e): The add(int index, E e) method is used to add the
specified element at a specific position in the ArrayList.
– addAll(Collection c): The addAll(Collection c) is used to add the
collection of elements to the end of the ArrayList.
– addAll(int index, Collection c): The addAll(int index, Collection c) is
used to add the collection of elements at specified position in the
ArrayList. OOP Prepared By: Daniel Tesfy 55
ArrayListTest

OOP Prepared By: Daniel Tesfy 56


Example 2

OOP Prepared By: Daniel Tesfy 57


End of Chapter Three

OOP Prepared By: Daniel Tesfy 58


CHAPTER FOUR

OOP Prepared By: Daniel Tesfy 59

You might also like