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 … …
ifWrite a program that accepts a name and midterm grade and issues a warning if the grade is below 70.
import java.util.*;
public class SimpleIf {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter last name: ");
String lastName = scanner.next();
System.out.print("Enter first name: ");
String firstName = scanner.next();
System.out.print("Enter midterm: ");
int midterm = scanner.nextInt();
if (midterm < 70)
System.out.println(lastName + ", " + firstName + " better shape up!");
}
}
if without else
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
stdout (System.out)
Enter last name: Enter first name: Enter midterm: Weiss, Gerald better shape up!
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
stdout (System.out)
Enter last name: Enter first name: Enter midterm:
if/elseWrite a program that accepts a name, midterm, and final exam grades and prints out a pass/fail grade.
import java.util.*;
public class SimpleIfElse {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter last name: ");
String lastName = scanner.next();
System.out.print("Enter first name: ");
String firstName = scanner.next();
System.out.print("Enter midterm: ");
int midterm = scanner.nextInt();
System.out.print("Enter final: ");
int finalExam = scanner.nextInt();
double average = (midterm + finalExam) / 2.0;
if (average >= 60)
System.out.println(lastName + ", " + firstName + " " + "passes the course");
else
System.out.println(lastName + ", " + firstName + " " + "fails the course");
}
}
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:
;)
;'s because they do not contain
any statements within themselves, but the if itself, and the else don't take a ; because the contain the true and false statement respectively.
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 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 |
import java.util.*;
public class Grader {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Midterm: ");
int midterm = scanner.nextInt();
System.out.print("Final: ");
int finalExam = scanner.nextInt();
double average = (midterm + finalExam) / 2.0;
String grade;
if (average >= 90)
grade = "A";
else if (average >= 80)
grade = "B";
else if (average >= 70)
grade = "C";
else if (average >= 60)
grade = "D";
else
grade = "F";
System.out.println("average: " + average + " grade: " + grade);
}
}
F
elses?
else if (average < 60) grade = "F";
if (…) if (…) … else … else if (…) … else …
if (…) 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'.