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

Control Structures Oop

This document discusses control structures in Java programming. It covers decision statements like if, if-else and chained if-else statements which allow code to execute conditionally based on boolean expressions. It also discusses switch statements that enable testing multiple cases generated by an expression. Loops are also mentioned as another type of control structure that can alter program flow. The document is a lecture outline on control structures for an Object Oriented Programming class at Debre Markos Institute of Technology.

Uploaded by

Footkball 1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Control Structures Oop

This document discusses control structures in Java programming. It covers decision statements like if, if-else and chained if-else statements which allow code to execute conditionally based on boolean expressions. It also discusses switch statements that enable testing multiple cases generated by an expression. Loops are also mentioned as another type of control structure that can alter program flow. The document is a lecture outline on control structures for an Object Oriented Programming class at Debre Markos Institute of Technology.

Uploaded by

Footkball 1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

DEBRE MARKOS INSTITUTE OF TECHNOLOGY

ECEg3142: Object Oriented Programming

2. Basic Programming Constructs of Java


Control Structures

Mequanent Argaw Muluneh


Day 3
Thursday, June 3, 2021
2
2

Lecture Outline

❖ Decision Statements

❖ Loops

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
Program Flow 3
3

A more sophisticated program


❖ Java will execute the statements in your statement
code in a specific sequence, or "flow".
statement
❖ The "flow" of the program can be
described through a "flow diagram":
statement statement
a simple program

statement
statement
statement

statement

statement statement
2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
4
4

What are Control Structures?


❖ Control structures alter the flow of the program, the sequence of statements
that are executed in a program.

❖ They act as "direction signals" to control the path a program takes.

❖ Two types of control structures in Java:

❖ decision statements & loops

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
5
5

Decision Statements
❖ A decision statement allows the code to execute a statement or
block of statements conditionally.

❖ The two types of decision statements in Java are:


❖ if statements
❖ switch statements

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
6
6

if Statement
if (expression) {
statement;
}
rest_of_program;

❖ expression must evaluate to a boolean value, either true or false

❖ If expression is true, statement is executed and then rest_of_program

❖ If expression is false, statement is not executed and the program continues


at rest_of_program

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
7
7

if Statement Flow Diagram


The if decision statement executes
a statement if an expression is true. no
Is expression
true?

yes
if (expression) {
statement1; execute
statement1
}
rest_of_program
execute
rest_of_program

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
8
8

if- else Statement


if (expression) {
statement1;
}
else{
statement2;
}
next_statement;

❖ Again, expression must produce a boolean value.

❖ If expression is true, statement1 is executed and then next_statement


is executed.

❖ If expression is false, statement2 is executed and then next_statement


is executed.
2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
9
9

if-else Flow Diagram


The if-else decision statement executes
is
yes no
“expression”
a statement if an expression is true and true?
a different statement if it is not true.

if (expression){ execute execute


statement1; statement1 statement2
} else {
statement2;
} execute
rest_of_program rest_of_program

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
10 10

Chained if-else Statements


if (grade == 'A')
System.out.println("You got an A.");
else if (grade == 'B')
System.out.println("You got a B.");
else if (grade == 'C')
System.out.println("You got a C.");
else
System.out.println("You got an F.");

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
11
switch Statements
❖ The switch statement enables you to test several cases generated by a given expression.
❖ For example:
switch (expression) {
case value1:
statement1;
case value2:
statement2;
default:
default_statement;
}
Every statement after the true case is executed.

❖ The expression must evaluate to a char, byte, short or int, but not long, float,

or double.
2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT
Thursday, June 3, 2021
12 12

switch Statements …
expression y
equals Do value1 thing
switch (expression){ value1?
case value1:
// Do value1 thing n

case value2: expression y


// Do value2 thing equals
Do value2 thing
value2?

... n
default:
Do default action
// Do default action
} Continue the
// Continue the program program

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
13 13

break Statements in switch Statements


❖ The break statement tells the computer to exit the switch statement.
❖ For example:
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
default:
default_statement;
break;
}

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
14 14

