Lessons
|
Programing Decisions
Most of the programs that we made in the 3rd quarter where linear. Meaning that there was only one possible way of a program running. The program runs and then we wait, watch, and then it finishes. Not the most exciting programs to be sure! Most times however, we want to have programs that behave differently in different situations. We want the programs to say You win! if you win the game or Sorry, try again if you lose.
The ability to make decisions based on certain criteria, that we the programmer create, is an important part of programming. If we didn't have this ability, programs would be very linear in nature and quite frankly boring. Scratch’s Control category contains two blocks that allow you to make decisions and control actions in your programs: the if block and the if/else block. Using these blocks, you can ask a question and take a course of action based on the answer.
If Block
The if block is a decision structure that gives you the ability to specify whether a set of commands should (or should not) be executed based on the result of a test condition. If the condition in the block is true then the code inside the if part of the block are executed. if the condition is false, no code is executed.
If/Else Block
The if /else block is a little different than the if block. The specified condition is tested at the top of the block. If the condition is true, the commands in the if part of the block are executed. If the condition is false,
however, the commands under else will execute instead. The program will only execute one of the two groups of commands in the if/else block. |