Aalborg Media LabApr 18, 2023
Software Design
Lecture 6
“Conditionals and Loops”
Aalborg Media LabApr 18, 2023 2
Writing Classes
Chapter 5
• Define the flow of control through a method• Explore boolean expressions• Perform decision making• Examine how to execute statement statements
repetitively• More GUI components and events
Aalborg Media LabApr 18, 2023
Flow of Control
• Unless specified otherwise, the order of statement execution through a method is linear
• Some programming statements modify that order, allowing us to:
– decide whether or not to execute a particular statement, or
– perform a statement over and over, repetitively
• These decisions are based on a boolean expression (also called a condition) that evaluates to true or false
• The order of statement execution is called the flow of control
Aalborg Media LabApr 18, 2023
Conditional Statements
• A conditional statement / selection statements lets us choose which statement will be executed next
• Conditional statements give us the power to make basic decisions
• Java's conditional statements are – the if statement– the if-else statement– the switch statement
Aalborg Media LabApr 18, 2023
• Loops / Repetition statement, allows the execution of statements over and over again
• Java's loop statements are – the while statement– the do statement– the for statement
Loops
Aalborg Media LabApr 18, 2023 6
Boolean Expressions
• Base for all decisions
• A condition often uses one of Java's equality operators or relational operators, which all return boolean results
• … your income is GREATER THAN 100..
== Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
Aalborg Media LabApr 18, 2023 7
Boolean Expressions• Java has three logical operators that produce boolean
results.• Logical operators are used to construct sophisticated
conditions
! Logical NOT !a true if a is false&& Logical AND a && b true if a and b are true|| Logical OR a || b true if a or b are true
Aalborg Media LabApr 18, 2023
Truth Tables• A truth table shows the possible true/false combinations of the
terms
• Since && and || each have two operands, there are four possible combinations of conditions a and b
a b a && b a || b
true true true true
true false false true
false true false true
false false false false
Aalborg Media LabApr 18, 2023 9
The if Statement
if ( condition ) statement;
if is a Javareserved word
The condition must be a boolean expression.It must evaluate to either true or false.
If the condition is true, the statement is executed.If it is false, the statement is skipped.
Aalborg Media LabApr 18, 2023
The if Statementif (sum > MAX) delta = sum - MAX;System.out.println ("The sum is " + sum);
First, the condition is evaluated. The value of sumis either greater than the value of MAX, or it is not.
If the condition is true, the assignment statement is executed.If it is not, the assignment statement is skipped.
Either way, the call to println is executed next.
Aalborg Media LabApr 18, 2023
Logic of an if statement
conditionevaluated
false
statement
true
Age.javaListing 5.1
Aalborg Media LabApr 18, 2023 12
The if-else Statement
• An else clause can be added to an if statement to make an if-else statement
if ( condition ) statement1;else statement2;
• If the condition is true, statement1 is executed; if the condition is false, statement2 is executed
Aalborg Media LabApr 18, 2023
Logic of an if-else statement
conditionevaluated
statement1
true false
statement2
Aalborg Media LabApr 18, 2023 14
Block Statements
• Several statements can be grouped together into a block statement
• A block is delimited by braces : { … }
• A block statement can be used wherever a statement is called for by the Java syntax
• For example, in an if-else statement, the if portion, or the else portion, or both, could be block statement
Aalborg Media LabApr 18, 2023 15
Nested if Statements
• The statement executed as a result of an if statement or else clause could be another if statement
• These are called nested if statements
• An else clause is matched to the last unmatched if (no matter what the indentation implies)
• Braces can be used to specify the if statement to which an else clause belongs
Aalborg Media LabApr 18, 2023
Listing 5.6
if (num1 < num2)if (num1 < num3)
min = num1; else
min = num3;elseif (num2 < num3)
min = num2;else
min = num3;
Aalborg Media LabApr 18, 2023 17
The switch Statement• The switch statement provides another means to
decide which statement to execute next
• The switch statement evaluates an expression, then attempts to match the result to one of several possible cases
• Each case contains a value and a list of statements
• The flow of control transfers to statement associated with the first value that matches
Aalborg Media LabApr 18, 2023
The switch Statement
switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ...
}
switchandcaseare
reservedwords
If expressionmatches value2,control jumpsto here
Aalborg Media LabApr 18, 2023
The switch Statement
• 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 can be appropriate, but usually we want to execute only the statements associated with one case
Aalborg Media LabApr 18, 2023
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
• Though the default case can be positioned anywhere in the switch, usually it is placed at the end
Aalborg Media LabApr 18, 2023
The switch Statement
• The expression of a switch statement must result in an integral type, meaning an int or a char
• It cannot be a boolean value, a floating point value a byte, a short, or a long
• The implicit boolean condition in a switch statement is equality - it tries to match the expression with a value
Aalborg Media LabApr 18, 2023
Repetition Statements• Repetition statements / loops allow us to execute a
statement multiple times
• Like conditional statements, they are controlled by boolean expressions
• Java has three kinds of repetition statements:
– the while loop
– the do loop
– the for loop
Aalborg Media LabApr 18, 2023 23
The while Statement
while ( condition ) statement;
while is areserved word
If the condition is true, the statement is executed.Then the condition is evaluated again.
The statement is executed repeatedly untilthe condition becomes false.
Aalborg Media LabApr 18, 2023
Logic of a while Loop
statement
true
conditionevaluated
false
Aalborg Media LabApr 18, 2023 25
The while Statement• Note that if the condition of a while statement is false
initially, the statement is never executed
• Therefore, the body of a while loop will execute zero or more times
• The statement may be a block of multiple statements
while(condition){statement1;statement2;
}
Aalborg Media LabApr 18, 2023 26
Infinite Loops
• The body of a while loop eventually must make the condition false
• If not, it is an infinite loop, which will execute until the user interrupts the program
• You should always double check to ensure that your loops will terminate normally
Aalborg Media LabApr 18, 2023
Nested Loops• Similar to nested if statements, loops can be
nested as well
• That is, the body of a loop can contain another loop
• Each time through the outer loop, the inner loop goes through its full set of iterations
• See PalindromeTester.java (page 235)
Aalborg Media LabApr 18, 2023
The do Statementdo{ statement;}while ( condition )
do andwhile arereserved
words
The statement is executed once initially,and then the condition is evaluated
The statement is executed repeatedlyuntil the condition becomes false
Aalborg Media LabApr 18, 2023
Logic of a do Loop
true
conditionevaluated
statement
false
Aalborg Media LabApr 18, 2023
The do Statement
• A do loop is similar to a while loop, except that the condition is evaluated after the body of the loop is executed
• Therefore the body of a do loop will execute at least once
Aalborg Media LabApr 18, 2023
The for Statement
for ( initialization ; condition ; increment ) statement;
Reservedword
The initializationis executed once
before the loop begins
The increment portion is executed at the end of each iterationThe condition-statement-increment cycle is executed repeatedly
Aalborg Media LabApr 18, 2023
The for Statement
• A for loop is functionally equivalent to the following while loop structure:
initialization;while ( condition ){ statement; increment;}
Aalborg Media LabApr 18, 2023
Logic of a for loop
statement
true
conditionevaluated
false
increment
initialization
Aalborg Media LabApr 18, 2023
The for Statement
• Like a while loop, the condition of a for statement is tested prior to executing the loop body
• Therefore, the body of a for loop will execute zero or more times
• It is well suited for executing a loop a specific number of times that can be determined in advance
Aalborg Media LabApr 18, 2023
Choosing a Loop Structure• When you can’t determine how many times you
want to execute the loop body, use a while statement or a do statement
– If it might be zero or more times, use a while statement
– If it will be at least once, use a do statement
• If you can determine how many times you want to execute the loop body, use a for statement
Aalborg Media LabApr 18, 2023
Drawing with loops and conditionals• Drawing multiple circles with
different diameter position using for loops
• Enables us to distinguish between various event sources
Aalborg Media LabApr 18, 2023
JButton left = new JButton(“Left”);JButton right = new JButton(“Right”);
ButtonListener bl = new ButtonListener();left.addActionListener(bl);right.addActionListener(bl);…private class ButtonListener implements ActionListener{
public void actionPerformed ( ActionEvent event ){if ( event.getSource() == left )label.setText (“Left button was pushed”);elselabel.setText (“Right button was pushed”);}
}}
Aalborg Media LabApr 18, 2023
Exercises
• Use Dialog Boxes as in Palindrome example (on Homepage) for user interaction (5.13)
• 5.1, 5.2
• 5.13 with GUI (JFrame & Panel ) use checkboxes!!