Using Control Structures

22
Using Control Structures

description

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. - PowerPoint PPT Presentation

Transcript of Using Control Structures

Page 1: Using  Control  Structures

Using Control Structures

Page 2: Using  Control  Structures

GoalsUnderstand the three basic forms of control

structuresUnderstand how to create and manage

conditionalsUnderstand how to change program

execution flow based on conditionalsBring it! (Practice with conditionals)

Page 3: Using  Control  Structures

Programming and Control StructuresAll programming languages support

Control StructuresEssentially, 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

Page 4: Using  Control  Structures

Control Structures: SequentialSequential 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!)

Page 5: Using  Control  Structures

Control Structures: BranchingBranching 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.

Page 6: Using  Control  Structures

Control Structures: LoopingLooping 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…

Page 7: Using  Control  Structures

Understanding ConditionalsIn 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!

Page 8: Using  Control  Structures

“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.

Page 9: Using  Control  Structures

The Conditional as SentrySo… 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.

Page 10: Using  Control  Structures

Using the Relational OperatorThe 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)

Page 11: Using  Control  Structures

Available Relational Operators

Comparison JavaScript Relational Operator

Is equal to ==Is not equal to !=Is less than <Is less than or equal to <=Is greater than >Is greater than or equal to >=

Page 12: Using  Control  Structures

Multiple ConditionsSometimes, 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.

Page 13: Using  Control  Structures

Boolean OperatorsSometimes, 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

Page 14: Using  Control  Structures

More on Boolean Operators

From Boolean expressions, we can develop Truth Tables:

Cond A Cond. B A && B A || B !(A)

T T T T FT F F T FF T F T TF F F F T

Page 15: Using  Control  Structures

Multiple ConditionsCond. Question Ye

sNo Response

I Weekend? X - Response 1First day? X - Response 2

II Weekend? X - Response 3First day? - X Response 4

III Weekend? - X Response 5First day? X - Response 6

IV Weekend? - X Response 7First day? - X Response 8

Page 16: Using  Control  Structures

Operator PrecedenceOrde

r Description Operator

1 Dot Operator .

2 Parenthesis (Work from inside out) ()

3 Instance Operator new

4 Multiplication, Division, Modulus * / %

5 Addition, Subtraction, Concatenation + -

Page 17: Using  Control  Structures

Operator Precedence (continued)

Order Description Operator

6 Assignment = += -=*= /= %=

7 Comparison Operators <, >, <=, >=

8 Inequality !=

9 Boolean AND &&

10 Boolean OR ||

Page 18: Using  Control  Structures

Boolean ValuesWhen 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)) …

Page 19: Using  Control  Structures

window.confirm() MethodBoolean 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

Page 21: Using  Control  Structures

Questions?

Page 22: Using  Control  Structures

ResourcesJavaScript: 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)