Control flow in Java
Madhavan Mukund
[Link]
Programming Concepts using Java
Week 2
Control flow
Program layout
Statements end with semi-colon
Blocks of statements delimited by braces
Madhavan Mukund Control flow in Java Programming Concepts using Java 2/9
Control flow
Program layout
Statements end with semi-colon
Blocks of statements delimited by braces
Conditional execution
if (condition) { ... } else { ... }
Madhavan Mukund Control flow in Java Programming Concepts using Java 2/9
Control flow
Program layout
Statements end with semi-colon
Blocks of statements delimited by braces
Conditional execution
if (condition) { ... } else { ... }
Conditional loops
while (condition) { ... }
do { ... } while (condition)
Madhavan Mukund Control flow in Java Programming Concepts using Java 2/9
Control flow
Program layout
Statements end with semi-colon
Blocks of statements delimited by braces
Conditional execution
if (condition) { ... } else { ... }
Conditional loops
while (condition) { ... }
do { ... } while (condition)
Iteration
Two kinds of for
Madhavan Mukund Control flow in Java Programming Concepts using Java 2/9
Control flow
Program layout
Statements end with semi-colon
Blocks of statements delimited by braces
Conditional execution
if (condition) { ... } else { ... }
Conditional loops
while (condition) { ... }
do { ... } while (condition)
Iteration
Two kinds of for
Multiway branching – switch
Madhavan Mukund Control flow in Java Programming Concepts using Java 2/9
Conditional execution
if (c) {...} else {...}
else is optional public class MyClass {
Condition must be in parentheses
...
If body is a single statement, braces are not
needed public static int sign(int v) {
if (v < 0) {
No elif, à la Python return(-1);
Indentation is not forced } else if (v > 0) {
Just align else if return(1);
} else {
Nested if is a single statement, no separate return(0);
braces required }
}
No surprises
Aside: no def for function definition }
Madhavan Mukund Control flow in Java Programming Concepts using Java 3/9
Conditional loops
while (c) {...}
Condition must be in parentheses public class MyClass {
If body is a single statement, braces are not
...
needed
public static int sumupto(int n) {
int sum = 0;
while (n > 0){
sum += n;
n--;
}
return(sum);
}
}
Madhavan Mukund Control flow in Java Programming Concepts using Java 4/9
Conditional loops
while (c) {...}
Condition must be in parentheses public class MyClass {
If body is a single statement, braces are not
...
needed
do {...} while (c) public static int sumupto(int n) {
int sum = 0;
Condition is checked at the end of the loop int i = 0;
At least one iteration
do {
sum += i;
i++;
} while (i <= n);
return(sum);
}
Madhavan Mukund Control flow in Java } Programming Concepts using Java 4/9
Conditional loops
while (c) {...}
Condition must be in parentheses public class MyClass {
If body is a single statement, braces are not
...
needed
do {...} while (c) public static int sumupto(int n) {
int sum = 0;
Condition is checked at the end of the loop int i = 0;
At least one iteration
Useful for interactive user input do {
sum += i;
do { i++;
read input; } while (i <= n);
} while (input-condition);
return(sum);
}
Madhavan Mukund Control flow in Java } Programming Concepts using Java 4/9
Iteration
for loop is inherited from C
for (init; cond; upd) {...}
init is initialization
cond is terminating condition
upd is update
Madhavan Mukund Control flow in Java Programming Concepts using Java 5/9
Iteration
for loop is inherited from C
for (init; cond; upd) {...} public class MyClass {
init is initialization ...
cond is terminating condition
public static int sumarray(int[] a) {
upd is update
int sum = 0;
Intended use is int n = [Link];
int i;
for(i = 0; i < n; i++){...}
for (i = 0; i < n; i++){
sum += a[i];
}
return(sum);
}
Madhavan Mukund }
Control flow in Java Programming Concepts using Java 5/9
Iteration
for loop is inherited from C
for (init; cond; upd) {...} public class MyClass {
init is initialization ...
cond is terminating condition
public static int sumarray(int[] a) {
upd is update
int sum = 0;
Intended use is int n = [Link];
int i;
for(i = 0; i < n; i++){...}
Completely equivalent to for (i = 0; i < n; i++){
sum += a[i];
i = 0;
}
while (i < n) {
i++;
return(sum);
}
}
Madhavan Mukund }
Control flow in Java Programming Concepts using Java 5/9
Iteration
Intended use is
for(i = 0; i < n; i++){...} public class MyClass {
Completely equivalent to
...
i = 0;
while (i < n) { public static int sumarray(int[] a) {
i++; int sum = 0;
} int n = [Link];
int i;
for (i = 0; i < n; i++){
sum += a[i];
}
return(sum);
}
Madhavan Mukund }
Control flow in Java Programming Concepts using Java 6/9
Iteration
Intended use is
for(i = 0; i < n; i++){...} public class MyClass {
Completely equivalent to
...
i = 0;
while (i < n) { public static int sumarray(int[] a) {
i++; int sum = 0;
} int n = [Link];
int i;
However, not good style to write for
instead of while for (i = 0; i < n; i++){
sum += a[i];
}
return(sum);
}
Madhavan Mukund }
Control flow in Java Programming Concepts using Java 6/9
Iteration
Intended use is
for(i = 0; i < n; i++){...} public class MyClass {
Completely equivalent to
...
i = 0;
while (i < n) { public static int sumarray(int[] a) {
i++; int sum = 0;
} int n = [Link];
However, not good style to write for for (int i = 0; i < n; i++){
instead of while sum += a[i];
}
Can define loop variable within loop
The scope of i is local to the loop return(sum);
An instance of more general local }
scoping allowed in Java
}
Madhavan Mukund Control flow in Java Programming Concepts using Java 6/9
Iterating over elements directly
Java later introduced a for in the style of
Python
for x in l:
do something with x
Madhavan Mukund Control flow in Java Programming Concepts using Java 7/9
Iterating over elements directly
Java later introduced a for in the style of
Python public class MyClass {
for x in l:
do something with x ...
Again for, different syntax public static int sumarray(int[] a) {
for (type x : a) int sum = 0;
do something with x; int n = [Link];
}
for (int v : a){
sum += v;
}
return(sum);
}
Madhavan Mukund }
Control flow in Java Programming Concepts using Java 7/9
Iterating over elements directly
Java later introduced a for in the style of
Python public class MyClass {
for x in l:
do something with x ...
Again for, different syntax public static int sumarray(int[] a) {
for (type x : a) int sum = 0;
do something with x; int n = [Link];
}
for (int v : a){
It appears that loop variable must be sum += v;
declared in local scope for this version of }
for
return(sum);
}
Madhavan Mukund }
Control flow in Java Programming Concepts using Java 7/9
Multiway branching
switch selects between different public static void printsign(int v) {
options switch (v) {
case -1: {
[Link]("Negative");
break;
}
case 1: {
[Link]("Positive");
break;
}
case 0: {
[Link]("Zero");
break;
}
}
}
Madhavan Mukund Control flow in Java Programming Concepts using Java 8/9
Multiway branching
switch selects between different public static void printsign(int v) {
options switch (v) {
case -1: {
Be careful, default is to “fall [Link]("Negative");
through” from one case to the next break;
}
Need to explicitly break out of
case 1: {
switch
[Link]("Positive");
break available for loops as well break;
Check the Java documentation }
case 0: {
[Link]("Zero");
break;
}
}
}
Madhavan Mukund Control flow in Java Programming Concepts using Java 8/9
Multiway branching
switch selects between different public static void printsign(int v) {
options switch (v) {
case -1: {
Be careful, default is to “fall [Link]("Negative");
through” from one case to the next break;
}
Need to explicitly break out of
case 1: {
switch
[Link]("Positive");
break available for loops as well break;
Check the Java documentation }
case 0: {
Options have to be constants [Link]("Zero");
Cannot use conditional expressions break;
}
}
}
Madhavan Mukund Control flow in Java Programming Concepts using Java 8/9
Multiway branching
switch selects between different public static void printsign(int v) {
options switch (v) {
case -1: {
Be careful, default is to “fall [Link]("Negative");
through” from one case to the next break;
}
Need to explicitly break out of
case 1: {
switch
[Link]("Positive");
break available for loops as well break;
Check the Java documentation }
case 0: {
Options have to be constants [Link]("Zero");
Cannot use conditional expressions break;
}
Aside: here return type is void }
Non-void return type requires an }
appropriate return value
Madhavan Mukund Control flow in Java Programming Concepts using Java 8/9
Summary
Program layout: semi-colons, braces
Conditional execution: if, else
Conditional loops: while, do-while
Iteration: two kinds of for
Local declaration of loop variable
Multiway branching: switch
break to avoid falling through
Madhavan Mukund Control flow in Java Programming Concepts using Java 9/9