Lec 5
Lec 5
3
4
Control Structures
• Sequential execution
– Program statements execute one after the other
• Transfer of control
– Three control structures can specify order of statements
• Sequence structure (default)
• Selection structure
• Repetition structure
• Activity diagram
– Models the workflow (flowchart)
• Action-state symbols
• Transition arrows
5
if ( condition )
statement;
8
Boolean Expressions
• A condition often uses one of Java's equality operators or relational
operators, which all return boolean results:
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
• Note the difference between the equality operator (==) and the
assignment operator (=)
The if Statement
• An example of an if statement:
if (sum > MAX)
delta = sum - MAX;
System.out.println ("The sum is " + sum);
• First the condition is evaluated -- the value of sum is either greater than
the value of MAX, or it is not
5-10
Logical Operators
• Boolean expressions can also use the following logical operators:
! Logical NOT
&& Logical AND
|| Logical OR
5-12
if…else Selection Statement
• Perform action only when condition is true
• Perform different specified action when condition is false
• Conditional operator (?:)
• Nested if…else selection structures
{
System.out.println (“Failed”);
}
if…else double-selections statement activity diagram.
13
The if-else Statement condition
evaluated
• An else clause can be added to an if statement to
make an if-else statement true false
if ( condition )
statement1; statement1 statement2
else
statement2;
• Example:
System.out.println (studentGrade >= 60 ? “Passed”: “Failed”);
• If studentGrade is not greater than nor equal 60, then “Failed" is printed
5-19
Nested If statements
• Since an “If” statement is a statement, it can appear inside
another if statement.
if (condition1)
if (condition2)
statement;
if (condition1)
statement1;
else if (condition2)
statement2;
else
statement3;
The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
if (condition1){
// block of code to be executed if condition1 is true
}
else if (condition2){
// block of code to be executed if the condition1 is false and condition2 is true
}
else {
// block of code to be executed if the condition1 is false and condition2 is false
}
• Execution transfers to statement list associated with the first value that
matches
The switch statement provides another way
to decide which statement to execute next.
5-25
The switch Statement
• Often a break statement is used as the last statement in each
case's statement list.
• A break statement causes control to transfer to the end of the
switch statement.
• If a break statement is not used, the flow of control will continue
into the next case.
• Sometimes this may be appropriate, but often we want to execute
only the statements associated with one case.
The switch Statement
• An example of a switch statement:
switch (option)
{
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
default:
System.out.println(“default.. ");
}
5-27
The switch Statement
• A switch statement can have an optional default case.
• The default case has no associated value and simply uses the
reserved word default.
• If the default case is present, control will transfer to it if no other
case value matches.
• If there is no default case, and no other value matches, control
falls through to the statement after the switch.
5-28
The switch Statement
• The expression of a switch statement must result in an integral
type, meaning an integer (byte, short, int, long) or a char
• It cannot be a boolean value or a floating-point value (float or
double)
• The implicit boolean condition in a switch statement is equality
• You cannot perform relational checks with a switch statement
5-29
Example: int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Looking forward to the Weekend");
} // Outputs "Thursday" (day 4)
Shorthand Operators
Shorthand Operators
• Many operations are very commonly used
x = x + 1;
sum = sum + x;
count = count + 1;
The Increment and Decrement Operators
• The increment and decrement operators can be applied in prefix
(before the variable) or postfix (after the variable) form
• When used alone in a statement, the prefix and postfix forms are
basically equivalent. That is,
count++; //postfix
is equivalent to
++count; //prefix
The Increment and Decrement Operators
• When used in a larger expression, the prefix and postfix forms
have a different effect.
• In both cases the variable is incremented (decremented).
• But the value used in the larger expression depends on the form
Expressions Operation Value Of expression
count++ add 1 old value
++count add 1 new value
count-- subtract 1 old value
--count subtract 1 new value
The Increment and Decrement Operators
• If count currently contains 45, then
total = count++;
total = ++count;