Control Statements Lecture 6 from Chapter 5
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
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.
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
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
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
Example result
Example result
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
Example – three cases
Example with random value the value is random
Example with advanced Switch the value is random
Iteration Statements While Do-while For Nested loop
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; }
Example – while result
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);
Example – difference between while and do-while i = 1: no difference i = 6: difference
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 ) . . . }
args[0]: Peter args[1]: Suana args[2]: John Example – for-loop args[0]: Peter args[1]: Suana args[2]: John
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); }
Nested Loop – Java allows loops to be nested
Modification of nested loop j depends on i
Jump Statement Three jump statements Break, continue and return
Break and Continue with the nested loop result: j 1 and 2) only break
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:
Example – use break to exit from loop break once it becomes negative
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
Example - continue 1, 3, 5, 7 are missing
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
Example
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