aalborg media lab 23-jun-15 software design lecture 6 “conditionals and loops”

38
Aalborg Media Lab Mar 25, 2022 Software Design Lecture 6 “Conditionals and Loops”

Post on 21-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

Aalborg Media LabApr 18, 2023

Software Design

Lecture 6

“Conditionals and Loops”

Page 2: Aalborg Media Lab 23-Jun-15 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

Page 3: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 4: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 5: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 6: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and 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

Page 7: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 8: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 9: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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.

Page 10: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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.

Page 11: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

Aalborg Media LabApr 18, 2023

Logic of an if statement

conditionevaluated

false

statement

true

Age.javaListing 5.1

Page 12: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 13: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

Aalborg Media LabApr 18, 2023

Logic of an if-else statement

conditionevaluated

statement1

true false

statement2

Page 14: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 15: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 16: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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;

Page 17: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 18: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 19: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 20: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 21: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 22: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 23: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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.

Page 24: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

Aalborg Media LabApr 18, 2023

Logic of a while Loop

statement

true

conditionevaluated

false

Page 25: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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;

}

Page 26: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 27: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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)

Page 28: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 29: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

Aalborg Media LabApr 18, 2023

Logic of a do Loop

true

conditionevaluated

statement

false

Page 30: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 31: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 32: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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;}

Page 33: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

Aalborg Media LabApr 18, 2023

Logic of a for loop

statement

true

conditionevaluated

false

increment

initialization

Page 34: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 35: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 36: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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

Page 37: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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”);}

}}

Page 38: Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”

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!!