Choice – if-else

Choice – if-else
Aim: Select during execution which one of two statements to execute,
depending on variable values that may differ between executions
Means: if-else statement
1
if-else
import java.util.∗;
public class AccountChoice {
Scanner sc = new Scanner(System.in);
void handleBalance() {
double balance; int rate, debitRate;
rate = 3; debitRate = 5;
System.out.println(”Type amount for balance.”);
balance = sc.nextDouble();
if (balance>=0) { // guard
balance = balance + (rate ∗ balance)/100;
} else {
balance = balance + (debitRate ∗ balance)/100;
}
System.out.println(”New balance is ” + balance + ”.”);
} }
2
if-else syntax template
Choice between two alternatives:
if ( boolean expression ) {
statements1
} else {
statements2
}
3
nested if-else syntax template
Choice between more than two alternatives:
if ( boolean expression ) {
statements1
} else if ( boolean expression ) {
statements2
...
} else {
statements
}
4
switch
Choice between alternatives identified by a single value: switch can
be used
import java.util.∗;
public class AccountSwitch {
Scanner sc = new Scanner(System.in);
void handleBalance() {
double balance; // balance of account in some currency
char choice; // choice of services
balance = 0;
System.out.println(”Type choice of action (display balance: ”+
”’d’ or ’b’, add: ’a’, withdraw: ’w’).”);
choice = sc.next().charAt(0);
...
5
switch
... switch (choice) {
case ’d’:
case ’b’:
SOP(”Current balance is ” + balance + ”.”);
break;
case ’a’:
SOP(”Type add amount”); bal = balance + sc.nextDouble();
break;
case ’w’:
SOP(”Type withdraw amount”); balance = balance − sc.nextDouble();
break;
default:
SOP(”Illegal choice.”);
break;
}
System.out.println(”New balance is ” + balance + ”.”);
}
}
6
switch syntax template
switch( controlling expression ) {
case value-1 :
statements-1
break;
...
case value-n1 :
...
case value-nm :
statements-n
break;
case default:
statements-default
}
7
Repetition – while
Aim: Execute some statements more than once, depending on variable
values that may differ between and change during executions
Means: while statement.
Repeat part of a program, the loop body, according to a guard
• inspect the guard (boolean expression) before execution of the body
• if true, execute the body, including adapting the guard value
• as long as (while) the value of the guard is true, else terminate
8
Fixed number of repetitions
public class Hello3Times {
// outputs Hello 3 times
void showHello() {
int index; // guard variable
index = 0; // set guard variable
while (index < 3) { //check guard
System.out.println(”Hello”);
index = index + 1; // update guard variable
}
}
}
9
Syntax template while
while ( boolean expression ) {
statements
}
10
Chosen number of repetitions
import java.util.∗;
public class HelloNTimes {
Scanner sc = new Scanner(System.in);
// outputs Hello
void showHello() {
int times, index; // number of times and guard variable
System.out.println(”Choose number of times”);
times = sc.nextInt(); // choose number of times
index = 0; // set guard variable
while (index < times) { // guard
System.out.println(”Hello”);
index = index + 1; //update guard variable
}
}
}
11
Repetition till stopped in step
import java.util.∗;
public class HelloTillStop {
Scanner sc = new Scanner(System.in);
// outputs Hello
void showHello() {
char choice; //guard variable
System.out.println(”For Hello type H, to stop other char”);
choice = sc.next().charAt(0); // set guard variable
while (choice == ’H’) { // check guard
System.out.println(”Hello”);
choice = sc.next().charAt(0);; //update guard variable
}
}
}
12
Repetition till interrupted?
Can you program repeated execution of the body till the user interrupts?
13
While (and int) is all we need ...
Church-Turing thesis: All computable functions can be computed
through labelling statements plus conditional goto <label>
While (+ int) theorem: goto can be expressed with while (and int)
• goto can be expressed with while and if-else (try)
• if-else can be expressed with while (try)
• Data can be encoded in one int variable (don’t try ;-))
HUMANS NEED STRUCTURE (INCLUDING RE-USE) - provided
by OO; rest of the course
14
Variant: do-while
while for 0 or more iterations, do-while for 1 or more
System.out.println(”For Hello type H, to stop other char”);
choice = sc.next().charAt(0); // set guard variable
while (choice == ’H’) { // guard
System.out.println(”Hello”);
System.out.println(”For Hello type H, to stop other char”);
choice = sc.next().charAt(0);; //update guard variable
}
do { // at least once!
System.out.println(”Hello”);
System.out.println(”For Hello type H, to stop other char”);
choice = sc.next().charAt(0);; //update guard variable
} while (choice == ’H’) // guard
15
Syntax template while and do-while
while ( boolean expression ) {
statements
}
do {
statements
} while ( boolean expression );
16
do-while expressed with while
do {
Body
} while ( Guard );
and
Body
while ( Guard ) {
Body
}
are equivalent
17
while expressed with do-while
while ( Guard ) {
Body
}
and
do {
if ( Guard ) {
Body
}
} while ( Guard );
are equivalent
18
Variant: for statement (fixed number example)
public class HelloFor3Times {
// No guard variable declared here!
// outputs Hello
void showHello() {
for(int index = 0; index < 3; index = index + 1) {
//declare and set; check; update guard (variable)
System.out.println(”Hello”);
}
}
}
for also equivalent to while
19
Syntax template while, do-while and for
while ( boolean expression ) {
statements
}
do {
statements
} while ( boolean expression );
for ( initialization ; guard ; update ) {
statements
}
20
Summation
Write a program that
computes the sum of a sequence of non-zero integers, of unknown length,
ending with 0
Example:
> Type sequence of numbers, end with 0
> 9
> 22
> -10
> 0
Sum = 21
21
Running sum
import java.util.∗;
public class RunningSum {
Scanner sc = new Scanner(System.in);
// computes sum
void add() {
int number, sum; // number and sum (also guard variable)
sum = 0;
System.out.println(”Type sequence of numbers, end with 0”);
number = sc.nextInt();
while (number != 0) {
sum = sum + number; // running sum
number = sc.nextInt();
}
System.out.println(”Sum is ” + sum);
}
}
22
Averaging
Write a program that
computes the average of a sequence of non-zero integers, of unknown
length, closed by a 0
Example:
> Type sequence of numbers, end with 0
> 9
> 22
> -10
> 0
Average = 7
23
Averaging
import java.util.∗;
public class Averager {
Scanner sc = new Scanner(System.in);
void average() {
int number, sum, counter; double average; // input counter
sum = 0; counter = 0;
System.out.println(”Type sequence of numbers, end with 0”);
number = sc.nextInt();
while (number != 0) {
sum = sum + number; counter = counter + 1;// counts inputs
number = sc.nextInt();
}
System.out.println(”Average = ” + (sum ∗ 1.0/counter));
}
}
24
Above averaging
Write a program that
computes for a sequence of non-zero integers, of unknown length, closed
by a 0, how many inputs are above average
Example:
> Type sequence of numbers, end with 0
> 9
> 22
> -10
> 0
Average is 7
Above average: 2
25