KEMBAR78
COS202 Lecture 2 | PDF | Control Flow | Object Oriented Programming
0% found this document useful (0 votes)
26 views5 pages

COS202 Lecture 2

The document is a lecture on Java repetition statements, detailing the use of loops such as while, do-while, and for. It explains their syntax, provides examples, and discusses the break and continue keywords for controlling loop execution. Additionally, it includes classwork questions and assignments related to the concepts covered in the lecture.

Uploaded by

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

COS202 Lecture 2

The document is a lecture on Java repetition statements, detailing the use of loops such as while, do-while, and for. It explains their syntax, provides examples, and discusses the break and continue keywords for controlling loop execution. Additionally, it includes classwork questions and assignments related to the concepts covered in the lecture.

Uploaded by

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

ACHIEVERS UNIVERSITY, OWO

COLLEGE OF NATURAL AND APPLIED SCIENCES


DEPARTMENT OF COMPUTER SCIENCE
COS 202 – COMPUTER PROGRAMMING II – 3 UNITS

LECTURER IN CHARGE - MR. ADEPOJU, S. E.

LECTURE 2

JAVA REPETITION STATEMENT


As learnt in control structure class, a loop control statement allows you to repeat one or more
statements until a specified condition is met. They are essential tools that allow for the execution
of certain operations until the condition becomes false. This type of control statement is what
makes computers so valuable. A computer can repeatedly execute the same instructions
over-and-over again without getting bored with the repetition.
Java has three standard looping constructs: while, do-while, and for. Loops can execute a block
of code as long as a specified condition is reached. Loops are handy because they save time,
reduce errors, and they make code more readable.

While loop
The while loop iterates through a block of code as long as a specified condition is true:
Syntax
while (condition) {
// code block to be executed
}

where code block to be executed may be a single statement or a block of statements, and
condition defines the condition that controls the loop. The condition may be any valid Boolean
expression. The loop repeats while the condition is true. When the condition becomes false,
program control passes to the line immediately following the loop.

Example 2.1
1​ public class Main {
2​ public static void main(String[] args) {
3​ int toddler = 0;
4
5​ while (toddler <= 3) {
6​ System.out.println(toddler);
7 ​ toddler++;
8​ }
9​ }
10​ }

Note: It is important to increase the variable used in the condition, otherwise the loop will never
end!

Example 2.2
1​ public class Main {
2​ public static void main(String[] args) {
3​ int countdown = 5;
4
5​ while (countdown > 0) {
6​ System.out.println(countdown);
7 ​ countdown--;
8​ }
9
10​ System.out.println("Happy New Year!!");
11​ }
12​ }
13​ }

Do… While loop


The do/while loop is a variant of the while loop. This loop will execute the code block once,
before checking if the condition is true, then it will repeat the loop as long as the condition is
true.
Syntax
do {
// code block to be executed
}
while (condition);

Example 2.3
1​ public class Main {
2​ public static void main(String[] args) {
3​ int i = 0;
4
5​ do {
6​ System.out.println(i);
7 ​ i++;
8​ }
9​ while (i < 5);
10​ }
11​ }

The loop will always be executed at least once, even if the condition is false, because the code
block is executed before the condition is tested:

For loop
When you know exactly how many times you want to loop through a block of code, use the for
loop instead of a while loop.
Syntax
for (initialization; condition; update) {
// code block to be executed
}

where:
initialization is executed (one time) before the execution of the code block.
condition defines the condition for executing the code block.
update is executed (every time) after the code block has been executed.

Example 2.4
1​ public class Main {
2​ public static void main(String[] args) {
3​
4​ for (int i = 0; i < 10; i++) {
5​ System.out.println(i);
6​ }
7​ }
8​ }

The break Keyword


We have already seen the break statement used in the previous class, it was used to "jump out"
of a switch statement. The break statement can also be used to jump out of a loop. The example
below stops the loop when i is equal to 5

Example 2.5
1​ public class Main {
2​ public static void main(String[] args) {
3​
4​ for (int i = 0; i < 10; i++) {
5​ if (i == 5) {
6​ break;
7​ }
8​ System.out.println(i);
9​ }
10​ }
11​ }

The continue Keyword


The continue statement breaks one iteration (in the loop), if a specified condition occurs, and
continues with the next iteration in the loop. Example 2.6 skips the value of 5.

Example 2.6
1​ public class Main {
2​ public static void main(String[] args) {
3​
4​ for (int i = 0; i < 10; i++) {
5​ if (i == 5) {
6​ continue;
7​ }
8​ System.out.println(i);
9​ }
10​ }
11​ }

Java break and continue can also be used in while loops.

CLASSWORK
1)​ How many times will the following loop execute?
int i = 0;
while (i < 4) {
i++;
}

2)​ How many times will the following loop execute?


int j = 1;
do {
System.out.println("Error");
}
while (j == 1);

3)​ How many times will the following loop execute?


for (x = 1; x > 5; x++) {
System.out.println(x);
}
ASSIGNMENT
1)​ Write a Java program that determines whether a number is even or odd. List and explain four
(4) keywords used in the program.
2)​ In a Java program, what is the default control flow? Explain
3)​ Develop a Java program that displays even numbers between 0 and 10 using any of the loop
statements.

You might also like