



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of control statements, specifically focusing on selection statements such as if and switch. It covers the two forms of the if statement, choosing between if and if/else, and examples of their usage. The document also introduces the switch statement and provides examples of its usage.
Typology: Lab Reports
1 / 5
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; }