Conditional Statements & Loops in JavaScript

Slides:



Advertisements
Similar presentations
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Advertisements

Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Introduction to JavaScript for Python Programmers
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
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.
CIS 375—Web App Dev II JavaScript II.
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.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
LOGO Conditional Statements & Loops in JavaScript CHAPTER 10 Eastern Mediterranean University School of Computing and Technology Department of Information.
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.
JavaScript Syntax, how to use it in a HTML document
JavaScript, Fourth Edition
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
 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)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CSI 3125, Preliminaries, page 1 Control Statements.
COMP Loop Statements Yi Hong May 21, 2015.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
JavaScript, Sixth Edition
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
Learning Javascript From Mr Saem
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
Computer Programming -1-
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
CHAPTER 10 JAVA SCRIPT.
Web Systems & Technologies
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Control Structures.
JavaScript Syntax and Semantics
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Conditional Statements and Loops
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
JavaScript: Control Statements I
Chapter 19 JavaScript.
Introduction to Client-Side Scripting and JavaScript
Loop Control Structure.
- Additional C Statements
IF if (condition) { Process… }
Chapter 8 JavaScript: Control Statements, Part 2
Introduction to JavaScript for Python Programmers
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 2.2 Control Structures (Iteration)
Lab5 PROGRAMMING 1 Loop chapter4.
2.6 The if/else Selection Structure
Dale Roberts, Lecturer IUPUI
BIT116: Scripting Lecture 6 Part 1
Control Statements Paritosh Srivastava.
PROGRAM FLOWCHART Iteration Statements.
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Conditional Statements & Loops in JavaScript Eastern Mediterranean University School of Computing and Technology Department of Information Technology ITEC229 Client-Side Internet and Web Programming Conditional Statements & Loops in JavaScript CHAPTER 10

CONTENT 10.1 Conditional Statements if.. Statement if.. else Statement if.. else if Statement switch case Statement 10.2 Loops For Loop While Loop Do.. While Loop http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements Conditional statements are used to perform different actions based on different conditions. 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. http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements In JavaScript we have the following conditional statements: if statement - use this statement to execute some code only if a specified condition is true if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements if statement: Use the if statement to execute some code only if a specified condition is true. Syntax: if (condition)   {   code to be executed if condition is true   } Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error! Notice that there is no ..else.. in this syntax. You tell the browser to execute some code only if the specified condition is true. http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements if statement: Example: <html><head><title></title></head><body> <script type="text/javascript"> var total=0; var even=0; document.write( "Raygan"); for (x=1; x<=10; x++) { total = total + x; if ((x%2)==0) { even = even + x; } } document.write( "The total sum: "+total+"<br>"); document.write( "The sum of even values:"+ even ); </script> </body> </html> http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements if.. else statement: Syntax: if (expression) { statement(s) to be executed if expression is true } else statement(s) to be executed if expression is false http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements if.. else statement: Example: <script type="text/javascript"> var total = prompt("Enter your total", "50"); if (total >=50) {alert("PASSED")} else {alert("FAILED")} </script> http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements if.. else if statement: Syntax: if (expression 1) { statement(s) to be executed if expression 1 is true } else if (expression 2) { statement(s) to be executed if expression 2 is true } else if (expression 3) { statement(s) to be executed if expression 3 is true } else { statement(s) to be executed if no expression is true } http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements if.. else if statement: Example: <script type="text/javascript"> var age = prompt("Enter your age", "25"); if (age >0 && age <= 3) {alert("WOW Baby")} else if (age >= 4 && age < 13) {alert("You are child")} else if (age >= 13 && age < 31) {alert("You are young ;)")} else if (age >= 31 && age < 46) {alert("Middle Age :/")} else if (age >= 46 && age < 71) {alert("You are old :(")} else if (age >= 71 && age < 101) {alert("Get ready for a new world!!!")} else {alert("Ageless")} </script> http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements switch case statement: Syntax: switch (expression) { case condition 1: statement(s) break; case condition 2: statement(s) ... case condition n: statement(s) default: statement(s) } http://sct.emu.edu.tr/it/itec229

10.1 Conditional Statements switch case statement: Example: <body> <script type="text/javascript"> var flower=prompt("Which flower whould you like to buy?","rose"); switch (flower) { case "rose": alert(flower + " costs 10TL"); break; case "daisy": alert(flower + " costs 3TL"); case "orchild": alert(flower + " costs 25TL"); default: alert("SORRY =( there is no such flower in our shop"); break; } </script> </body> http://sct.emu.edu.tr/it/itec229

10.2 Loops For Loop: For loop is the most compact form of looping and includes the following three important parts: the loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. The test statement which will test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out. The iteration statement where you can increase or decrease your counter. http://sct.emu.edu.tr/it/itec229

10.2 Loops For Loop: You can put all the three parts in a single line separated by a semicolon. Syntax: for (initialization; test condition; iteration statement) { statement(s) to be executed if test condition is true } http://sct.emu.edu.tr/it/itec229

10.2 Loops For Loop: Example: <script type="text/javascript"> document.write("<h2>Multiplication table from (1 to 10)</h2>"); document.write("<table border=2 width=50%"); for (var i = 1; i <= 10; i++ ) { //this is the outer loop document.write("<tr>"); document.write("<td>" + i + "</td>"); for ( var j = 2; j <= 10; j++ ) { // inner loop document.write("<td>" + i * j + "</td>"); } document.write("</tr>"); document.write("</table>"); </script> http://sct.emu.edu.tr/it/itec229

10.2 Loops For Loop: Example Output: http://sct.emu.edu.tr/it/itec229

10.2 Loops The while Loop:  loops through a block of code as long as a specified condition is true. Syntax: while (condition) { statement(s) to be executed if expression is true } http://sct.emu.edu.tr/it/itec229

10.2 Loops The while Loop: Example: <script type="text/javascript"> var total=0; var i=0; var j; while (i<= 10) { j=i%2; if (j!== 0) { total+=i;} i++ } document.write("Total of odd numbers between 1-10:" +" +total); </script> Output: Total of odd numbers between 1-10: 25 http://sct.emu.edu.tr/it/itec229

10.2 Loops The do..while Loop: similar to the while loop except that the condition check happens at the end of the loop. this means that the loop will always be executed at least once, even if the condition is false. Syntax: do { statement(s) to be executed; } while (condition); http://sct.emu.edu.tr/it/itec229

10.2 Loops The do..while Loop: Example: Output: 5 10 15 20 25 <script type="text/javascript"> var num = 0; do{ num = num + 5; document.write(num + "<br />"); } while (num < 25); </script> Output: 5 10 15 20 25 http://sct.emu.edu.tr/it/itec229

Conditional Statements & Loops in JavaScript Thank You ! Conditional Statements & Loops in JavaScript END of CHAPTER 10 http://sct.emu.edu.tr/it/itec229