



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1
2
3
if (condition) { statements to be executed if condition is true }
if (condition) { statements to be executed if condition is true } else { statements to be executed if condition is false }
4
print("Average a list of non-negative integers.\n"); print("Enter one value per line, "+SENTINEL + " to end\n"); int total = 0; int count = 0; int value = readInt("Enter number, "+SENTINEL+" to end: "); while (value != SENTINEL) { if (value != 0) { total += value; count++; } value = readInt("Enter number, "+SENTINEL+" to end:"); }
if (count != 0) println("Average is: " + (double) total / (double) count; else println("No valid values were entered.");
// private static final int SENTINEL = -1;
The AverageList Program - Counting Zeroes
int total = 0; int count = 0; int zeroCount = 0; int value = readInt("Averaging list, "+SENTINEL+" to end:"); while (value != SENTINEL) { if (value != 0) { total += value; count++; } else zeroCount++; value = readInt("Enter number, "+SENTINEL+" to end:"); } if (count != 0) print("Average is: " + (double) total / (double) count + "\n"; else print("No valid values were entered.\n"); println("There were " + zeroCount + " 0’s in data.");
7
Common Forms of the if Statement
if (condition) statement;
if (condition) { statement
.. .more statements... }
8
The if Statement
ENTER
true
false
EXIT
Expression
Boolean (^) ACTION
if (Boolean Expression) { ... ACTION ... } ...
9
Example — if Statement
10
Example — if Statement
When the ACTION is more than one statement, braces are required.
Common Forms — if/else
if (condition) { statements to be executed if condition is true } else { statements to be executed if condition is false }
Common Forms — Cascading if
if (condition 1 ) { statements 1 } else if (condition 2 ) { statements 2 } .. . else if (conditionn− 1 ) { statementsn− 1 } else { statementsn }
19
Cascading if Statements
BE (^1)
BE (^2)
BE (^) n−
BE (^) n
A 1
A 2
An−
An An+
true
false
true
true
false
false
false
false
true
20
Example — Cascading if Statements
// Summing positive and negative values separately
int posSum = 0; int negSum = 0; int n = readInt("Enter value, "+SENTINEL+" to stop: "); while (n != SENTINEL) { if (n > 0) // if n is positive, posSum += n; // add to positive sum else if (n < 0) // if n is negative negSum += n; // add to negative sum else // if n is zero, inform user println("That value was zero"); n = readInt("Enter value, "+SENTINEL+" to stop: "); }
21
Do the else’s Matter?
// Summing positive and negative values separately // and counting number of times zero is entered
int posSum = 0; int negSum = 0; int zeroCount = 0; int n = readInt("Enter value, "+SENTINEL+" to stop: "); while (n != SENTINEL) { if (n > 0) // if n is positive, posSum += n; // add to positive sum if (n < 0) // if n is negative negSum += n; // add to negative sum else // if n is zero zeroCount++; // count it n = readInt("Enter value, "+SENTINEL+" to stop: "); }
22
Which is Required, a while or an if Statement?
The switch Statement
switch ( expr ) { case v 1 : statements executed if expr == v 1 break;
case v 2 : statements executed if expr == v 2 break;
...more case clauses if needed...
default: statements executed if no values match break; }
switch Statement Example
int month = readInt("Enter month (Jan=1): "); switch ( month ) { case 2: println("28 days, 29 in leap years"); break; case 4: case 6: case 9: case 11: println("30 days"); break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: println("31 days"); break; default: println("Illegal month number"); break; }