M3 Statements FlowControl
M3 Statements FlowControl
control
Programming 1
-1-
Contents
1. Expressions and statements
2. Conditional execution
3. Loops
-2-
Deege U Java lesson 016
Expressions and
Statements
Expression
a = 15 * b + c;
-4-
Statement
Basic instruction
-5-
Statement types
• Declaration
int a;
• Assignment
a = a + 1;
• Method call
System.out.println(a);
• Compound statements (flow control)
– Logical: if, switch
– Loop: while, for
-6-
Grouping statements: {block}
• A block is a group of statements enclosed
within {a pair of curly braces}
if (b < 12) { Bloc that groups
int a = 10; t o state ents
System.out.println(a + b);
}
-7-
Quiz
HowMany.java
public static void main(String[] args) {
int guess; -How many expressions?
int secret = 56;
int counter = 0;
-How many statements?
-How many code blocks?
Scanner keyboard = new Scanner(System.in);
while (guess != secret) {
System.out.print("Enter a number: ");
guess = keyboard.nextInt();
counter++;
if (guess < secret) System.out.print("Higher! ");
if (guess > secret) System.out.print("Lower! ");
} //end while
System.out.print("Congratulations, you used " + counter
+ " guesses to find the right number!");
keyboard.close();
}//end main
-8-
Code convention: Indent blocks
HowMany.java
public static void main(String[] args) {
int guess;
int secret = 56;
int counter = 0;
-9-
Code convention: Indent code
•Indent statements an extra level within each
new code block
•Indent when continuing a statement on a new
line
Shortcut Ctrl-Alt-L
- 10 -
Code blocks and variable scope
•A variable is only known within the block it is
declared… and in nested code blocks
•Scope = the program region where the
variable is known, where it can be used
– Starts from the line where it is declared
– Ends at the closing curly brace of the code
block in which it is declared
- 11 -
Quiz
bool
public static void main(String[] args) {
boolean bool = false;
outer int outer = 100;
if (!bool) {
inner
int inner = 10;
System.out.println(outer + inner);
}
bool = true;
inner if (bool) {
int inner = 42;
System.out.println(outer + inner);
}
System.out.println(outer);
}
Can o add here int outer = 200;?
- 13 -
Conditional
execution
Flow control
•Statements are executed in sequence
•Flow control statements modify the sequential
sequence
– conditional execution
▪if...else
▪switch
– repeated execution
▪while
▪for
- 15 -
if statement 4.5
if(condition)statement;
[condition == true]
Best practice:
always use a
{code block}
after an if statements
- 17 -
if…else statement 4.6
- 18 -
if…else syntax
if(condition)statement;
else statement;
[else] [condition == true]
Best practice:
always use a else if
statements statements
{code block}
after an if
and else
- 19 -
Exercise
- 20 -
Nested if
- 22 -
Conditional operator 4.6.4
Ternary operators
if then else
1. returns "too high"or "ok"
2. concatenates returned value ith "BMI is "
- 23 -
switch statement 5.6
- 25 -
switch statement
•Fall through (until a break)
Switch.java printSeason
switch(month) {
case "march":
case "april":
case "may": System.out.println("spring");
break;
case "june":
case "july":
case "august": System.out.println("summer");
break;
case "september":
case "october":
case "november": System.out.println("autumn");
break;
case "december":
case "january":
case "february": System.out.println("winter");
break;
default: System.out.println("invalid month");
}
- 26 -
Simplified switch expression
- 27 -
Simplified switch expression
s itch can e used as an ex ression
since Java 14 (so, returns a alue)
String seasonName = switch (month) {
case "march", "april", "may" -> "spring";
case "june", "july", "august" -> "summer";
case "september", "october", "november" -> "autumn";
case "december", "january", "february" -> "winter";
default -> "invalid month";
}; If ou se a s itch ex ression
System.out.println(seasonName); and all ossi le cases are not
covered, then default is
mandator
Switch.java printSeasonYield
String seasonName = switch (month) {
case "march", "april", "may" -> "spring";
case "june", "july", "august" -> "summer";
case "september", "october", "november" -> "autumn";
case "december", "january", "february" -> "winter";
default -> {
System.out.println(
">>> Please use lower case english month names");
yield "invalid month"; When usin a {block ith m lti le statements}
} in a si plified s itch e pression
}; precede t e val e to be returned wit yield
System.out.println(seasonName);
- 29 -
Exercises
•Ex 03.01 Age
•Ex 03.02 Scrabble
- 30 -
Loops
while loop 4,7
while (condition) {
statements;
}
Processing:
1. check condition
○ If true: execute code block
(statements )and return to 1
(check again)
○ If false: end loop and continue
with the statement after the
codeblock
- 33 -
while loop
- 34 -
while loop
• The {curly braces} are not necessary if the
while statement is covering a single
statement int counter = 10;
while (counter < 100)
System.out.println(counter++);
- 35 -
break 5.7
- 36 -
continue
int counter = 0;
while (++counter < 10) {
if (counter % 3 == 0) {continue;}
System.out.print(counter + " ");
}
// 1 2 4 5 7 8
- 37 -
do..while loop 5.5
[else]
[condition == true]
- 38 -
do..while syntax
do {
statements;
} while (condition);
Processing:
1. execute code block (statements)
2. check condition
○ If true: go to 1
○ If false: end loop and continue with
the statement after the codeblock
- 39 -
Exercises
•Ex 03.03 Loops
– Task 1 & 2
- 40 -
for loop: count from 0 to 9 5.3
- 41 -
for loop
initialisation
Deege U Java
[else] lesson 020
code block
[condition == true]
Modification
- 42 -
for loop
•Compact syntax for repeating actions a
number of times
•Syntax
for (initialisation; condition; modification){
statements;
}
•Order of execution:
- 43 -
Comparing for and while loop
int i = 0;
while(i < 10) {
System.out.println(i);
i++;
}
- 44 -
Remarks
- 45 -
Quiz
•What is the outcome of these for loops?
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
- 46 -
Exercises
•Ex 03.03 Loops
– Task 3
•Ex 03.04-03.12
- 47 -
Review
1. Expressions and statements
2. Conditional execution
3. Loops
- 48 -