if( <condition> ) { <result 1>; } else { <result 2>; }In the above example, <result 1> represents code which is executed only if the <condition> is true. If it's false, <result 2> is executed instead. The curly braces are only needed for results that contain more than one line of code.
if( <condition> ) { <result 1>; }In the above format, if <condition> is false, <result 1> is not executed, and the code continues after the closing curly brace (or after the semi-colon if there are no curly braces).
==
for equality comparison! (assignment operator "=" changes the value on the
left, instead of checking to see if values are equal)
<
means less than
<=
means less than or equal to
>
means greater than
>=
means greater than or equal to
!=
means not equal
In-class exercise #1: Write a program to input the names and ages of 2 people. Then output which name is longer, and if the ages are the same or not.
NamesAndAges.java: Solution to above in-class exercise
int age = 17; if (age >= 18) if (age <= 65) System.out.println("working age"); else System.out.println("not working age");What will be output by the above code? Else-statements always go with the most recent if-statement in their block, so the else applies to situations where age is over 65 only. No output is produced. To make the else-statement associate with the first if-statement, curly braces must be used:
int age = 17; if (age >= 18) { if (age <= 65) System.out.println("working age"); } else System.out.println("not working age");Now the text "not working age" will be displayed, but only if age is under 18. If it's over 65, no output is produced. The indentation is different above for readability, but the curly braces, indentation, are what changed the program behavior.
if(age < 13) System.out.println("child"); else if(age < 18) System.out.println("teenager"); else System.out.println("adult");One of the above outputs will always be produced. For better readability, however, the above code is usually written a bit differently:
if(age < 13) System.out.println("child"); else if(age < 18) System.out.println("teenager"); else System.out.println("adult");The meaning of the code is exactly the same, but the indentation more clearly shows that one of the 3 options will always be executed.
if (x < y < z)
if (x < y && y < z)
if(age >= 18 && <= 65)
if(age >= 18 && age <= 65)
!<
because it's not valid. The way to write that is
>=
int num = 5, den = 0; if (den != 0 && num/den > 1) System.out.println(" num is greater than den "); else System.out.println(" den is 0 or it's greater than num");The above code will generate a run-time error (division by zero) if the right-side of the AND expression is evaluated. Fortunately, the left-side will evaluate to false, making the whole expression false no matter what the right expression produces. So the program will never perform the division, and no run-time error will result.
int x = 5; boolean b = x < 10; // store true in b if x is less than 10, false otherwise if (b) // means "if (b == true)" System.out.println("x is less than 10"); // this output will be displayed
-bash-3.2$ java NamesAndAgesPart3 Please enter first person's name: Craig Please enter first person's age: 37 Please enter second person's name: Serafina Please enter second person's age: 7 Craig is between 18 and 65, but Serafina is not. -bash-3.2$ java NamesAndAgesPart3 Please enter first person's name: Tobias Please enter first person's age: 5 Please enter second person's name: Serafina Please enter second person's age: 7 Neither person is between 18 and 65. -bash-3.2$ java NamesAndAgesPart3 Please enter first person's name: Geoff Please enter first person's age: 37 Please enter second person's name: Craig Please enter second person's age: 37 Both people are between 18 and 65 -bash-3.2$
switch(age) { case 16: // if age == 16 System.out.println("you can now drive"); break; case 18: // if age == 18 System.out.println("you can now vote"); break; case 21: // if age == 21 System.out.println("you can now drink"); break; default: // if none of the above match age System.out.println("there is nothing special about your age"); }
First 3 digits of ZIP code | City |
---|---|
900 or 901 | Los Angeles (and area) |
921 | San Diego (and area) |
937 | Fresno (and area) |
941 | San Francisco |
942 | Sacramento |
946 | Oakland (and area) |
ZipCodes.java: Solution to in-class exercise #4 to determine city from ZIP code, using switch
Return to CS 111A page