break in switch …
expression y break
Do value1 thing
equals
switch (expression){ value1?
case value1:
n
// Do value1 thing
break;
case value2: expression y
equals Do value2 thing
// Do value2 thing value2?
break

break;
...
n
default:
// Do default action do default action
break;
}
break Continue the
// Continue the program program

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
15 15

Remember the Chained if-else . . .


if (grade == 'A')
System.out.println("You got an A.");
else if (grade == 'B')
System.out.println("You got a B.");
else if (grade == 'C')
System.out.println("You got a C.");
else
System.out.println("You got an F.");

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
16 16

if-else . . .
❖ This is how it is accomplished with a switch:
switch (grade) {
case 'A':
System.out.println("You got an A.");
break;
case 'B':
System.out.println("You got a B.");
break;
case 'C':
System.out.println("You got a C.");
break;
default:
System.out.println("You got an F.");
}
❖ if-else chains can sometimes be rewritten as a “switch” statement.
❖ switches are usually simpler and faster.
2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
17 17

Loops
❖ A loop allows you to execute a statement or block of statements
repeatedly.

❖ Three types of loops in Java:


1. while loops

2. for loops

3. do-while loops

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
18 18

The while Loop int sum = 0;


int i = 1;
while (i <= 10){
Example sum += i;
while (expression){
i++;
statement; }
sum = ?
}
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
❖ This while loop executes as long as the given logical expression between
parentheses is true. When expression is false, execution continues with the
statement following the loop block.

❖ The expression is tested at the beginning of the loop, so if it is initially false, the
loop will not be executed at all.

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
19 19

The for Loop


for (init_expr; loop_condition; increment_expr) {
statement;
}

The control of the for loop appear in parentheses and is made up of three parts:

1. The first part, the init_expression, sets the initial conditions for the loop and is executed
before the loop starts.

2. Loop executes so long as the loop condition is true and exits otherwise.

3. The third part of the control information, the increment_expr, is usually used to
increment the loop counter. This is executed at the end of each loop iteration.

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
20 20

The for Loop …


Example
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
} 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
❖ What is the value of sum?
❖ Example:
for(int div = 0; div < 1000; div++){
if(div % 2 == 0) {
System.out.println("even: " + div);
} else {
System.out.println("odd: " + div);
}
}
prints out each integer from 0 to 999,
❖ What will this for loop do?
correctly labeling them even or odd
2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
21 21

The for Loop …


❖ If there are more than one variable to set up or increment they are separated by
a comma.
for(i=0, j=0; i*j < 100; i++, j+=2) {
System.out.println(i * j);
}

❖ You do not have to fill all three control expressions but you must still have two
semicolons.
int n = 0;
for(; n <= 100;) {
System.out.println(++n);
}

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
22 22

The for Loop …


Initialize count
The while loop The for loop
n
Test condition
is true?
n
Test condition
is true? y

Execute loop
y statement(s)

Execute loop
statement(?) Increment
count

Next statement New statement

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
23 23

The continue Statement


❖ The continue statement causes the program to jump to the next iteration of
the loop.
/**
* prints out "5689"
*/
for(int m = 5; m < 10; m++) {
if(m == 7) {
continue;
}
System.out.print(m);
}
2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
24 24

The break Statement in Loops


❖ We have seen the use of the break statement in the switch statement.

❖ You can also use the break statement to exit the loop entirely.
// prints out numbers unless
// num is ever exactly 400
while (num > 6) {
if(num == 400) {
break;
}
System.out.println(num);
num -= 8;
}

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
25 25

Nested Loops
❖You can nest loops of any kind one inside another to any

depth. What does this print?


6
5
5
3
3
2
1
2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
26 26

Pop Quiz
1. In the switch statement, which types can expression evaluate to?
char, byte, short, int
2. What must be used to separate each section of a for statement? semicolons

3. Which statement causes a program to skip to the next iteration of a


continue
loop?

4. Write a for loop that outputs 100-1 in reverse sequence.

5. Write a for loop that outputs all numbers that are divisible by 3
between 0-50.

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021
27 27

Questions???

2. Control Structures, Object Oriented Programming, Electrical and Computer Engineering, DMiT Thursday, June 3, 2021

You might also like