During the last lecture we had a discussion on Data Types, Variables & Operators

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

1 CS428 Web Engineering Lecture 12 Data Types & Operators (JavaScript - II)
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
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
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
The switch Statement, DecimalFormat, and Introduction to Looping
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.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120 Introduction to Computer Science Iteration (Looping)
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Introduction to JavaScript Data Types & Operators.
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.
Week 4 Program Control Structure
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
COMP Loop Statements Yi Hong May 21, 2015.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
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)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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 this.
Java Programming Fifth Edition
Selection (also known as Branching) Jumail Bin Taliba by
Selections Java.
The switch Statement, and Introduction to Looping
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 6: Conditional Statements and Loops
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 10 Programming Fundamentals with JavaScript
Chapter 8 JavaScript: Control Statements, Part 2
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 7 Conditional Statements
Chapter 4 Selection.
Chapter 6: Repetition Statements
Objectives In this chapter, you will:
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4: Control Structures I (Selection)
Week 3 – Program Control Structure
Using Decision Structures
Javascript Chapter 19 and 20 5/3/2019.
CSC215 Lecture Control Flow.
Controlling Program Flow
Lecture 9: JavaScript Syntax
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)

During the last lecture we had a discussion on Data Types, Variables & Operators We found out about JavaScript data types About variables and literals We also discussed several operators supported by JavaScript

JavaScript Data Types JavaScript recognizes & distinguishes among the following types of values: Numbers Booleans Strings Undefined

Variables Variables give us the ability to manipulate data through reference instead of actual value Variables are containers that hold values

Declaring Variables Although JavaScript allows variable declaration, it does not require it - except in the case when we want to declare a variable being local (more on local variables later in the course!)

JavaScript Variables are Dynamically Typed Any variable in JavaScript can hold any type of value, and the that type can change midway through the program

JavaScript Operators JavaScript has numerous operators, classified in many categories. We will look at only a few of them belonging to the following categories: Assignment operators Arithmetic operators Comparison operators Logical operators String operators

comments let the code speak for itself!

Decimal to Binary Conversion in JavaScript 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 ) ;

Today’s Lecture: Flow Control & Loops We’ll try to understand the concept of flow control using the “if” and “switch” structures And also the concept behind the “while” and “for” looping structures We will solve simple problems using flow control and loop structures

Flow Control

Select between alternate courses of action depending upon the evaluation of a condition True False statement block 1 statement block 2

JavaScript Flow Control Structures if … else switch

if: Example 1 if ( day == “Sunday” ) bhola = “Cool” ; Set the value of the variable ‘bhola 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?

if: Example 2 if ( day == “Sunday” ) { bhola = “Cool” ; mood = “Great” ; clothing = “Casual” ; } Set the value of the variable ‘bhola 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

if: Example 2 if ( day == “Sunday” ) { bhola = “Cool” ; mood = “Great” ; clothing = “Casual” ; } Set the value of the variable ‘status’ to ‘Cool’, ‘mood’ to ‘Great’, and ‘clothing’ to ‘casual’ if the ‘day’ is equal to ‘Sunday’ Note: No semicolon after the closing curly brace

Compound Statements At times, we need to put multiple statements at places where JavaScript expects only one For those situations, JavaScript provides a way of grouping a number of statements into a single statement, called a “statement block”

Compound Statements This is done simply by enclosing any number of statements within curly braces, { } NOTE: Although the statements within the block end in semicolons, the block itself doesn’t

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

if: Example 4 weekend = ( day == “Sunday” ) || ( day == “Saturday” ) ; if ( weekend ) { bhola = “Cool” ; mood = “Great” ; clothing = “Casual” ; } What is the data type of the variable “weekend”?

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 … else: Example 1 if ( GPA >= 1.0 ) bhola = “Pass” ; else bhola = “Fail” ;

if … else: Example 2 if ( GPA >= 1.0 ) { bhola = “Pass” ; } else bhola = “Fail” ;

if … else: Example 3 if ( GPA >= 1.0 ) { bhola = “Pass” ; mood = “Great” ; } else bhola = “Fail” ;

if … else: Example 4 if ( GPA >= 1.0 ) { bhola = “Pass” ; mood = “Great” ; } else { bhola = “Fail” ; mood = “Terrible” ;

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 ; This piece of code is correct, but not very efficient! What can we do to improve it?

Nested if … else Structures

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

JavaScript Flow Control Structures if … else switch

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

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

? if … else switch

if…else --?-- switch If the action to be taken of the value of a single variable (or a single expression), use ‘switch’ When the action depends on the values of multiple variables (or expressions), use the ‘if...else’ structure

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

Loops

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

JavaScript’s Looping Structures while for …

Decimal to Binary Conversion in JavaScript The condition enclosed in parentheses 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 ) ;

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

while: Example 3 x = 1 ; while ( x < 6000 ) { document.write ( x ) ; x = x + 1 ; }

JavaScript’s Looping Structures while for …

for: Example 1 x = 1 ; while ( x < 6000 ) { document.write ( x ) ; x = x + 1 ; } Initial count Condition Operation for ( x = 1 ; x < 6000 ; x = x + 1 ) { document.write ( x ) ; }

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

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

for: Example 2 for ( x = 99 ; x < 6000 ; x = x + 1 ) { document.write ( x ) ; }

for: Example 3 for ( x = 6000 ; x > 0 ; x = x - 1 ) { document.write ( x ) ; } How many iterations would this ‘for’ loop run for? 6000?

for: Example 4 for ( x = 6000 ; x < 0 ; x = x - 1 ) { document.write ( x ) ; } How many iterations would this ‘for’ loop run for? None?

? for while

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

During Today’s Lecture … We discussed the concept of flow control using the “if” and “switch” structures And also the concept behind the “while” and “for” looping structures We also solved simple problems using flow control and loop structures

Next (the 9th) Web Dev Lecture: Arrays We will find out why we need arrays We will become able to use arrays for solving simple problems