1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Creating PHP Pages Chapter 7 PHP Decisions Making.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
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.
UNIT II Decision Making And Branching Decision Making And Looping
Fundamentals of C and C++ Programming Control Structures and Functions.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CPS120 Introduction to Computer Science Iteration (Looping)
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Controlling Execution Dong Shao, Nanjing Unviersity.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute.
Week 4 Program Control Structure
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Working with Loops, Conditional Statements, and Arrays.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Java Programming Fifth Edition
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
JavaScript: Control Statements I
During the last lecture we had a discussion on Data Types, Variables & Operators
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Homework Any Questions?.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4: Control Structures I (Selection)
CSC215 Lecture Control Flow.
Presentation transcript:

1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)

2 if: Example 1 if ( day == “Sunday” ) Weather = “Cool” ; Set the value of the variable ‘weather to ‘Cool’ if the ‘day’ is equal to ‘Sunday’ The condition enclosed in parentheses semicolon This was the case if we want to execute a single statement given that the condition is true What if we want to execute multiple statements in case the condition is true?

3 if: Example 2 if ( day == “Sunday” ) { person = “Cool” ; mood = “Great” ; clothing = “Casual” ; } Set the value of the variable ‘person to ‘Cool’, ‘mood’ to ‘Great’, and ‘clothing’ to ‘casual’ if the ‘day’ is equal to ‘Sunday’ These curly braces group the multiple statements into a single compound statement

4 Compound Statements 1.At times, we need to put multiple statements at places where JavaScript expects only one 2.For those situations, JavaScript provides a way of grouping a number of statements into a single statement, called a “statement block” 3.This is done simply by enclosing any number of statements within curly braces, { } 4.NOTE: Although the statements within the block end in semicolons, the block itself doesn’t

5 if: Example 3 if ( (day == “Sunday”) || (day == “Saturday”) ) { person = “Cool” ; mood = “Great” ; clothing = “Casual” ; } ===================== weekend = ( day == “Sunday” ) || ( day ==“Saturday” ) ; if ( weekend ) { person = “Cool” ; mood = “Great” ; clothing = “Casual” ; }

6 We now know how to execute a statement or a block of statements given that the condition is true…. What if we want to include an alternate action as well, i.e. a statement or a block of statements to be executed in case the condition in not true…. if ( GPA >= 2.5 ) student = “Pass” ; else student = “Fail” ;

7 if … else: Example if ( GPA >= 2.5 ) { student = “Pass” ; mood = “Great” ; } else student = “Fail” ;

8 if … else: Example 5 if ( grade == “A” ) points = 4.0 ; if ( grade == “B” ) points = 3.0 ; if ( grade == “C” ) points = 2.0 ; if ( grade == “D” ) points = 1.0 ; if ( grade == “F” ) points = 0.0 ; What can we do to improve it? This piece of code is correct, but not very efficient!

9 if … else: Example 6 if ( grade == “A” ) points = 4.0 ; else { if ( grade == “B” ) points = 3.0 ; else { if ( grade == “C” ) points = 2.0 ; else { if ( grade == “D” ) points = 1.0 ; else points = 0.0 ; }

10 switch: Example 1 switch ( grade ) { case “A” : points = 4.0 ; break ; case “B” : points = 3.0 ; break ; case “C” : points = 2.0 ; break ; case “D” : points = 1.0 ; break ; default : points = 0.0 ; } The expression enclosed in parentheses is evaluated and matched with case labels This is a case label A colon following the case label is required This ‘break’ statement is the exit point The ‘default’ statement acts like the ‘else’ clause in the ‘if…else’ structure

11 switch: Example 2 switch ( inquiry ) { case “apple” : document.write( “Apples are Rs 50/kg” ) ; break ; case “mangos” : document.write( “Mangos are Rs 90/kg” ) ; break ; case “grapes” : document.write( “Grapes are Rs 60/kg” ) ; break ; default : document.write( inquiry + “? Please retry!” ) ; }

12 if … else: Example if ( ( GPA >= 2.5 ) && ( attendance >= 40 ) ) bhola = “Pass” ; else { if ( ( GPA >= 2.0 ) && ( attendance >= 36 ) ) bhola = “Probation” ; else bhola = “Fail” ; }

13 Loop through a set of statements as long as a condition is true condition True False statement block

14 x = 75 ;// x is the decimal number y = “” ;// y is the binary equivalent while ( x > 0 ) { remainder = x % 2 ; quotient = Math.floor( x / 2 ) ; y = remainder + y ; x = quotient ; } document.write( “y = ” + y ) ; Decimal to Binary Conversion in JavaScript The condition enclosed in parentheses

15 while: Example 2 while ( tankIsFull == false ) { tank = tank + bucket ; } document.write ( “Tank is full now” ) ; ======== x = 1 ; while ( x < 6000 ) { document.write ( x ) ; x = x + 1 ; }

16 for: Example 1 x = 1 ; while ( x < 6000 ) { document.write ( x ) ; x = x + 1 ; } for ( x = 1 ; x < 6000 ; x = x + 1 ) { document.write ( x ) ; } Initial countConditionOperation

17 for: Description (1) 1.The ‘for’ loop starts by initializing the counter variable (which in this case is x) 2.The initial value in this case is ‘1’, but can be any other positive or negative number as well 3.Next the ‘for’ loop checks the condition. If the condition evaluates to a ‘true’ value, the ‘for’ loop goes through the loop once

18 for: Description (2) 4.After reaching the end of that iteration, the ‘for’ loop goes to the top once again, performs the operation, checks the condition 5.If the condition evaluates to a ‘false’ value, the ‘for’ loop finishes looping 6.Otherwise, the ‘for’ loop goes through the loop once again 7.Repeat from step 4

19 for: Example for ( x = 99 ; x < 6000 ; x = x + 1 ) { document.write ( x ) ; } ============= for ( x = 6000 ; x > 0 ; x = x - 1 ) { document.write ( x ) ; } How many iteration s would this ‘for’ loop run for?

20 for --?-- while When the exact number of iterations is known, use the ‘for’ loop When the number of iterations depend upon a condition being met, use the ‘while’ loop ‘for’ loops become especially useful when used in conjunction with arrays We’ll find out about arrays next time, and we’ll probe their usefulness as part of ‘for’ loop structures