represent branching logical processes by using flowcharts
use if statements
use if/else statements
use an else if statement
If statements are found in all programming languages as a way to get your programs to make decisions!
The statements in the 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.
In an if statement, if the condition is true then the next statement or 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 parentheses ( and a close parentheses ) and then followed by a single statement or block of statements.
The single statement or block of statements are only executed if the condition is true.
The open curly brace { and a close curly brace } are used to group a block of statements together.
Technically, for a single line of code in the body of an if statement, the curly braces are not required.
However, it is recommended to always put in the curly braces even if you only have one statement in the body of the if statement.
Make sure not to put a semicolon ; at the end of the boolean expression!
Here is an example of an in statement; this program will remind you to take an umbrella if it is currently raining.
The variable isRaining is a boolean variable that is either true or false. If it is true then the message Take an umbrella! will be printed and then the execution will continue with the next statement which will print Drive carefully.
If boolean isRaining was initialized as false instead of true, the print statement in the if block will be skipped and Drive carefully will still be printed.
It is common for if statements to have a boolean condition that uses relational operators like ==, !=, >, <, <=, >=.
Here is a simple program showing how these relational operators can be used as the boolean condition:
IMPORTANT: Notice that I didn't use curly braces { } for the body of the if statements above. This can only be done if there is ONE command below the if statement. Here are some screenshots of the output to show you that the code above still works without the curly braces.
A common mistake in an if statement is using = instead of == in the condition by mistake.
You should always use ==, not =, in the condition of an if statement to test a variable.
I can't say this enough... One equal sign (=) is used to initialize and/or reassign values to variables. Two equal signs (==) is used to test if a variable has a certain value.
IF-ELSE STATEMENTS
If statements are great, but what if you want to pick between two possibilities?
For example, what if you wanted to make a program to flip a coin and different things happen if it lands on heads or tails?
We can use the if keyword followed by a statement or block of statements and then the else keyword also followed by a statement or block of statements:
Notice that this time I used the curly braces { } which were is proper (but not required) if the if or else statement only contains a single line of code.
When in doubt, just make sure to use the curly braces { }.
The following flowchart demonstrates that if the condition (the boolean expression) is true, one block of statements is executed, but if the condition is false, a different block of statements inside the body of the else statement is executed:
Keep in mind that the else will only execute if the condition is false.
If-else statements can also be used with the relational operators (==, !=, >, <, >=, <=) as we have seen previously, but notice that the else statement does not have a condition! It is simply the keyword else followed by a line of code or block of code.
NESTED IFS AND DANGLING ELSE
If statements can be nested inside other if statements.
Sometimes with nested ifs we find a dangling else that could potentially belong to either if statement.
The rule is that the else will always be a part of the closest if statement in the same block of code, regardless of indentation.
This is why I recommend always using curly brackets { } to enclose your if statements.
It makes it much easier to see which if statement the else belongs to:
20 QUESTIONS
We could use nested if-else statements to create a 20 questions game! I didn't create the entire game, but the code below shows how this could be done:
ELSE-IF STATEMENTS
If statements are great if you need one possible outcome. If-else statements are great if you need two possible outcomes. But you can also pick between 3 or more possible outcomes if you add else if between the if and the else!
In Python we were able to use elif instead of else if, but I always have to remind myself that this doesn't work in Java :(
Here is a simple program showing you the basic syntax for else-if statements:
Here is a flowchart for a conditional with 3 options like in the code above:
Another way to handle 3 or more conditional cases is to use the switch and break keywords, but those will not be on the exam.
Here is a program where I used multiple else if statements, each with a different block of code:
Again, notice that I didn't use curly braces { } because each if, else-if, and else statement only contained a single line of code.
To be honest, I only did this to make the screenshot smaller haha. I usually ALWAYS use the curly braces.
SUMMARY
if-statements test a boolean expression and if it is true, go on to execute the following statement or block of statements surrounded by curly brackets { } like below:
// A single if-statement
if (boolean expression)
Do statement;
// A block if-statement
{
Do Statement1;
Do Statement2;
...
Do StatementN;
}
Relational operators (==, !=, <, >, <=, >=) are used in boolean expressions to compare values and arithmetic expressions.
Conditional (if) statements affect the flow of control by executing different statements based on the value of a Boolean expression.
If statements can be followed by an associated else part to form a 2-way branch:
if (boolean expression)
{
Do statment;
}
else
{
Do other statement;
}
A two way selection (if-else) is written when there are two sets of statements: one to be executed when the Boolean condition is true, and another set for when the Boolean condition is false.
The body of the if-statement is executed when the Boolean condition is true, and the body of the "else" is executed when the Boolean condition is false.
Use 2 test-classes to find errors or validate results to try both branches of an if-else statement.
The else statement attaches to the closest if statement.
A multi-way selection is written when there are a series of conditions with different statements for each condition.
Multi-way selection is performed using if-else-if statements such that exactly one section of code is executed based on the first condition that evaluates to true:
// 3 way choice with else if
if ( boolean expression)
{
statement 1;
}
else if (boolean expression)
{
statement 2;
}
else
{
statement 3;
}
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.