1 / 11

Session 2

Session 2. Conditional Branching.

brede
Télécharger la présentation

Session 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 2

  2. Conditional Branching • We encounter situations that employ conditional branching techniques every day. It all comes down to choices. We often have two or more things to do at a time and must decide upon one and only one. When faced with choices such as these, we must evaluate the reasoning for each and make a decision based on a condition. • For example, say you bring $300 to the mall to buy a new outfit. You narrow it down to two selections; the first costs $500, but the second costs $200 and isn’t as nice. You think to yourself “If I have more than $500 I will buy the nicer outfit. Otherwise, I will buy the other outfit.” This is a perfect example of a conditional branching.

  3. Conditional Branching • Conditional branching is used to alter the normal flow of execution depending on the value of a Boolean expression (Boolean expressions are explained in the following slide). • The use of conditional branching allows programs to become much more flexible. • The primary block associated with this behavior is the if/else block: • The box next to “If” is called the predicate or conditional part of the structure and is where the Boolean expression is placed. This is what determines if the consequent (internal), or alternate (else) blocks are executed.

  4. Boolean Expressions • If blocks are controlled by Boolean expressions located in the predicate area. • By default, the if block comes with a true or false value set in the predicate. In most cases, this value will need to be replaced by a Boolean expression (operator, variable or function). • Some examples of Boolean expressions found in the world’s functions tab: • If the expression in the predicate evaluates to “true,” the blocks located below “If” ARE executed, and the blocks located below “Else” are NOT executed. • Conversely, if the expression evaluates to “false”, the blocks located below “If” are NOT executed, and the blocks located below “Else” ARE executed.

  5. Conditional Branching – If/Else Block • Let’s take a look at an example: • Starting at the top, the mummy will move 1 meter in whatever direction it’s currently pointing. • Then the computer asks itself the question “Does 1 equal 2?” Obviously it does not so the comparison block evaluates to false. Since the predicate is false, the turn block is skipped. • The normal flow of execution is then resumed and the second move block is encountered. As a result, this code snippet causes the mummy to move a total of 2 meters.

  6. Conditional Branching – If/Else Block • What if we change the numbers so the comparison returns true? • Since 2 does equal 2, the comparison returns true and the turn block is executed. • Then, as before, the normal flow of execution resumes and the remaining block beneath the “If” block is executed. • Consequently, this snippet of code moves the mummy 1 meter, turns 180 degrees, and then travels 1 meter back to its starting position.

  7. Conditional Branching – If/Else Block • So far we’ve only used the consequent part of the If/Else block. Now, we’re going to explore the alternate (else) area. • While the blocks in the consequent area are executed if the condition is true, the blocks in the alternate (else) area are executed only if the condition is false. • By definition, it is impossible for blocks contained in both the consequent and alternate areas to be executed. Once the path of execution is determined, it is strictly one or the other.

  8. Conditional Branching – If/Else Block • Let’s look at an example of If/Else block behavior. • The mummy moves half a meter then reaches the predicate. • The comparison evaluates to false since 2 is NOT less than 1. • Due to the false value of the condition, the turn left block is skipped and the turn right block is executed instead. • The mummy then moves another half meter.

  9. Conditional Branching – User Input • In the previous examples, the condition was unable to change. The comparison would evaluate to either true or false and stay that way forever because the relation of numbers never changes. Let’s modify the previous bit of code so the condition changes based on input from the user. • With this small change, the true flexibility of the If/Else structure begins to emerge. • Now, every time the program is executed, a different branch can be taken. The number of choices is limited to two in this case, but through the power of nesting, this limitation is no longer an issue.

  10. Conditional Branching – Nesting • What if the question asked had more than 2 valid answers? A single If/Else block is unable to handle a third result, and even if it could, there is no way to choose it since all Boolean expressions give only a true or false answer. • This problem is addressed with a technique know as nesting. By placing If/Else blocks inside of other If/Else blocks, powerful decision trees can be created to handle any number of situations.

  11. Project 2 • Create a program that asks the user which way to turn the camera (left or right). Then ask if they want to move the camera forward, and then if so, how far in that direction the camera should move. • Once you have completed the above project, modify it so the user is asked the questions twice in a row instead of just once (get direction, get distance, move, get direction, get distance, move).

More Related