Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Control Statements Lecture 6 from Chapter 5

2 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. code 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-elsestatementdefinitionhidinginterface

3 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.

4 4 Review Selection Statements If and switch Iteration Statements While, do-while, for and nested loops Jump Statements Break, continue and return

5 5 Selection Statements - If If (condition) statement1; else statement2 If 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

6 6 Selection Statements – Nested Ifs It is an if statement that is the target of another if or else. Example int a = 3, b = 4, c = 5; if (a >b) { //a >b if (b > c) b = 1; //b > c, b = 1 else c = 1; //b < c, c = 1 } else a =1; // b > a, a = 1

7 7 Example

8 8

9 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: //statement1 Break; Case value2: //satement2 Break … Default; //default statement }

10 10 Example – three cases

11 11 Example with random value

12 12 Example with advanced Switch

13 13 Iteration Statements While Do-while For Nested loop

14 14 Iteration Statements - While while(condition) { // statements to keep executing while condition is truecondition true.... } Example //Increment n by 1 until n is greater than 100 while (n > 100) { n = n + 1; }

15 15 Example – while

16 16 Iteration Statements – do while Do { // statements to keep executing while condition is truecondition true } while(condition)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);

17 17 Example – difference between while and do-while

18 18 Iteration Statements – for for(initializer; condition; incrementer) { // statements to keep executing while condition is true } initializercondition true Example intint i; int length = 10; for (i = 0; i < length; i++) {... // do something to the (up to 9 )... } int

19 19 Example – for-loop

20 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); }

21 21 Nested Loop – Java allows loops to be nested

22 22 Modification of nested loop

23 23 Jump Statement Three jump statements Break, continue and return

24 24 Break and Continue with the nested loop

25 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:

26 26 Example – use break to exit from loop

27 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 Example for (int i = 0; i < length; i++) { if (m[i] % 2 == 0) continue; // process odd elements... }

28 28 Example - continue

29 29 Return It has purpose to exit from the current methodmethod It jumps back to the statement within the calling method that follows the original method call.statementmethodmethod call Example return expressionexpression

30 30 Example

31 31 Summary Selection Statements If and switch Iteration Statements While, do-while, for and nested loops Jump Statements Break, continue and return


Download ppt "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."

Similar presentations


Ads by Google