Control Structure: Branching & Repetition: Object-Oriented Programming
Control Structure: Branching & Repetition: Object-Oriented Programming
Object-Oriented Programming 2
Please see these materials first
Object-Oriented Programming 3
Outlines
1. Branching.
• The if-else, ternary, and switch-case models.
2. Repetition.
• The for and its variants, while, and do-while models.
• Breaking and skipping a loop.
Object-Oriented Programming 4
Branching
Object-Oriented Programming 5
Background
Object-Oriented Programming 6
A case study
Object-Oriented Programming 7
// File: BMICalculator.java
package example.branching;
Body Mass Index (BMI)
public class BMICalculator {
public static float calculate(int _weight, int _height) {
float height_m = (float) _height / 100; // in m
return (_weight / (height_m * height_m)); BMI implementation in a class-level behavior.
} Why not object level?
System.out.printf("%.2f\n", bmi);
}
}
Object-Oriented Programming 8
Branching: key concept
Object-Oriented Programming 9
Branching in Java
• Branching statements:
• the if-else statement; and
• the switch-case statement.
Object-Oriented Programming 10
Conditional expression
Object-Oriented Programming 11
The if-else Model
Object-Oriented Programming 12
The if-else model
Object-Oriented Programming 13
package example.branching;
System.out.printf("%.2f\n", bmi);
System.out.printf("%h\n", bmiLevel);
}
}
Object-Oriented Programming 14
The Ternary Model
Object-Oriented Programming 15
The ternary model
type v;
if(conditional-expression) {
v = expression1;
} else { equivalent
v = expression2;
}
type v;
v = (conditional-expression)? expression1 : expression2;
Object-Oriented Programming 16
package example.branching;
System.out.printf("%.2f\n", bmi);
Object-Oriented Programming 17
The switch-case Model
Object-Oriented Programming 18
The switch-case model
• A multi-alternative flows.
• An expression is tested to a list of labels.
• Possible labels:
• Constant expression of type char, byte, short, and int;
• Enumerated constants; or
• String literals. switch (expression) {
• When the expression matches to a label: case constant1:
• the program execution continues ... statements
execution
there downwardly. case constant2:
• Otherwise: ... statements
• If the default case does exist, ... // more cases
the execution continues there. default: // optional
... statements
• Else, no action at all.
}
Object-Oriented Programming 19
package example.branching;
import java.util.Scanner;
} 0
add coffee
add boiling water
Object-Oriented Programming 20
package example.branching;
System.out.printf("%.2f\n", bmi);
29.75
short bmiLevel = 0; // assume normality 1
obese class iii Incorrect, why?
if (bmi < 16.0) {
bmiLevel = -3;
} else if (16.0 <= bmi && bmi < 17.0) {
bmiLevel = -2;
} else if (17.0 <= bmi && bmi < 18.5) { String strBmiLevel = null;
bmiLevel = -1;
} else if (25.0 <= bmi && bmi < 30.0) { switch (bmiLevel) {
bmiLevel = 1; case -3:
} else if (30.0 <= bmi && bmi < 35.0) { strBmiLevel = "severe thinness";
bmiLevel = 2; case -2:
} else if (35.0 <= bmi && bmi < 40.0) { strBmiLevel = "moderate thinness";
bmiLevel = 3; case -1:
} else if (40.0 < bmi) { strBmiLevel = "mild thinness";
bmiLevel = 4; case 0:
} ...
case 4:
System.out.printf("%h\n", bmiLevel); strBmiLevel = "obese class iii";
} }
}
System.out.printf("%s\n", strBmiLevel);
Object-Oriented Programming 21
Breaking the switch-case
• Cases serve as labels, from where the program execution will go.
• The execution then to be continued from that point downwardly.
• To escape, break or return keyword is used.
switch (expression) {
case label1:
... statements
execution
break;
case label2:
... statements
break;
... // more cases
default: // optional
... statements
}
Object-Oriented Programming 22
package example.branching;
Object-Oriented Programming 23
Repetition
Object-Oriented Programming 24
Repetition
Object-Oriented Programming 25
Repetition in Java
Object-Oriented Programming 26
Repetition models
Object-Oriented Programming 27
The for Loop &
Its Variants
Object-Oriented Programming 28
The for loop: suitable cases
• Range values.
done done
sheet
jump 1 to 10
1 to 20
• Jump 10 times.
jumping grading
Object-Oriented Programming 29
The for loop statement
...
for(decl-init statement; cond-expr; incr-decr statement) {
// repetition body
}
Object-Oriented Programming 30
The for loop statement
An statement to declare and initialize A conditional expression to decide should
the watch variable (sentinel). the repetition body be executed or not.
This statement will be executed once, This expression will be executed right before
before any other instructions. the repetition body.
Object-Oriented Programming 31
package example.repetition;
Object-Oriented Programming 32
The for loop statement to
iterate a collection object
The to-be iterated collection.
An element, retrieved from the collection.
Object-Oriented Programming 33
package example.repetition;
import java.util.Collection;
import java.util.LinkedList;
The for loop statement to
iterate a collection object
public class ForCollection {
Jaka Sembung
null
Wiro Sableng
Object-Oriented Programming 34
The while Loop
Object-Oriented Programming 35
The while loop: suitable cases
false false
• Sample cases:
still day any sheet?
jumping grading
... ...
Object-Oriented Programming 36
The while loop statement
... ...
... ...
while(conditional-expression) {
// repetition body
}
Object-Oriented Programming 37
The while loop statement
while(conditional expression) {
// repetition body
}
Object-Oriented Programming 38
package example.repetition;
while (grading < nrAnswerSheet) { This is the way to make the conditional expression fail sometime
++grading; in the future.
System.out.printf("grading #%d\n", grading);
}
}
}
grading
grading
#1
#2
Can you develop the same solution
grading #3 with a decrement operation?
grading #4
grading #5
grading #6
grading #7
grading #8
grading #9
grading #10
grading #11
grading #12
Object-Oriented Programming 39
The do-while Loop
Object-Oriented Programming 40
The do-while loop: suitable cases
... ...
• Sample cases:
• A random number is generated. incorrect
more hobby?
false false
Object-Oriented Programming 41
The do-while loop statement
false
...
do {
// repetition body
} while(conditional-expression);
Object-Oriented Programming 42
The do-while loop statement
do {
// repetition body
} while(conditional expression);
Object-Oriented Programming 43
package example.repetition;
import java.util.Scanner;
Object-Oriented Programming 44
Exiting and Skipping Loop
Object-Oriented Programming 45
Exiting from a loop
Object-Oriented Programming 46
package example.repetition;
A B C D E F G H I J
K
Object-Oriented Programming 47
Skipping a loop cycle
Object-Oriented Programming 48
package example.repetition;
A B C D E F G H I J L M N O P Q R S T
U
Object-Oriented Programming 49
Todo
Object-Oriented Programming 50
References
Object-Oriented Programming 51
Thank
you
Object-Oriented Programming 52