Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
if
syntax
Lecture notes |
Pdf slides |
|
if
... else
Lecture notes |
Pdf slides |
|
if ... else
syntax
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
if ... else
Lecture notes |
Pdf slides |
|
if ... else if ...
else
Lecture notes |
Pdf slides |
|
if ... else if ... else
syntax
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
switch
Lecture notes |
Pdf slides |
|
switch
Syntax
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
switch
statements
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
while
loop
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
while
syntax
Lecture notes |
Pdf slides |
|
while
body
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
do ... while
loop
Lecture notes |
Pdf slides |
|
do ... while
syntax
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
while
Lecture notes |
Pdf slides |
|
while
by for
Lecture notes |
Pdf slides |
|
for
syntax
Lecture notes |
Pdf slides |
|
for
variable scope
Lecture notes |
Pdf slides |
|
for
variable scope
equivalence
Lecture notes |
Pdf slides |
|
for
vs. while
relationship
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
row
and
column
in favour of i
and
j
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Purposes of statements:
Declaring variables and assigning values.
Control whether code will be executed.
Control how often code will be executed.
Statement's body terminated by “;”
{statement};
int a;
a = 33;
int a = 33;
a - 4
b = a - 4;
Notice the trailing “;
”.
a = b + 3; b = a - 4;
Discouraged by good coding practices:
Poor readability
Hampers debugging
double initialAmount = 34; { // first block final double interestRate = 1.2; // 1.2% System.out.println("Interest:" + initialAmount * interestRate / 100); } { // second block final double interestRate = 0.8; // 0.8% System.out.println("Interest:" + initialAmount * interestRate / 100); }
|
|
|
|
Done! |
if (booleanExpression)
(block | statement)
else
double saving = 320.00; if (1000 <= saving ❶) { ❷ // Rich customer, 1,2% interest rate System.out.println( "Interest:" + 1.2 * saving / 100); } ❸ else { ❹ // Joe customer, 0.8% // standard interest rate System.out.println( "Interest:" + 0.8 * saving / 100); } System.out.println("Done!"); Interest:2.56 Done! |
if (booleanExpression) (block | statement) [else (block | statement) ] ❶
Use
if (4 == variable) ...
in favour of:
if (variable == 4) ... ❶
Branches containing exactly one statement don't require a block definition.
double initialAmount = 3200;
if (100000 <= initialAmount)
System.out.println("Interest:" + 1.2 * initialAmount / 100);
else if (1000 <= initialAmount)
System.out.println("Interest:" + 0.8 * initialAmount / 100);
else
System.out.println("Interest:" + 0);
|
else if
|
if (booleanExpression) (block | statement) [else if (booleanExpression) (block | statement) ]* ❶ [else (block | statement) ] ❷
|
Enter a value:123 You entered 123 See |
Task: Convert day's numbers to day's names |
|
final Scanner scan = new Scanner(System.in));
System.out.print("Enter a weekday number (1=Monday, 2=Tuesday,...) : ");
final int number = scan.nextInt();
if (1 == number) {
System.out.println("Monday");
} else if (2 == number) {
System.out.println("Tuesday");
...
} else if (7 == number) {
System.out.println("Sunday");
} else {
System.out.println("Invalid number " + number);
}
switch
statement
...
switch(number) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
case 4: System.out.println("Thursday"); break;
case 5: System.out.println("Friday"); break;
case 6: System.out.println("Saturday"); break;
case 7: System.out.println("Sunday"); break;
default: System.out.println("Invalid number " + number); break;
} ...
Enter a weekday number (1=Monday, 2=Tuesday,...) : 6 Saturday
switch(expression) {
[case value_1 :
[statement]*
[break;] ]
[case value_2 :
[statement]*
[break;] ]
...
[case value_n :
[statement]*
[break;] ]
[default:
[statement]*
[break;] ]
}
String month, season;
...
// Since Java 7: String based case
labels
switch(month) {
case "March": case "April": case "May":
season = "Spring"; break;
case "June": case "July": case "August":
season = "Summer"; break;
case "September": case "October": case "November":
season = "Autumn"; break;
case "December": case "January": case "February":
season = "Winter"; break;
}
}
Objective: Execute the same statement multiple times.
“Solution”: Copy / paste the statement in question:
System.out.println("Do not copy!");
System.out.println("Do not copy!");
System.out.println("Do not copy!");
System.out.println("Do not copy!");
Problem: Only works if number of repetitions is known at compile time.
System.out.print("Enter desired number of repetitions: ");
final int repetitions = scan.nextInt();
switch(repetitions) {
case 5: System.out.println("Do not copy!");
case 4: System.out.println("Do not copy!");
case 3: System.out.println("Do not copy!");
case 2: System.out.println("Do not copy!");
case 1: System.out.println("Do not copy!"); }
Limited and clumsy workaround.
while
Code | Execution |
---|---|
|
Enter repetitions: 3 Do not copy! Do not copy! Do not copy! |
while (booleanExpression)
(block | statement)
int threeSeries = 1;
while ((threeSeries *=3 ) < 100);
System.out.println(threeSeries);
Exercise: Guess resulting output.
Enter value, 0 to terminate: 3 Enter value, 0 to terminate: 1 Enter value, 0 to terminate: 0 Sum: 4 |
do
(block | statement)
while (booleanExpression);
for
Nice to have: More concise syntax
for ( init ; booleanExpression ; update )
(block | statement)
// i being defined within
// loop's scope
for (int i = 0 ; i < 3; i++) {
System.out.println(i);
}
// Error: i undefined outside
// loop's body
System.out.println(i); |
|
for (int i = 0 ; i < 3; i++) {
System.out.println(i);
}
// i undefined in outer scope |
{ // Beginning block scope
int i = 0;
for (; i < 3; i++) {
System.out.println(i);
}
} // Ending block scope
// i undefined in outer scope |
|
|
Observation: for (...)
is more
general than while(...)
.
|
(1|1) (1|2) (1|3) (2|1) (2|2) (2|3) |
|
1 2 3 3 4 5 4 5 6 7 5 6 7 8 9 |
// What do i and j actually represent? for (int i = 0; i < 6; i++) { for (int j = 0; j < i; j++) { System.out.print(i + j + " "); } System.out.println(); } |
// Improved code comprehension. for (int row = 0; row < 6; row++) { for (int column = 0; column < row; column++) { System.out.print( row + column + " "); } System.out.println(); } |
for
|
1 + ... + 5 = 15 |
Will be explained in detail.
Idea: Feed in samples, check results for correctness.
Previous slide: Logic-1 > alarmClock
Sample project at MI Gitlab.
public class AlarmClock { /** Given a day of the week encoded as 0=Sun, 1=Mon,... */ static ❶ public String alarmClock(int day, boolean vacation) { switch (day) { case 1: ... if (vacation) { return "off"; } else { return "10:00"; ...
public class AlarmClockTest { @Test ❶ public void test_1_false() { Assert.assertEquals( "7:00", AlarmClock.alarmClock(1, false)); } ▲ ▲ ▲ ... ┃ ┃ ┃ Expected result Input parameter @Test ┃ ┃ ┃ public void test_0_false() { ▼ ▼ ▼ Assert.assertEquals("10:00", AlarmClock.alarmClock(0, false)); } ...
public class AlarmClockTest { Input parameter @Test ┃ ┃ public void test_1_false() { ▼ ▼ final String result = AlarmClock.alarmClock(1, false); ┗━━━━━━━━━━━━━━━┓ ▼ Assert.assertEquals( "7:00", result); } ▲ ... ┃ Expected result