Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
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.
IM 215 Operators, Conditions and Loops. Review Nature of Javascript How to include Javascript on a page Declaring and assigning variables Commenting code.
Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
The switch Statement, DecimalFormat, and Introduction to Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 9 Interactive Multimedia Authoring with Flash - Introduction to Programming “Computers and Creativity” Richard D. Webster, COSC 109 Instructor.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Understanding Mathematical Operators For a mathematical calculation, you use a mathematical operator. The values that you use can be any sort of values.
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.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
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.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Working with Loops, Conditional Statements, and Arrays.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JavaScript, Sixth Edition
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Learning Javascript From Mr Saem
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CHAPTER 4 DECISIONS & LOOPS
CHAPTER 10 JAVA SCRIPT.
JavaScript Loops.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Loop Control Structure.
Chapter 10 Programming Fundamentals with JavaScript
During the last lecture we had a discussion on Data Types, Variables & Operators
3 Control Statements:.
Intro to Programming CURIE 2011.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Computer Science Core Concepts
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
BIT116: Scripting Lecture 6 Part 1
Selections and Loops By Sarah, Melody, Teresa.
If-Statements and If/Else Statements
CSE 206 Course Review.
How to allow the program to know when to stop a loop.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Conditional Statements and Loops

Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure Identify various types of conditional statements and loops Know how conditional statements and loops are used in scripts

Defining Conditional Statements What is a conditional statement? Why conditionals are useful?

What is a Conditional Statement? A statement that is used to execute a bit of code based on a condition or to do something else when the condition is not met It’s a bit like cause and effect –If you study hard, you will pass the course. Otherwise, you will fail.

What is a Conditional Statement? Example: –If a variable named my money is greater than 1000, send an alert that says my finances are ok. Otherwise, send an alert saying I need more money!

Why are Conditionals Useful? Let’s us execute only certain parts of the script instead of executing every single line of code in the script

Types of Conditional Statements If/Else Statement Blocks Switch Statement Blocks

If/Else Statement Blocks Structure Block Nesting Complex Comparisons

If/Else Statement Block Structure if (comparison here) We want to see if a variable named ‘boats’ is equal to 3.

If/Else Statement Block Structure if (boat==3)

If/Else Statement Block Structure if (boat==3) { JavaScript Statements Here }

If/Else Statement Block Structure if (boat==3) { JavaScript Statements Here } else { JavaScript Statements Here }

Problem 1 Send an alert that says “You have the right number of boats” if the variable boats is equal to three. If it is not, we want to send an alert that says “You do not have the right number of boats” instead.

<!-- if (boats == 3) { window.alert("You have the right number of boats."); } else { window.alert("You do not have the right number of boats."); } //-->

Let’s declare a variable and assign it a value …

<!-- var boats = 3; if (boats == 3) { window.alert("You have the right number of boats."); } else { window.alert("You do not have the right number of boats."); } //-->

Now change the value of the variable …

<!-- var boats = 0; if (boats == 3) { window.alert("You have the right number of boats."); } else { window.alert("You do not have the right number of boats."); } //-->

If/Else Statement Block Nesting During nesting, we put one structure inside another structure of a similar nature

Problem 2 If variable named ‘car’ is equal to yes, and if a variable named ‘licence’ is equal to yes, send an alert that says ‘You can drive’ to the browser. If variable named ‘car’ is equal to yes, but if a variable named ‘licence’ is not equal to yes, send an alert that says ‘You cannot drive’ to the browser; otherwise send an alert that says ‘You need a car’.

<!-- if (car == "yes") { if (licence == "yes") { window.alert("You can drive."); } else { window.alert("You need a licence to drive."); } else { window.alert("You need a car."); } //-->

Oops, I made a mistake …

<!-- if (car == "yes") { if (licence == "yes") { window.alert("You can drive."); } else { window.alert("You need a licence to drive."); } else { window.alert("You need a car."); } //-->

Let’s declare some variables and assign them values …

<!-- var car = "yes"; var licence = "yes"; if (car == "yes") { if (licence == "yes") { window.alert("You can drive."); } else { window.alert("You need a licence to drive."); } else { window.alert("You need a car."); } //-->

Now change the values of the variables …

<!-- var car = "yes"; var licence = “no"; if (car == "yes") { if (licence == "yes") { window.alert("You can drive."); } else { window.alert("You need a licence to drive."); } else { window.alert("You need a car."); } //-->

and changing the values of the variables again …

<!-- var car = "no"; var licence = "no"; if (car == "yes") { if (licence == "yes") { window.alert("You can drive."); } else { window.alert("You need a licence to drive."); } else { window.alert("You need a car."); } //-->

Another example …

if (car == "yes") { if (licence == "yes") { window.alert("You can drive."); } else { window.alert("You need a licence to drive."); } else { if (helicopter == "yes") { window.alert("Use the helicopter."); } else { window.alert("You need a car."); }

Let’s declare some variables and assign them values …

var car = "no"; var licence = "no"; var helicopter = "yes"; if (car == "yes") { if (licence == "yes") { window.alert("You can drive."); } else { window.alert("You need a licence to drive."); } else { if (helicopter == "yes") { window.alert("Use the helicopter."); } else { window.alert("You need a car."); }

Switch Statements Allows us to take a single variable value and execute a different line of code based on the value of the variable.

Example var thename = "Salman"; switch (thename) { case “Naveed”: window.alert("Naveed is an OK name."); break; case “Salman”: window.alert(“Salman is a great name!"); window.alert("Hi Salman!"); break; default: window.alert("You have a unique name."); }

What is a Loop? A block of code that allows us to repeat a section of code a certain number of times, perhaps changing certain variable values each time the code is executed.

document.write(“Hello. Welcome to the world.”);

We can write this in a more efficient manner using loops …

Do this block 10 times { document.write(“Hello. Welcome to the world.”); }

Types of Loops For loops While loops

For Loops Structure Block Nesting

Structure of a For Loop for (statement) { JavaScript goes here }

Structure of a For Loop for (varname = 1; varname <11; varname +=1) { JavaScript code goes here }

for (count = 1; count < 11; count += 1) { document.write(“Hello. Welcome to the world.”); }

Oops, I made a mistake …

for (count = 1; count < 11; count += 1) { document.write(“Hello. Welcome to the world. ”); }

Nesting For Loops We can have nested loops just like If/Else blocks

for (count = 1; count < 5; count += 1) { document.write(count+“Hello. Welcome to the world. ”); for (nestcount=1, nestcount<3; nestcount+=1) { document.write(“Stop!”); }

for (count = 1; count < 11; count += 1) { if (count = 5) { document.write(“I’m halfway through.”); } else { document.write(“I’m part of a loop.”); }

While Loops Looks at short comparison and repeats until comparison is no longer true

var count = 1; while (count < 11) { document.write(“I’m part of a loop. ”); count +=1; }

Do While Loops Loop executed at least once, even if comparison used return false the first time.

var count = 1; do { document.write(“Hi!”); count +=1; }while (count < 6);

var count = 11; do { document.write(“Hi!”); count +=1; }while (count < 10);

What We Learnt Today … Learnt about conditional statements and their structure Learnt about loops and their structure Identified various types of conditional statements and loops Found out how conditional statements and loops are used in scripts