Lecture 4-1: if-else statements
After reading through Chapter 4, you should be able to
- Write
ifstatements - Write
if-elsestatements - Write
if-else-ifstatements - Write nested
ifstatements - Explain how to use a flag variable
- Explain the scope of a variables declared inside a set of curly braces, such as those belonging to an if statement
- Write boolean expressions
- Use
switchstatements - Use the conditional operator
- Compare
Stringobjects using equals(), compareTo(), equalsIgnoreCase(), and compareToIgnoreCase() - Format numbers using the standard
DecimalFormatclass - Explain the concept of algorithm decomposition and how a lengthy or complex method can be broken into several methods that each perform a related task
- Explain the role of private methods
Today's Learning Objectives
- Understand the concept of flow of control
- Write conditional (decision making) statements using
ifstatements - Know how to use block statements within
ifstatements
Flow of Control
Unless indicated otherwise, the order of statement execution (the flow of control) through a method is linear. That is, one statement after the other in the order they are written
Java provides two other ways to change the flow of control:
- Conditional statements (branching:
ifandswitch) - Repetition statements (looping:
while,for, anddo)
Conditional Statements
A conditional statement lets us select which statement will be executed next. By allowing selections, conditional statements give us the power to make basic decision choices in our programs
Java's conditional statements include
ifstatementif-elsestatementif-else-ifstatementswitchstatement- conditional operator
The if Statement
The if statement has the following syntax:
if ( condition )
statement;
The semantics (meaning) of the if statement is if the condition is true, do the statement that immediately follows.
If the condition is false (not true), skip the statement that immediately follows.
Example: if Statement
An example of an if statement:
if ( age >= 21 ) System.out.println("Congratulations, you're legal");
Age.java
// Declare constant final int YOUTH = 21; // Declare variables for user input int age; // Create a Scanner object to read inputScannerkeyboard = newScanner(System.in); // Get the user's age System.out.print("Enter your age: "); userInput; = keyboard.nextInt(); // Re-display the user input System.out.println ("\nYou entered: " + age); // Exampleifstatement (indent 3 spaces)if( age < YOUTH ) System.out.println ("\nYouth is a wonderful thing."); // Always do this statement System.out.println ("\nAge is a state of mind.\n");
Boolean Expressions
A condition often uses one of Java's six 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-else Statement
An else clause can be added to an if statement to make it an if-else statement:
if( condition ) statement1;elsestatement2;
The semantics (meaning) of the if-else statement is that if the condition is true, statement1 is executed;
otherwise, if the condition is false, then statement2 is executed.
One or the other statements will be executed, but not both
Wages.java
// Declare constant values final double PAY_RATE = 8.25; final int HOURS_IN_WORK_WEEK = 40; // Declare variables int hours; double pay; // Create a Scanner object to read inputScannerkeyboard = newScanner(System.in); // Get the number of hours worked this week System.out.print ("Enter the number of hours worked: "); hours = keyboard.nextInt(); // Use if-else statement to determine pay // overtime is at "time and a half"if( hours > HOURS_IN_WORK_WEEK ) pay = (HOURS_IN_WORK_WEEK * PAY_RATE) + ((hours - HOURS_IN_WORK_WEEK) * (PAY_RATE * 1.5));elsepay = hours * PAY_RATE; // Display the amount earned System.out.println ("\nGross earnings: $" + pay);
Block Statements
- Several statements can be grouped together into a block statement
- A block is delimited by curly braces { }
- A block statement can be used wherever a statement is called for in the Java syntax
if( guess==answer ) System.out.println ("You got it! Good guessing!");else{ System.out.println ("That is not correct, sorry."); System.out.println ("The number was " + answer); }
Nested if Statements
- An if statement can be nested inside another if statement.
if(num1 < num2) {if(num1 < num3) min = num1;elsemin = num3; }else{if(num2 < num3) min = num2;elsemin = num3; }
URL: http://www.cwu.edu/~schwing/cs110/Java/notes/lecture4-1.html
Title: CS 110: Lecture Notes for Programming Fundamentals 1
Author: Ed Gellenbeck, Central Washington University
Last Updated: Monday, January 16, 2006