Compound Statements
Learn about compound statements in detail.
We'll cover the following...
Compound statements
As we learned in the previous lesson, a compound statement is a statement that spans several lines. These compound statements are made up of one or more normal one-line statements. They can also consist of other compound statements, which we’ll learn later on. For example, in our previous application that turns on the outdoor lights, we have some conditions that must be met before we do something. One condition is that we only turn on the lights if the current time is after sunset. The logic can be visualized in a flowchart:
As you can see, the part where we turn on the lights is only executed if it’s after sunset. So, for that statement to be performed, we have a condition that must be met. That condition is a statement, and it includes the statement that turns on the lights.
Code
The code might look something like this:
wait_for_phone_signal()current_time = get_current_time()sunset_time = get_sunset_time()if current_time > sunset_time thenturn_on_light()end_if