1 / 22

Using Control Structures

Using Control Structures. Goals. Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how to change program execution flow based on conditionals Bring it! (Practice with conditionals). Programming and Control Structures.

acton
Download Presentation

Using Control Structures

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. Using Control Structures

  2. Goals • Understand the three basic forms of control structures • Understand how to create and manage conditionals • Understand how to change program execution flow based on conditionals • Bring it! (Practice with conditionals)

  3. Programming and Control Structures • All programming languages support Control Structures • Essentially, control structures give programmers a toolset for “controlling” what code gets executed, and how many times. • There are three basic control structures: • Sequential • Branching • Looping

  4. Control Structures: Sequential • Sequential Code – Unless otherwise indicated, programming instructions are executed in order… the first line of code, then the next, then the third. • Sequential processing is the default control structure (ie, as a programmer, you get it without asking!)

  5. Control Structures: Branching • Branching structures allow the programmer to structure mutually exclusive blocks of code • Based on the value of some conditional, either one block of code executes or another…. • For example: If it’s raining, take an umbrella. Else, leave the umbrella at home.

  6. Control Structures: Looping • Looping structures allow the programmer to create repeating blocks of code. • The code can repeat zero or many times, based on the value of some conditional. • For example: Would you like to play a game? Game is played and ends, and the user is asked: Would you like to play again? Game is played and ends, and the user is asked: Would you like to play again…

  7. Understanding Conditionals • In a later presentation, we will look at branching and looping in detail, but for now, let’s flesh out the toolset of the conditional • A conditional is a Boolean expresssion that can be used to control program execution. • (And btw, a Boolean expression is one that can be evaluated as True or False) • As it turns out, you already know quite a bit about conditionals!

  8. “Concepts” to “Conditions” Consider the following statements: • “It’s raining.” • “Your shirt is blue.” • “There are no more records in the file.” • “The user clicked ‘YES’” • All of these statements are Boolean expressions – we could figure out if each is a True or False statement • On the other hand, an expression such as “x + 3” isn’t a Boolean. Even if you told me what x was, ‘x + 3’ doesn’t result in a True or False. • Programmers construct conditionals to act as Guards in front of a section of code… if the conditional is TRUE, the guard let’s you pass into a section of code. If the conditional is FALSE, the guard blocks you from entering.

  9. The Conditional as Sentry So… consider the following conditional: If (x > 3) {Do a bunch of fun stuff; Including more fun stuff; and even more fun stuff; and yati, yati, yati!!!} Else {Poke yourself in the eye with a sharp stick} The conditional (x>3) stands guard in front of the fun lines of code. If the expression turns out to be True (ie, x IS greater than 3), the sentry (ie, soldier) lets you pass into the fun code. This sentry analogy is so compelling that the variable in a conditional is referred to as the “sentinel” value. If the conditional is False, the sentinel blocks you from entering the fun code and yikes, you gotta get that stick…. Given how important the conditional is, it’s worth understanding in detail the power it offers a programmer.

  10. Using the Relational Operator • The relational (comparison) operator compares two values of THE SAME TYPE (i.e. – Strings are compared to strings, integers are compared to integers, etc.) • There are several different types of comparison operators (see next slide) • Comparison operators HAVE NO PRECEDENCE among themselves and are evaluated LEFT to RIGHT (however, parentheses can be added to alter precedence)

  11. Available Relational Operators

  12. Multiple Conditions • Sometimes, it’s necessary to combine multiple conditions to form a single, more complex text. • It is a good idea to first establish a table (on paper) mapping the multiple conditions, the possible responses and the program’s reaction to responses.

  13. Boolean Operators • Sometimes, conditions can be joined together in the same test using Boolean operators: • Logical AND is represented by &&Example: if (A = B && C = D) … • Logical OR is represented by ||Example: if(A = B || C = D) … • Logical NOT is represented by ! (bang symbol)Example: if( !(A = B) ) … • Boolean Precedence: NOT, followed by AND, followed by OR

  14. More on Boolean Operators • From Boolean expressions, we can develop Truth Tables:

  15. Multiple Conditions

  16. Operator Precedence

  17. Operator Precedence (continued)

  18. Boolean Values • When testing against Boolean values, you can use shortcuts (True Example): if(myVar == true) … is the same thing as if(myVar) … • False Example: if(myVar == false) … is the same thing as if(!(myVar)) …

  19. window.confirm() Method • Boolean values are returned by the window.confirm() method. • window.confirm() takes input from the user, based on which button they choose (OK=TRUE & CANCEL=FALSE) • It looks and feels a lot like window.alert(), except that it has both an OK and a CANCEL button

  20. window.confirm() & Boolean Example Code Example

  21. Questions?

  22. Resources • JavaScript: The Definitive Guide by David Flanagan (O’Reilly, 2002) • JavaScript Concepts & Techniques: Programming Interactive Web Sites by Tina Spain McDuffie (Franklin, Beedle & Associates, 2003) • Extended Prelude to Programming: Concepts and Design by Stewart Venit (Scott/Jones, Inc., 2002)

More Related