boolean Data Typeif Statementsif/else Statementsif Statementsif Statements?if/ else if StatementsHomework 5 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
return statement
which has the following format
return EXPRESSION
java interpreter comes to a return statement
it does the following
return statementvoidreturn statement
provide a value to the method call
ls tmp
public static void main(String[] args)
String[] means it is an array of stringsargs[0]
public class CommandLineInput {
public static void main (String[] args){
System.out.println(args[0]);
}
}
$ java CommandLineInput Mercury Mercury $ java CommandLineInput Venus Venus $ java CommandLineInput Earth Earth $ java CommandLineInput Mars Mars
Integer.parseInt(input);
public class Double {
public static void main (String[] args){
String input;
input = args[0];
System.out.println("input: " + input);
int number;
number = Integer.parseInt(input);
System.out.println("number: " + number);
System.out.println("number * 2: " + number * 2);
}
}
$ java Double 3 input: 3 number: 3 number * 2: 6 $ java Double 4 input: 4 number: 4 number * 2: 8 $ java Double 5 input: 5 number: 5 number * 2: 10
java interpreter comes across a conditional
statement it has to make a decision
if statementif/else statement if/else if statementboolean Data Typeboolean can only have the values true or falsetruefalseif statements evaluate boolean expressions
BOOLEAN_VALUE RELATIONAL_OPERATOR BOOLEAN_VALUE
| Operator | Meaning | Example | Value |
|---|---|---|---|
| == | equal to | 2 + 2 == 4 | true |
| != | not equal to | 3.2 != 4.1 | true |
| < | less than | 4 < 3 | false |
| > | greater than | 4 > 3 | true |
| <= | less than or equal to | 2 <= 0 | false |
| >= | greater than or equal to | 2.4 >= 1.6 | true |
if Statementsif statementif statement contains other statements inside it
if (CONDITION){
STATEMENT
STATEMENT
...
}
public class Odd {
public static void main (String[] args){
for (int num = 1; num <= 10; num++){
odd(num);
}
}
public static void odd (int number){
if (number % 2 == 1 ){
System.out.println(number + " is odd ");
}
}
}
$ java Odd 1 is odd 3 is odd 5 is odd 7 is odd 9 is odd
if/else Statementsif statement the statements inside the code block are run if
the boolean expression in the header is true
if statement there are two choices
if/else statement you have two choices
ifelseif/else statement has the following format
if (CONDITION){
STATEMENT;
STATEMENT;
...
} else {
STATEMENT;
STATEMENT;
...
}
public class OddEven {
public static void main (String[] args){
for (int num = 1; num <= 10; num++){
odd_even(num);
}
}
public static void odd_even (int number){
if (number % 2 == 1 ){
System.out.println(number + " is odd ");
} else {
System.out.println(number + " is even ");
}
}
}
$ java OddEven 1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd 10 is even
if Statementsif statements are
compound statements
if statement is a statementif statement inside an if statement for statement inside a for statement100 - 90 A 89 - 80 B 79 - 70 C 69 - 60 D 59 - 0 F
public class Grades {
public static void main (String[] args){
public static void main (String[] args){
String input = args[0];
int score = Integer.parseInt(input);
System.out.println("A score of " + score + " gets a grade of " + grade(score));
}
public static String grade(int score){
if (score >= 90){
return "A";
} else {
if (score >= 80) {
return "B";
} else {
if (score >= 70){
return "C";
} else {
if (score >= 60){
return "D";
} else {
return "F";
}
}
}
}
}
}
$ java Grades 91 A 83 B 78 C 65 D 59 F
if Statements?100 - 93 A 92 - 90 A- 89 - 86 B+ 85 - 83 B 82 - 80 B- 79 - 76 C+ 75 - 73 C 72 - 70 C- 69 - 66 D+ 65 - 63 D 62 - 60 D- 59 - 0 F
if/else statement only has two branchesif/ else if Statementsif/else if statement gives us
many different paths through the code
if (BOOLEAN_EXPRESSION_1) {
STATEMENT;
...
} else if (BOOLEAN_EXPRESSION_2) {
STATEMENT;
...
...
[ else {
STATEMENT;
...
{
if/else if statements
it would look like this
public class Grades2 {
public static void main (String[] args){
String input = args[0];
int score = Integer.parseInt(input);
System.out.println("A score of " + score + " gets a grade of " + grade(score));
}
public static String grade(int score){
if (score >= 90){
return "A";
} else if (score >= 80) {
return "B";
} else if (score >= 70){
return "C";
} else if (score >= 60){
return "D";
} else {
return "F";
}
}
}
if/else if statements
if/else if
clauses as you like
else clause is optionalif/else if clauseselse clause is optional