๐3.2: If Statements
Table of Contents
๐ This page is a condensed version of CSAwesome Topic 3.2
๐ Take notes in a Codespace during class, coding along with the instructor.
- Go to GitHub and click on your picture in the TOP RIGHT corner
- Select
Your repositories
- Open
CS2-Unit-3-Notes
- Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
(unless you already have one listed there), wait for the environment to load, then youโre ready to code!
If Statements and Control Flow
The statements in a Java main method normally run or execute one at a time in the order they are found, from top to bottom. If statements (also called conditionals or selection) change the flow of control through the program so that some code is only run when something is true. If statements are found in all programming languages as a way to make choices.
- In an if statement, if the condition is
true
then the next statement or a block of statements will execute. - If the condition is
false
then the next statement or block of statements is skipped.
A conditional uses the keyword if
followed by a boolean expression inside of an open parenthesis (
and a close parenthesis )
and then followed by a single statement or block of statements. The open curly brace {
and a close curly brace }
are used to group a block of statements together.
It is recommended to always include the curly braces even if you have just one statement under the if statement! The questions you will see on the AP exam will usually use curly braces.
Examples of if
statements:
// A single if statement
if (boolean expression)
statement;
// A single if statement with {}
if (boolean expression) {
statement;
}
// An if statement block
if (boolean expression) {
statement1;
statement2;
...
statementN;
}
Note that there is no semicolon (
;
) at the end of the boolean expression in an if statement, even if it is the end of that line. The semicolon goes at the end of the whole if statement, often on the next line. Or{ }
are used to mark the beginning and end of the block of code under the if condition.
Examine the example below. Imagine that your cell phone wanted to remind you to take an umbrella if
it was currently raining in your area when it detected that you were leaving the house:
boolean isRaining = true;
if (isRaining) {
System.out.println("Take an umbrella!");
}
System.out.println("Drive carefully");
The variable
isRaining
is a boolean variable that is eithertrue
orfalse
. If it is true then the messageTake an umbrella!
will be printed and then execution will continue with the next statement which will printDrive carefully
.
Relational Operators in If Statements
Most if statements have a boolean condition that uses relational operators like ==
, !=
, <
, >
, <=
, >=
, as we saw in the last lesson.
A common mistake in if statements is using =
instead of ==
in the condition. You should always use ==
in the condition of an if statement to test a variable. One equal sign (=
) assigns a value to a variable, and two equal signs (==
) test if a variable has a certain value.
๐ป In-Class Activity
Have you ever seen a Magic 8 ball? You ask it a yes-no question and then shake it to get a random response like Signs point to yes!
, Very doubtful
, etc. If youโve never seen a Magic 8 ball, check out this simulator.
๐ฎ Write a short program in your Unit-3-Notes
repository that does the following:
- Choose a random number from 1 to 8
If you need help with random numbers, see lesson 2.9
- Come up with 8 possible responses to yes-no questions.
- Use if statements to test the number and print out a different response for each number.
โญ๏ธ Summary
- if statements test a boolean expression and if it evaluates to
true
, go on to execute the following statement or block of statements surrounded by curly braces ({}
) like below.
// A single if statement
if (boolean expression)
statement;
// A single if statement with {}
if (boolean expression) {
statement;
}
// An if statement block
if (boolean expression) {
statement1;
statement2;
...
statementN;
}
-
Relational operators (
==
,!=
,<
,>
,<=
,>=
) are used in boolean expressions to compare values and arithmetic expressions. -
Conditional (if) statements affect the programโs flow of control by executing different statements based on the value of a Boolean expression.
๐ When class ends, donโt forget to SAVE YOUR WORK!
- Navigate to the
Source Control
menu on the LEFT sidebar - Type a brief commit message in the box, for example:
updated Main.java
- Click the button on the LEFT menu
- Click the button on the LEFT menu
- Finally you can close your Codespace!
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.