View
213
Download
0
Embed Size (px)
Citation preview
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. http://www.csi.uottawa.ca:4321/java/index.html#nestedif-elsestatement
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 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 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
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
7
Example
8
Example
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
}
10
Example – three cases
11
Example with random value
12
Example with advanced Switch
13
Iteration Statements
WhileDo-whileForNested 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 100while (n > 100) {n = n + 1;}
15
Example – while
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
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
Example – for-loop
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
23
Jump Statement
Three jump statementsBreak, continue and return
24
Break and Continue with the nested loop
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
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... }
28
Example - continue
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
30
Example
31
Summary
Selection Statements If and switch
Iteration Statements While, do-while, for and nested
loops
Jump Statements Break, continue and return