Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Statements Lecture 6 from Chapter 5.

Similar presentations


Presentation on theme: "Control Statements Lecture 6 from Chapter 5."— Presentation transcript:

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

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 Review Selection Statements Iteration Statements Jump Statements
If and switch Iteration Statements While, do-while, for and nested loops Jump Statements Break, continue and return

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 = // will set b = 1

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 Example result

8 Example result

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 } if expression is value1, it will jump to statement 1 until it hits break

10 Example – three cases

11 Example with random value
the value is random

12 Example with advanced Switch
the value is random

13 Iteration Statements While Do-while For Nested loop

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

15 Example – while result

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

17 Example – difference between while and do-while
i = 1: no difference i = 6: difference

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

19 args[0]: Peter args[1]: Suana args[2]: John
Example – for-loop args[0]: Peter args[1]: Suana args[2]: John

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 Nested Loop – Java allows loops to be nested

22 Modification of nested loop
j depends on i

23 Jump Statement Three jump statements Break, continue and return

24 Break and Continue with the nested loop
result: j 1 and 2) only break

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 Example – use break to exit from loop
break once it becomes negative

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... } it will process the odd case

28 Example - continue 1, 3, 5, 7 are missing

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

30 Example

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


Download ppt "Control Statements Lecture 6 from Chapter 5."

Similar presentations


Ads by Google