Types of selection structures

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

2 Casa 15m Perspectiva Lateral Izquierda.
Repaso: Unidad 2 Lección 2
1 A B C
Simplifications of Context-Free Grammars
Variations of the Turing Machine
4 Control Statements: Part 1.
AP STUDY SESSION 2.
1
& dding ubtracting ractions.
Select from the most commonly used minutes below.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
David Burdett May 11, 2004 Package Binding for WS CDL.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Create an Application Title 1Y - Youth Chapter 5.
CALENDAR.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
The 5S numbers game..
Media-Monitoring Final Report April - May 2010 News.
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
1 The Royal Doulton Company The Royal Doulton Company is an English company producing tableware and collectables, dating to Operating originally.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
TESOL International Convention Presentation- ESL Instruction: Developing Your Skills to Become a Master Conductor by Beth Clifton Crumpler by.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Chapter 1: Expressions, Equations, & Inequalities
1..
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
1 Termination and shape-shifting heaps Byron Cook Microsoft Research, Cambridge Joint work with Josh Berdine, Dino Distefano, and.
Artificial Intelligence
Before Between After.
Subtraction: Adding UP
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
1 Titre de la diapositive SDMO Industries – Training Département MICS KERYS 09- MICS KERYS – WEBSITE.
Essential Cell Biology
Converting a Fraction to %
CSE20 Lecture 15 Karnaugh Maps Professor CK Cheng CSE Dept. UC San Diego 1.
Clock will move after 1 minute
famous photographer Ara Guler famous photographer ARA GULER.
PSSA Preparation.
& dding ubtracting ractions.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
Chapter 14 - JavaScript/JScript: Control Structures I
Presentation transcript:

Types of selection structures Control Structures Types of selection structures if Single-selection structure Selects or ignores a single action or group of actions if/else Double-selection structure Selects between two actions or groups of actions switch Multiple-selection structure Selects among many actions or groups of actions

Four types of repetition structures 14.4 Control Structures Four types of repetition structures while do/while for for/in

All control structure names are keywords Control Structures All control structure names are keywords Reserved by language for feature implementation May not be used as variable names

The if Selection Structure Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” JavaScript statement: if( grade >= 60 ) document.writeln( “Passed” ); Proper syntax: indent all lines within structure  

The if Selection Structure Conditions which evaluate to true True condition Non-zero numeric value String containing at least one character Conditions which evaluate to false False condition Numeric value = 0 Empty string Variable with no assigned value  

The if/else Selection Structure Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed” JavaScript statement: if ( grade >= 60 ) document.writeln( “Passed” ); document.writeln( “Failed” );  

The if/else Selection Structure Conditional Operator (?:) JavaScript’s only ternary operator Takes three operands 1. Boolean expression 2. Value for conditional expression if true 3. Value for conditional expression if false Example document.writeln( studentGrade >= 60 ? “Passed” : “Failed” ); Same operation as preceding if/else statement    

1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 3 <!-- Fig. 14.9: Average2.html --> 4 5 <HEAD> 6 <TITLE>Class Average Program: 7 Sentinel-controlled Repetition</TITLE> 8 9 <SCRIPT LANGUAGE = "JavaScript"> 10 var gradeCounter, // number of grades entered 11 gradeValue, // grade value 12 total, // sum of grades 13 average, // average of all grades 14 grade; // grade typed by user 15 16 // Initialization phase 17 total = 0; // clear total 18 gradeCounter = 0; // prepare to loop 19 20 // Processing phase 21 // prompt for input and read grade from user 22 grade = window.prompt( 23 "Enter Integer Grade, -1 to Quit:", "0" ); 24 25 // convert grade from a String to an integer 26 gradeValue = parseInt( grade ); 27 28 while ( gradeValue != -1 ) { 29 // add gradeValue to total 30 total = total + gradeValue; 31 32 // add 1 to gradeCounter

33 gradeCounter = gradeCounter + 1; 34 35 // prompt for input and read grade from user 36 grade = window.prompt( 37 "Enter Integer Grade, -1 to Quit:", "0" ); 38 39 // convert grade from a String to an integer 40 gradeValue = parseInt( grade ); 41 } 42 43 // Termination phase 44 if ( gradeCounter != 0 ) { 45 average = total / gradeCounter; 46 47 // display average of exam grades 48 document.writeln( 49 "<H1>Class average is " + average + "</H1>" ); 50 } 51 else 52 document.writeln( "<P>No grades were entered</P>" ); 53 </SCRIPT> 54 </HEAD> 55 56 <BODY> 57 <P>Click Refresh (or Reload) to run the script again</P> 58 </BODY> 59 </HTML>

User Input: Script Output:

Both ways add 3 to the value of c Example 2 executes faster Assignment Operators Assignment operations with identical results can be written different ways Example 1: c = c + 3; Example 2: c += 3; Both ways add 3 to the value of c Example 2 executes faster Small difference for individual operations Significant over large number of operations

Arithmetic Assignment Operators

Increment and Decrement Operators Increment operator (++) Example: c++; is identical to c += 1; is identical to c = c + 1; Decrement operator (--) c--; is identical to c -= 1; is identical to c = c - 1; Faster operation – Save time over many repetitions Can be preincremented/decremented or postincremented/decremented Only makes a difference when variable appears in context of larger expression

Increment and Decrement Operators

JavaScript - loosely typed language 14.13 A Note on Data Types JavaScript - loosely typed language Does not require variable to have type before use in program (unlike other languages) Variable can contain a value of any data type JavaScript often converts between values of different types automatically When declaring variables If not given value, variable has undefined value To indicate variable has no value, assign it null