Download presentation
Presentation is loading. Please wait.
1
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
2
Aalborg Media Lab 23-Jun-152 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
3
Aalborg Media Lab 23-Jun-15 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
4
Aalborg Media Lab 23-Jun-15 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
5
Aalborg Media Lab 23-Jun-15 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
6
Aalborg Media Lab 23-Jun-156 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
7
Aalborg Media Lab 23-Jun-157 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
8
Aalborg Media Lab 23-Jun-15 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 aba && ba || b true false true falsetruefalsetrue false
9
Aalborg Media Lab 23-Jun-159 The if Statement if ( condition ) statement; if is a Java reserved 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.
10
Aalborg Media Lab 23-Jun-15 The 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. 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.
11
Aalborg Media Lab 23-Jun-15 Logic of an if statement condition evaluated false statement true Age.java Listing 5.1
12
Aalborg Media Lab 23-Jun-1512 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
13
Aalborg Media Lab 23-Jun-15 Logic of an if-else statement condition evaluated statement1 true false statement2
14
Aalborg Media Lab 23-Jun-1514 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
15
Aalborg Media Lab 23-Jun-1515 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
16
Aalborg Media Lab 23-Jun-15 Listing 5.6 if (num1 < num2) if (num1 < num3) min = num1; else min = num3; else if (num2 < num3) min = num2; else min = num3;
17
Aalborg Media Lab 23-Jun-1517 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
18
Aalborg Media Lab 23-Jun-15 The switch Statement switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } switch and case are reserved words If expression matches value2, control jumps to here
19
Aalborg Media Lab 23-Jun-15 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
20
Aalborg Media Lab 23-Jun-15 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
21
Aalborg Media Lab 23-Jun-15 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
22
Aalborg Media Lab 23-Jun-15 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
23
Aalborg Media Lab 23-Jun-1523 The while Statement while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repeatedly until the condition becomes false.
24
Aalborg Media Lab 23-Jun-15 Logic of a while Loop statement true condition evaluated false
25
Aalborg Media Lab 23-Jun-1525 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; }
26
Aalborg Media Lab 23-Jun-1526 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
27
Aalborg Media Lab 23-Jun-15 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)PalindromeTester.java
28
Aalborg Media Lab 23-Jun-15 The do Statement do { statement; } while ( condition ) do and while are reserved words The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false
29
Aalborg Media Lab 23-Jun-15 Logic of a do Loop true condition evaluated statement false
30
Aalborg Media Lab 23-Jun-15 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
31
Aalborg Media Lab 23-Jun-15 The for Statement for ( initialization ; condition ; increment ) statement; Reserved word The initialization is executed once before the loop begins The increment portion is executed at the end of each iteration The condition-statement-increment cycle is executed repeatedly
32
Aalborg Media Lab 23-Jun-15 The for Statement A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }
33
Aalborg Media Lab 23-Jun-15 Logic of a for loop statement true condition evaluated false increment initialization
34
Aalborg Media Lab 23-Jun-15 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
35
Aalborg Media Lab 23-Jun-15 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
36
Aalborg Media Lab 23-Jun-15 Drawing with loops and conditionals Drawing multiple circles with different diameter position using for loops Enables us to distinguish between various event sources
37
Aalborg Media Lab 23-Jun-15 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”); else label.setText (“Right button was pushed”); }
38
Aalborg Media Lab 23-Jun-15 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!!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.