Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal [email protected].

25
Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal [email protected]

Transcript of Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal [email protected].

Page 1: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Object Oriented Programming

Lecture 4: Flow Control

Mustafa Emre İ[email protected]

Page 2: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Recap

• Implementation– Coding – writing source code

– Assignments1-2

– Circle class

– Point class

– Applications using these classes

Page 3: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Today

• Flow control– Decision making

– Loops

• Thinking in Java – Chapter 3

Page 4: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Controlling Program Flow

• Branching flow based on certain criteria• Conditional statements• Repeating a process – Loops

Page 5: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Normal Flow

• A program is a series of commands

• Step by step, top-down execution of commands

• A pre-determined series is not adequate

• Based on state, a different route might need to be taken

• Need a branching mechanism - if

Page 6: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Including an optional additional path

• Executing a set of commands based on certain conditions

• Condition is of a boolean type in Java: either “true” or “false”

truefalse

Condition?

Page 7: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

if

if (condition) {if condition is ‘true’

instructions to execute

....

}

instructions after merging of both branches (normal execution path)

Page 8: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Parallel Paths

• The either/or situation: choosing one path or the other based on a condition.

• if - else

truefalse

Condition?

Page 9: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

if – else

if (condition) {instructions that are to be executed

when the condition is true

....

}

else {

instructions to be executed

when the condition is false

}

instructions after merging of both branches

Page 10: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

More than two paths

• When more than two alternatives exist for a condition, we go through the list of possibilities. Elminating them one-by-one

true

Condition2false

false

falsetrue

true

Condition1

Condition3

Page 11: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

if – elseifif (condition1) {

instructions to be executed if condition1 is true....

}elseif (condition2) {

instructions to be executed if condition1 is false but condition2 is true

}elseif (condition3) {

instructions to be executed if all conditions above are false and condition3 is true

} else {

instructions to be executed if all conditions are false

}

instructions after merging of all branches

Page 12: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

A different “condition testing”

• .if – elseif command allows us to test conditions based on boolean value (ture or false)

• The switch statement allows us to test for various values of a particular variable type

value4value1 value2 value3

test

Page 13: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

switchswitch (variable) {case value1:

instructions;break;

case value2:

instructions;break;

case value3:

instructions;break;

case value4:

instructions;break;

default:

instructions;}

Page 14: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Repeating a process

• Often a series of commands need to be repeated• Types of repetition:

– An application itself is a mecahnism for repeating commands.

– Function/method calls are repetitons based on demand

– Loops:

• A certain number of repetitons

• Repetition until a condition is met

Page 15: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Loops

truefalse

Condition?

true

false

Condition?

Page 16: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

while

while (test condition) {

body of loop;

}

---int i=0;

while (i<10) {

System.out.println(i);

i++;

}

Page 17: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

do - while

Do {

body of loop;

} while (test condition) ;

---int i=0;

do {

System.out.println(i);

i++;

} while (i<10) ;

Page 18: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

for

for (initialization[s]; test condition; counter update[s]) {

body of loop;

}

-----for (int i=0; i<10; i++) {

System.out.println(i);

}

Page 19: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

break - continue

• break– For immediate exit out of any loop body– Only one level exit in case of nested loops

• continue– To start the next iteration of the loop immediately.– Only one level

• If there is a need to go beyond one level, use “labels”.

Page 20: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

break - continue

while (true) { //sonsuz döngü

komutlar... ;

if (istisnai bir şart) {

break; //döngüden çıkış

}

komutlar;

}

Page 21: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Assignment 4

• Write a class named ‘Assignment4’ that implements the following static methods.

– static boolean isEven(int number)• if number is even, return true, otherwise return false

– static String resolveAreaCode (int code)• Return the name of the area for the area codes below:• 212: İstanbul-1; 216: İstanbul-2; 312: Ankara; 232: İzmir;

322: Adana; 266: Balikesir; 224: Bursa; 462: Trabzon; 532: Turkcell; 542:Vodafone; 505-Avea; other codes: Unknown_code

• (you are expected to use the switch statement)

Page 22: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Assignment 4

– static void fibonacciSeries(int n)• Display the first n numbers in the Fibonacci sequence (The

sequence of numbers that start with 0 and 1 and where each number thereafter is the sum of the two preceeding numbers.)

– static void fibonacciUntil (int max)• Display the fibonacci sequence that ends with the first number

greater than max.

– static void squares(int n)• Display the squares of the first n integers starting with 0.

(02,12,22,32,...)

– static void squaresUntil(int max)• Display the squares of integers that end with the first square

greater than max.

Page 23: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Assignment 4

– static void randomTest()• The Math.random() method generates a real number n (of

type double) where, 0.00 <= n < 1.00. Using this method, generate 1000 integers between 50 and 150 and display their average.

– static void countdown(int n)• Display integers (in reverse order) from n down to 0.

Page 24: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Assignment 4

– Write a class named ‘Assignment4App’• It should test all the static methods of the Assignment4 class

and display the outputs on screen.

Page 25: Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal emreilal@iyte.edu.tr.

Next Week

• Preperation: Thinking in Java Chapter 4-6