if
StatementDecides whether or not a section of code executes.
boolean
to decideif (boolean expression is true)
execute next statement
If statements can be modeled as a flow chart.
This—
if (average > 95)
= 'A'; grade
—is functionally equivalent to:
if (average > 95) grade = 'A';
if
StatementsCurly braces are used to group conditionally executed statements.
e.g.,
if (expression)
{
;
statement1;
statement2}
Note: Some trick questions will leave out the curly braces but indent blocks to look like they are conditionally executed when only the first line is conditional.
Boolean Expression: Any variable or calculation that results in a true or false condition.
Typically, we’ll use relational operators in boolean expressions because they return true or false.
Relational Operator | Meaning |
---|---|
> | is greater than |
< | is less than |
>= | is greater than or equal to |
<= | is less than or equal to |
== | is equal to |
!= | is not equal to |
Reminder:
=
is the assignment operator,==
is the comparison operator!
Flag: boolean
variable that monitors some condition in a program
true
boolean isPrime = true;
if (5 % 4 == 0)
= false;
isPrime if (5 % 3 == 0)
= false;
isPrime if (5 % 2 == 0)
= false;
isPrime if (isPrime)
System.out.println("5 is prime");
Characters can be tested with relational operators.
char x = 'A';
if (x < 'B')
System.out.println("A is less than B");
if (expression)
;
statementTrueelse
; statementFalse
Note: Curly braces not required if there is only one statement
- But they can help with readability
if
statementsNested if
Statement: An if
statement inside another if statement (single or block, doesn’t matter)
if
statement is true.if (coldOutside)
{
if (snowing)
{
wearParka();
}
else
{
wearJacket();
}
}
else
{
wearShorts();
}
if-else-if
Statementsif
can become very complex.if (expression)
{
// a
}
else if (expression)
{
// b
}
else if (expression)
{
// c
}
else{
// catch whatever
}
Note how only one block can be executed
Two logical (AND, OR) and one unary (NOT) operators are provided in Java.
Operator | Meaning |
---|---|
&& | AND |
|| | OR |
! | NOT |
System.out.printf
MethodPrint formatted string.
%[flags][width][.precision]conversion
Flags
: Padding with zeros, left-justifying, comma separatorsWidth
: Minimum field width for value.precision:
Number of decimal places number should be rounded toConversion
: f
for floating-point, d
for decimal integer, s
for stringExamples:
double grossPay = 874.12;
System.out.printf("Your pay is %f \n", grossPay);
%f
: Format specifier. Indicates that a floating-point value will be used.double grossPay = 874.12111;
System.out.printf("Your pay is %.2f \n", grossPay);
%.2f
: Format specifier. Indicates that a floating-point value will be printed, rounded to two decimal placed.double grossPay = 5874.12111;
System.out.printf("Your pay is %,.2f \n", grossPay);
%,.2f
: Format specifier. Indicates that a floating-point value will be printed, rounded to two decimal placed, with commas separating thousandsString name = "Ringo";
System.out.printf("Your name is %s", name);
%s
: Format specifier. Indicates that a string will be printed.int number = 9;
System.out.printf("The value is %6d", number);
%6d
: Format specifier. Indicates that an integer will appear in a field that is 6 spaces wide.String.format
Works exactly like System.out.printf
, except it returns a reference to the formatted string instead of printing to the console.
String
ObjectsCommon Mistake: Trying to compare two
String
objects with relational operators (==
)
- Objects are just pointers to their location in RAM; you need to use object methods to compare strings.
String name1 = "john", name2 = "John";
String name3 = new String("John");
String name4 = new String("John");
name1
and name2
share the same memory address; name3
and name4
each have their own unique pointers.Common methods:
equals()
equalsIgnoreCase()
compareTo()
compareToIgnoreCase()
? Value1 : Value2 BooleanExpression
if-else
BooleanExpression
is true
, value of conditional expression is Value1
BooleanExpression
is false
, value of conditional expression is Value2
= x > y ? 10 : 5;
z // functional equivalent:
if (x>y)
= 10;
z else
= 5; z
switch
StatementAllows you to use value of variable/expression to determine how a program will branch.
switch (SwitchExpression)
{
case CaseExpression;
break;
case CaseExpression;
break;
case CaseExpression;
break;
default:
// blah
}
SwitchExpression
can be a char
, byte
, short
, int
, long
, or String
SwitchExpressions
matched the CaseExpression
, the statements between the colon and break
will be executed.case
Statementbreak
ends the case
break
is optional, this allows the program to “fall through” statementsdefault
section is optional, executed if no CaseExpression
matches