Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.

Slides:



Advertisements
Similar presentations
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Advertisements

Introduction to C Programming
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Introduction to C Programming
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Using Decision Structures.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Computer Science Selection Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Flow of Control Part 1: Selection
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
JavaScript, Fourth Edition
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Computer Science Up Down Controls, Decisions and Random Numbers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3: Decisions and Loops
2.5 Another Java Application: Adding Integers
University of Central Florida COP 3330 Object Oriented Programming
Chapter 6: Conditional Statements and Loops
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
T. Jumana Abu Shmais – AOU - Riyadh
Conditions and Boolean Expressions
Selection Control Structure
CS139 October 11, 2004.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Using Decision Structures
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
LCC 6310 Computation as an Expressive Medium
Controlling Program Flow
Intro to Programming (in JavaScript)
Presentation transcript:

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

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!)

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.

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…

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!

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

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.

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)

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 >=

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.

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

More on Boolean Operators From Boolean expressions, we can develop Truth Tables: Cond A Cond. B A && B A || B !(A) TTTTF TFFTF FTFTT FFFFT

Multiple Conditions Cond.QuestionYesNoResponse I Weekend? X- Response 1 First day? X- Response 2 II Weekend? X- Response 3 First day? -X Response 4 III Weekend? -X Response 5 First day? X- Response 6 IV Weekend? -X Response 7 First day? -X Response 8

Operator Precedence OrderDescriptionOperator 1 Dot Operator. 2 Parenthesis (Work from inside out) () 3 Instance Operator new 4 Multiplication, Division, Modulus * / % 5 Addition, Subtraction, Concatenation + -

Operator Precedence (continued) OrderDescriptionOperator 6Assignment = += -= *= /= %= 7 Comparison Operators, =, = 8Inequality!= 9 Boolean AND && 10 Boolean OR ||

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

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

window.confirm() & Boolean Example Code Example

Questions?

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)