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 |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
if syntax
Lecture notes |
Pdf slides |
|
if ... elseLecture notes |
Pdf slides |
|
if ... else syntax
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
if ... elseLecture notes |
Pdf slides |
|
if ... else if ...
elseLecture 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 |
|
switchLecture notes |
Pdf slides |
|
switch Syntax
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
switch expressions
Lecture notes |
Pdf slides |
|
switch expressions
Lecture notes |
Pdf slides |
|
switch
statements
Lecture notes |
Pdf slides |
|
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 |
|
whileLecture 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
jLecture 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 * b;System.out.println("Hello");3 * (a - 4)b = 3 * (a - 4);Notice the trailing “;”.
a = b + 3; b = a - 4;Discouraged by good coding practices:
Poor readability
Hampers debugging
Method scope being delimited by the { ... } block
A variable's visibility is restricted to its block:
public class X {
public static void main(String[] args) {
int i = 1; // Visible in current method
System.out.println("i=" + i);
someMethod(); // Method call
}
}| Code | public static void main(String[] args) { double initialAmount = 500; { // first block final double interestRate = 2.0; // 2.0 % System.out.println(""First block interest:" + initialAmount * interestRate / 100" + initialAmount * interestRate / 100); } System.out.println("Second block"); { // second block final double interestRate = 1.0; // 1.0 % System.out.println("Second block interest:" + initialAmount * interestRate / 100); } } |
|---|---|
| Result | First block interest:10.0 Second block interest:5.0 |
|
|
| Code | |
|
|---|---|---|
| Result | Before: i = 1, j = 2 After: i = 2, j = 2 |
|
double saving = 320.00;
if (1000 <= saving) {
// Rich customer, 1,2% interest rate
System.out.println(
"Interest:" + 1.2 * saving / 100);
}
System.out.println("Done!"); |
|
Done! |
if (booleanExpression)
(block | statement)else
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 label:
[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;
} |
|
| Code | Output |
|---|---|
|
Second |
int wed = 3;
int number = 6;
switch(number) {
case 1 -> System.out.println("Monday"); // o.k., constant int literal
case 1 + 1 -> System.out.println("Tuesday"); // o.k. constant int expression
case wed -> System.out.println("Wednesday"); // Error: Constant expression required
...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: Desired number of repetitions must be known at compile time.
System.out.print("Enter desired number of repetitions: ");
final int repetitions = scan.nextInt();
switch(repetitions) { // Employing fall-through
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) |
|
|
|
|
|
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 readability. 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