Conditionals

You've learned that an app's behavior is defined by a set of event handlers. Event handlers define the sequence of blocks that should be performed when some event, such as a button click, occurs. But the response to an event need not be a linear sequence of blocks that are always performed; instead, the response can contain blocks that are only performed under certain conditions.

Another way to say this is that an app can make decisions. You program them to do so with conditional blocks in the form of if-else" questions. Typically the question involves information in the app's memory cells, its component properties and variables. In the "I Have a Dream" app, clicking on the pictures of each civil rights leader should either start or pause the speech. The first time each button is clicked, the response should be to play the speech. The second time it is clicked, the speech should be paused. Thus when the button is clicked, the response is not always the same and the app must make a decision based on whether or not the speech is already playing.

Almost all "if" questions (tests) involve checking the values of component properties or variables. In the case of "I Have a Dream", the Player component's IsPlaying property is checked.

Boolean expressions and relational and logical operators

The if "test" is called a boolean expression, that is, it must resolve to true or false. The boolean expression can be arbitrarily complex and involve any number of relational operators (e.g., <,>,=) and logical operators (and, or, not). If-else blocks can also be nested within each other. Thus, the simple if-else construct provides the basis for arbitrarily complex logic and what is commonly known as artificial intelligence.

Conditionals Learning Nugget (includes video)