Java-Conditionals-Loops-Emoon
Java-Conditionals-Loops-Emoon
text I/O
1
Building blocks of
Java
Conditionals and Loops
▪ The if Statement
▪ Loops: the while statement
▪ An alternative: the for loop
▪ Nesting
https://introcs.cs.princeton.edu/java/13flow/
2
Conditionals and Loops
Control flow
• The sequence of statements that are actually executed in a program.
• Conditionals and loops enable us to choreograph control flow.
true boolean 1
statement 1
false
statement 2 statement 1
false
statement 4
statement 3
Flip
Heads
import java.util.Scanner;
int a;
int b; 100
a = input.nextInt(); 99
b//implement here.
= input.nextInt(); 99
100
if(b < a) {
a sequence of -20
int temp = a;
a = b; statements 30
b = temp; within the -20
} curly braces. 30
System.out.println(a);
System.out.println(b);
}
} A. Reads two integers at runtime, then prints them out in ascending order.
6
int a = input.nextInt();
int b = input.nextInt();
int sum = a + b;
int prod = a * b;
System.out.println(a + " + " + b + " = " + sum);
System.out.println(a + " * " + b + " = " + prod);
Good programming practice. Use conditionals to check for and avoid runtime errors. 7
Robert Sedgewick | Kevin Wayne elice>Week 1-2> Error check for int operations
if-elseif
The following program assigns a grade based on the value of a test score:
import java.util.Scanner; Testscore Grade
public class LetterGrade {
public static void main(String[] args) { >=90 A
Scanner input = new Scanner(System.in);
Double testscore = input.nextDouble(); >=80 B
char grade;
>=70 C
if (testscore >= 90) >=60 D
grade = 'A';
else if (testscore >= 80) < 60 F
grade = 'B';
//implement here.
else if (testscore >= 70)
grade = 'C';
96
else if (testscore >= 60) Grade = A
grade = 'D';
else
grade = 'F';
10
https://introcs.cs.princeton.edu/java/13flow/
The while loop
Execute certain statements repeatedly until certain conditions are met.
• Evaluate a booleanexpression. i = 0;
• If true, execute a sequence of statements.
• Repeat. v = 1;
i <= n ? false
Example:
int i = 0;
int v = 1; true
while (i <= n)
System.out.println(v);
{
System.out.println(v); i
= i + 1; i = i + 1;
v= 2 v;
} v=2 v;
12
}
n Loop termination value
}
i Loop control counter
power Current power of 2
Prints the powers of two from 2 0 to 2 n .
13
import java.util.Scanner;
while (i <= n)
System.out.println(v);
i = i + 1;
v = 2 * v;
}
}
14
A do-while loop is almost the same as a while loop except that the
loop-continuation condition is omitted the first time through the loop.
16
The statements
Robert Sedgewick within the do block are always executed at least once.
| Kevin Wayne
Building blocks of
Java
Conditionals and Loops
▪ The if Statement
▪ Loops: the while statement
▪ An alternative: the for loop
▪ Nesting
https://introcs.cs.princeton.edu/java/13flow/
17
The for loop
An alternative repetition structure. Why? Can provide code that is more
• Evaluate an initialization statement. compact and understandable.
• Evaluate a b o o l e a n expression.
• If true, execute a sequence of statements,
then execute an increment statement.
• Repeat.
long product = 1; 1 1
for (int i = 1; i <= N; i++) 2 2
product = i; 6 3
System.out.println(product); 24 4
Compute N! = 1 * 2 * 3 * . . . * N k
2* Math.PI* k/N
0 0
1 1.57079632...
for (int k = 0; k <= N; k++)
2 3.14159265...
System.out.println(k + " " + 2* Math.PI* k/N);
3 4.71238898...
Print a table of function values 4 6.28318530...
int v = 1; 2
while (v <= N/2) 4
v = 2* v; 8
System.out.println(v); 16 trace at end of loop for N = 2 3
Print largest power of 2 less than or equal to N
19
21
https://introcs.cs.princeton.edu/java/13flow/
Nesting conditionals and loops
Nesting
• Any “statement” within a conditional or loop
may itself be a conditional or a loop statement.
• Enables complex control flows.
• Adds to challenge of debugging.
import java.util.Scanner;
import java.util.Scanner; 5
public class NameOfDay {
Friday
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int day = input.nextInt();
switch (day) {
case 0: System.out.println("Sunday"); break;
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;
default: System.out.println("invalid day"); break;
}
}
}
27
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Robert Sedgewick | Kevin Wayne
elice>Week 1-2> Name of day