31
1 Control Statements Lecture 6 from Chapter 5

1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

1

Control Statements

Lecture 6 from Chapter 5

Page 2: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

2

Three principles of OOP- Encapsulation

Encapsulation allows changes to code to be more easily made. It is because one can be confident that 'outsiders' are not relying on too many details. It has definition Creating a module to contain some algorithm or data structure, thus hiding its details behind the module's interface. http://www.csi.uottawa.ca:4321/java/index.html#nestedif-elsestatement

Page 3: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

3

3 principles of OOP- Inheritance and Polymorphism

Inheritance: It is a method for extending existing classes by adding methods and fields.Polymorphism: The behaviour varies depending on the actual type of an object.

Page 4: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

4

Review

Selection Statements If and switch

Iteration Statements While, do-while, for and nested

loops

Jump Statements Break, continue and return

Page 5: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

5

Selection Statements - If

If (condition) statement1; else statement2If condition is true, it will execute statement1 else statement2.Example

int a = 3, b = 4;if (a >b) a =1; // since a>b is false, it else b = 1 // will set b = 1

Page 6: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

6

Selection Statements – Nested Ifs

It is an if statement that is the target of another if or else.Exampleint a = 3, b = 4, c = 5;if (a >b) { //a >b

if (b > c) b = 1; //b > c, b = 1else c = 1; //b < c, c = 1}else a =1; // b > a, a = 1

Page 7: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

7

Example

Page 8: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

8

Example

Page 9: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

9

Selection Statement - switch

It is a multiway branch statement. It is similar to if-else-is, but is more well organized.

Switch (expression) {Case value1: //statement1Break;Case value2: //satement2Break…Default;//default statement

}

Page 10: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

10

Example – three cases

Page 11: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

11

Example with random value

Page 12: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

12

Example with advanced Switch

Page 13: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

13

Iteration Statements

WhileDo-whileForNested loop

Page 14: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

14

Iteration Statements - While

while(condition){// statements to keep executing while condition is true....

} Example

//Increment n by 1 until n is greater than 100while (n > 100) {n = n + 1;}

Page 15: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

15

Example – while

Page 16: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

16

Iteration Statements – do while

Do {// statements to keep executing while condition is true

} while(condition)It will first executes the statement and then evaluates the

condition.Example

int n = 5; Do {

System.out.println(“ n = “ + n);N--;

} while(n > 0);

Page 17: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

17

Example – difference between while and do-while

Page 18: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

18

Iteration Statements – for

for(initializer; condition; incrementer){// statements to keep executing while condition is true}  

Example

int i; int length = 10; for (i = 0; i < length; i++) { . . . // do something to the (up to 9 ) . . . }  

Page 19: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

19

Example – for-loop

Page 20: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

20

Multiple Initializers and Incrementers

Sometimes it's necessary to initialize several variables before beginning a for loop. Similarly you may want to increment more than one variable. Example

for (int i = 1, j = 100; i < 100; i = i+1, j = j-1) { System.out.println(i + j); }

Page 21: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

21

Nested Loop – Java allows loops to be nested

Page 22: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

22

Modification of nested loop

Page 23: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

23

Jump Statement

Three jump statementsBreak, continue and return

Page 24: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

24

Break and Continue with the nested loop

Page 25: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

25

Break as a form goto

A break statement exits a loop before an entry condition fails. The break statement can also be employed by itself to provide a form of “goto”. The format is:break label:

Page 26: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

26

Example – use break to exit from loop

Page 27: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

27

Continue - Explanation

Sometimes it is necessary to exit from the middle of a loop. Sometimes we want to start over at the top of the loop. Sometimes we want to leave the loop completely. For Examplefor (int i = 0; i < length; i++) { if (m[i] % 2 == 0) continue; // process odd elements... }

Page 28: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

28

Example - continue

Page 29: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

29

Return

It has purpose to exit from the current methodIt jumps back to the statement within the calling method that follows the original method call.

Examplereturn expression

Page 30: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

30

Example

Page 31: 1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It

31

Summary

Selection Statements If and switch

Iteration Statements While, do-while, for and nested

loops

Jump Statements Break, continue and return