CHAPTER 4 DECISIONS & LOOPS

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
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.
Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure.
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions.
IDK0040 Võrgurakendused I harjutus 07: PHP: Operators, Switch, Forms Deniss Kumlander.
Its all just code by group 8 张亚东,杨杰,甘伟,余青龙,肖春亮 chapter 2.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
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.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Logic and Control. 4-2 Decision (selection) structure: if (condition) { statement etc. } Example: if (age == 15) { document.write(“ you are a fifteen”);
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript Loops Please use speaker notes for additional information!
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LESSON 2: CONTROL STRUCTURES JAVASCRIPT. CONTROL STRUCTURES – ALLOWS US TO CHANGE THE ORDERING OF HOW THE STATEMENTS IN OUR PROGRAMS ARE EXECUTED.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Learning Javascript From Mr Saem
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
 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.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Sensor Information: while loops and Boolean Logic.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Core Java Statements in Java.
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
Robotics Programming Using Shaft Encoders
Chapter 6: Conditional Statements and Loops
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Movement using Shaft Encoders
Chapter 19 JavaScript.
Chapter 5 Repetition.
Chapter 10 Programming Fundamentals with JavaScript
IF if (condition) { Process… }
While Loops and If-Else Structures
Chapter 9 Control Structures.
Selection CSCE 121 J. Michael Moore.
Logical Operations In Matlab.
Lab5 PROGRAMMING 1 Loop chapter4.
Objectives In this chapter, you will:
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Computer Science Core Concepts
Robotics Programming Using Shaft Encoders
Robotics Programming Using Shaft Encoders
Program Flow.
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.
Decisions, decisions, decisions
Decision making and control functions
If-Statements and If/Else Statements
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

CHAPTER 4 DECISIONS & LOOPS

A script can do different things depending on what values it has been passed.

MAKING DECISIONS

Is test score greater than 50? Y Message: Try again… Message: You passed!

if

if (score > 50)

Is test score greater than 50?

if (score > 50) { document.write(‘You passed!’); }

Is test score greater than 50? Y

Is test score greater than 50? Y Message: You passed!

if (score > 50) { document.write(‘You passed!’); } else { }

Is test score greater than 50? Y Message: You passed!

if (score > 50) { document.write(‘You passed!’); } else { document.write(‘Try again…’); }

Is test score greater than 50? Y Message: Try again… Message: You passed!

Is test score greater than 50? Y Message: Try again… Message: You passed! CONTINUE SCRIPT…

COMPARISON OPERATORS

= = IS EQUAL TO

= = IS EQUAL TO ! = IS NOT EQUAL TO

= = IS EQUAL TO ! = IS NOT EQUAL TO = = = STRICT EQUAL TO

= = IS EQUAL TO ! = = = = ! = = IS NOT EQUAL TO STRICT EQUAL TO STRICT NOT EQUAL TO

> GREATER THAN

> GREATER THAN < LESS THAN

> GREATER THAN < > = LESS THAN > = GREATER THAN OR EQUAL TO

> GREATER THAN < > = < = LESS THAN > = GREATER THAN OR EQUAL TO < = LESS THAN OR EQUAL TO

LOGICAL OPERATORS

if (score > 75)&&(score < 95) { document.write(‘Very good!’); }

if (score > 75)&&(score < 95) { document.write(‘Very good!’); }

& & LOGICAL AND

& & LOGICAL AND | | LOGICAL OR

& & LOGICAL AND | | LOGICAL OR ! LOGICAL NOT

SWITCH STATEMENTS

switch

switch (level) { }

switch (level) { case ‘One’: title = ‘Level 1’; break; }

switch (level) { case ‘One’: title = ‘Level 1’; break; case ‘Two’: title = ‘Level 2’; break; }

switch (level) { case ‘One’: title = ‘Level 1’; break; case ‘Two’: title = ‘Level 2’; break; case ‘Three’: title = ‘Level 3’; break; }

switch (level) { case ‘One’: title = ‘Level 1’; break; case ‘Two’: title = ‘Level 2’; break; case ‘Three’: title = ‘Level 3’; break; default: title = ‘Test’; break; }

LOOPS

for (var i = 0; i < 3; i++) { document.write(i); }

for (var i = 0; i < 3; i++) { document.write(i); } KEYWORD

for (var i = 0; i < 3; i++) { document.write(i); } CONDITION (COUNTER)

for (var i = 0; i < 3; i++) { document.write(i); } The variable i is declared and set a value of 0 INITIALIZATION

for (var i = 0; i < 3; i++) { document.write(i); } Every time the loop is run, the condition is checked to see if i is less than 3 CONDITION

for (var i = 0; i < 3; i++) { document.write(i); } If i is less than 3, the code block is run

for (var i = 0; i < 3; i++) { document.write(i); } The variable i can be used inside the loop (here it is used to write its value to the page)

for (var i = 0; i < 3; i++) { document.write(i); } When the code inside the curly braces has been executed, the variable i is incremented by 1 UPDATE

ANATOMY OF A LOOP

2 1 i = 0 i = 1 i = 2 i = 3

i = 0

Is i less than 3? i = 0

Yes, i is less than 3 i = 0

Write i to the page i = 0

Add 1 to i i = 0 i = 1

i = 0 i = 1

i = 0 i = 1 Is i less than 3?

i = 0 i = 1 Yes, i is less than 3

i = 0 i = 1 Write i to the page 1

i = 0 i = 1 Add 1 to i i = 2 1

1 i = 0 i = 1 i = 2

1 i = 0 i = 1 i = 2 Is i less than 3?

1 i = 0 i = 1 i = 2 Yes, i is less than 3

1 i = 0 i = 1 i = 2 Write i to the page 2

1 i = 0 i = 1 i = 2 Add 1 to i i = 3 2

2 1 i = 0 i = 1 i = 2 i = 3

2 1 i = 0 i = 1 i = 2 i = 3 Is i less than 3?

2 1 i = 0 i = 1 i = 2 i = 3 No, i is not less than 3

2 1 i = 0 i = 1 i = 2 i = 3 End loop