println
we will be covering later
println
for printf
in sections
3.7-3.9 — that should help you get through it
if
statementif
statement) and for
loop
==
— Equality
!=
— Inequality
<
>
<=
>=
equals
method (of the String class), rather than
==
. (There are other methods for greater, less, etc — we'll cover them later):
String s1, s2; … s1 = … … s2 = … … if (s1.equals(s2)) // NOT s1 == s2 … …
if
Write a program that accepts a name and midterm grade and issues a warning if the grade is below 70.
if
without else
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
if/else
Write a program that accepts a name, midterm, and final exam grades and prints out a pass/fail grade.
if
without else
if/else
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
;
)
if (temperature > 98.6); System.out.println("You have a fever");
else
if (temperature > 98.6)
System.out.println("You have a fever");
else;
System.out.println("You're fine … get to school");
;
for (…) ;i.e., all the action is happening in the loop header (we'll have to wait for an example)
for
Loopfor
loop header
for (int i = 1; i <= 3; i = i + 1) // loop header … // loop body
int i = 1
i
in this case)
i <= 3
i = i + 1
// User controls number of times through loop System.out.println("How many times do you want to print"I will not talk in class? "); int n = scanner.nextInt(); for (int i = 1; i <= n; i = i + 1) System.out.println("I will not talk in class!");
for (int i = 1; i <= 100 i = i + 2) // odd numbers System.out.println(i);
for (int i = 2; i <= 100 i = i + 2) // even numbers System.out.println(i);
for (int i = 1; i <= n; ) System.out.println("Hello");
for (int i = 1; ; i = i + 1) System.out.println("Hello");
for (int i = 1; i <= 10; i = i - 1) System.out.println("Hello");???
for (int i = 1; i <= 3; i = i + 1);
System.out.println("Hello");
if
Part
1. if (<condition1>)
2. if (<condition2>>)
3. statement
The True-Stmt is executed only when all the conditions are true; i.e., the conditions act as a 'gatekeeper' for the True-Stmt
In order to drive, one must be a resident of New York, and be 18 or older. Write a conditional that prints "Can drive" to the screen under the proper conditions.
if (stateOfResidence.equals("NY")) if (age >= 18) System.out.println("Can drive);Suppose we also want to print 'Can't drive'
else
Part1. if (<condition1>) 2. … 3. else 4. if (<condition2>) 5. statement
if/else
: Assigning letter grades
Write a program that prompts for a midterm and final, calculates the average and assigns a letter grade based upon the following table:
Average | Grade |
---|---|
90-100 | A |
80-89 | B |
70-79 | C |
60-69 | D |
59 and below | F |
if (…) if (…) … else … else if (…) … else …
if (…) … else if (…) … else if (…) … else …
if (…) … if (…) … if (…) …
Program 3.3 Using a sequence of nested if/else's, find the maximum of three integers
Program 3.4 Using a waterfall approach, find the maximum of three integers
switch
StatementNotice the logic of expanding from the single character to the expanded string:
if (category == 'F') expanded = "Fiction"; else if (category == 'B') expanded = "Biography"; else if (category == 'N') expanded = "Non-fiction"; else if (category == 'A') expanded = "Anthology"; else if (category == 'S') expanded = "Short Stories"; else if (category == 'J') expanded = "Juvenile"; else expanded = "Unknown category";It is often the case that we have such a form of conditional, i.e.,:
if (var == some value) ... else if (var == a second valuevalue) ... else if (var == a third value) ... ... else ...
This construct may be replaced with a switch statement:
switch (var) { case some value: ... break; case a second value: ... break; case a third value: ... break; ... default: ... break; }How it works:
switch
, the value of var
is compared against the various cases.
var
matches one of the cases, the code for
that case is executed, up to the break
statement.
var
, the code
in the default
block is executed.
switch
, the book category code then becomes:
switch (category) { case 'F': expanded = "Fiction"; break; case 'B': expanded = "Biography"; break; case 'N': expanded = "Non-fiction"; break; case 'A': expanded = "Anthology"; break; case 'S': expanded = "Short Stories"; break; case 'J': expanded = "Juvenile"; break; default: expanded = "Unknown category"; break; }Note: The expression of the
switch
statement is limited to certain types, int
, char
, and
string
. Expressions of type double
are not legal for 'switch'.