BEI - if Statements Control Flow (Lesson)
if Statements Control Flow
Introduction
Flow of control is the order in which statements are executed. In the programs that we have written so far, our code has been executed one line at a time in the order that it was written. This order is called linear - meaning that programs are executed one statement at a time, one after another.
What if we wanted to change that? There are times when we may want one block of code to be executed several times instead of just once. For example, in our previous unit we looked at generating the roll of a die by creating a random number generator. How would you write code that would show five or ten rolls of the dice?
Conditional/Selection Statement
Conditional or selection statements allow us to choose which statement of code will be executed next. This allows statements of code to be repeated or skipped altogether. To do this in Java, we use if statements, if-else statements, and switch statements. The AP Exam, and this course, will cover if and if-else statements.
Decision Making
Now that we can repeat or skip lines of code, we need to make the decision when to do that. This is done with boolean expressions. Code can be written with a boolean expression to decide on whether a statement should be executed. Boolean expressions will either be evaluated true or false. If the expression is true, then the code will be executed. If it is false, then the code will be skipped.
if Statements Presentation
Click in the box below to learn more about if Statements in the presentation activity.
Click on "Runestone Academy" below to open the required reading that is listed.
Runestone Academy: AP CSA – Java Review Links to an external site.:
READ: 5.1 - Conditionals
Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.
Read: Introduction to Computer Science Using Java Links to an external site.:
READ: Chapter 16: The if Statement
Complete the quiz for practice.
Phrase Free Response Question Video
Click below to watch the video about Phrase Free Response Questions.
if Statements Practice
Click below to start the if Statements practice activity.
Practice-It! Self-Check
- Go to the PracticeIt website Links to an external site..
- Log into account.
- Click on Start Practicing!
- Go to the most recent edition.
- Click on Chapter 4: Conditional Execution.
- Complete Self-Check: 4.3
IMAGES CREATED BY GAVS