Download presentation
Presentation is loading. Please wait.
Published byGyles Ramsey Modified over 6 years ago
1
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement, moves down one statement at a time until the program is complete. A Java application begins with the first line of the main method and proceeds step by step until it gets to the end of the main method. Invoking a method changes the flow of control. Flow of Control
2
Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after the other in sequence 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
3
Conditional Statements
A conditional statement lets us choose which statement will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions Some conditional statements in Java are the if statement the if-else statement Note that the switch statement is not part of the AP subset and is not covered in the course.
4
The if Statement The if statement has the following syntax:
The condition must be a boolean expression. It must evaluate to either true or false. if is a Java reserved word if ( condition ) statement; If the condition is true, the statement is executed. If it is false, the statement is skipped. The if statement is a conditional statement found in many programming languages, including Java. An if statement may consists of the reserved word if followed by a Boolean expression, or condition, followed by a statement. The condition is enclosed in parentheses and must be either true or false. If the condition is true, the statement is executed and processing continues with the next statement. If the condition is false, the statement is skipped and processing continues immediately with the next statement.
5
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. 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.
6
This example reads the age of the user and then decides which sentence to print, based on the age that is entered. The Age program echoes the age value that is entered in all cases. If the age is less than the value of the constant MINOR, the statement about youth is printed. If the age is equal to or greater than the value of MINOR, the println statement is skipped. In either case the final sentence about age being a state of mind is printed.
7
Logic of an if statement
condition evaluated false statement true
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 (=)
9
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 Sometimes you want to do one thing if a condition is true and another thing if that condition is false. We can add an else clause to an if statement, making it an if-else statement, to handle this kind of situation. One or the other will be executed, but not both
10
Logic of an if-else statement
condition evaluated false statement2 statement1 true
11
The wages program uses an if-else statement to compute the payment for employees. If an employee works over 40 hours in a week, the payment amount includes the overtime hours. An if-else statement is used to determine whether the number of hours entered by the user is greater than 40. If it is, the extra hours are paid at a rate of one and a half times the normal rate. If there is no overtime hours, the total payment is based simply on the number of hours worked and the standard rate.
12
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 statements
13
The Guessing program uses an if-else statement with the statement of the else clause in a block statement. If the guess doesn’t match, two statements are printed, one that says that the guess is wrong and one that prints the actual answer. If we didn’t use the block braces, the sentence stating that the guess is incorrect would be printed if the guess was wrong, but the sentence revealing the correct answer would be printed in all cases. That is, only the first statement would be considered part of the else clause.
14
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
15
The MinOfThree program uses nested if statements to find the smallest of three integer values entered by the user.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.