SlideShare a Scribd company logo
1 of 27
Z
Week 12:
JavaScript
Conditional Statement
Subject Code: COMP121
By: Marlon Jamera
Email: mbjamera@ama.edu.ph
Z
JavaScript
Conditional Statement
Z
Scope of the Lesson
• Conditional Statements
• The if statement
• The else statement
• The switch statement
• The break Keyword
• Different Kind of Loops
• JavaScript for loop
• JavaScript for/in loop
• JavaScript while loop
• JavaScript do/while loop
Z
Learning Outcomes
By the end of the lesson, you will be
familiar and know how the website works
using JavaScripts.
• Discuss the introduction to JavaScript
and using conditional statements.
• Understand the coding syntax using the
break keywords.
• Explain thoroughly the coding styles of
different kinds of loops.
Z
Conditional Statements
• Conditional statements are used to
perform different actions based on
different conditions.
• Very often when we write code, we
wanted to perform different actions for
different decisions.
• We can use conditional statements in our
code to do this.
Z
Conditional Statements
• In JavaScript, we have the following
conditional statements:
• Use if to specify a block of code to be
executed, if a specified condition is true
• Use else to specify a block of code to
be executed, if the same condition is
false.
• Use else if to specify a new condition
to test, if the first condition is false.
• Use switch to specify many alternative
blocks of code to be executed.
Z
The IF Statement
• Use if to specify a block of code to be
executed, if a specified condition is true.
• Syntax:
if (condition) {
block of code to be executed if
the condition is true
}
• Note that if is in lower case. Uppercase
letters will generate a JavaScript error.
Z
The IF Statement
• Example:
• Make a "Good day" greeting if the hour
is less than 18:00:
if (hour < 18) {
greeting = "Good day";
}
• The result of greeting will be:
• Good day
Z
The ELSE Statement
• Use else to specify a block of code to be
executed, if the same condition is false.
if (condition) {
block of code to be executed if
the condition is true
} else {
block of code to be executed if
the condition is false
}
Z
The ELSE Statement
• Example:
• If the hour is less than 18, create a "Good
day" greeting, otherwise "Good evening":
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
• The result of greeting will be:
• Good day
Z
The ELSE IF Statement
• Use else if to specify a new condition to
test, if the first condition is false.
if (condition1) {
block of code to be executed if
condition1 is true
} else if (condition2) {
block of code to be executed if
the condition1 is false and condition2
is true
} else {
block of code to be executed if
the condition1 is false and condition2
is false
}
Z
The ELSE IF Statement
• Example:
• If time is less than 10:00, create a "Good
morning" greeting, if not, but time is less
than 20:00, create a "Good day" greeting,
otherwise a "Good evening":
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
Z
The SWITCH Statement
• Use switch to specify many alternative
blocks of code to be executed.
• Syntax:
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
default code block
}
Z
The SWITCH Statement
• How switch works?
• The switch expression is evaluated
once.
• The value of the expression is
compared with the value of each case.
• If there is a match, the associated
block of code is executed.
Z
The SWITCH Statement
• Example:
• The getDay() method returns the
weekday as a number between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2 ..)
• Use the weekday number to calculate
weekday name.
Z
The Break Keyword
• When the JavaScript code interpreter
reaches a break keyword, it breaks out of
the switch block.
• This will stop the execution of more
code and case testing inside the block.
• Note: When a match is found, and the
job is done, it is time for a break. There is
no need for more testing.
Z
Different Kinds of Loops
• JavaScript supports different kinds of
loops:
• for – loops through a block of code a
number of times.
• for/in – loops through the properties
of an object.
• while – loops through a block of code
while a specified condition is true.
• do/while - also loops through a block
of code while specified condition is
true.
Z
JavaScript FOR LOOP
• Loops can execute a block of code a
number of times.
• Loops are handy, if you want to run the
same code over and over again, each time
with a different value. Often this is the
case when working with arrays:
text += cars[0] + "<br>";
text += cars[1] + "<br>";
text += cars[2] + "<br>";
text += cars[3] + "<br>";
text += cars[4] + "<br>";
text += cars[5] + "<br>";
Z
JavaScript FOR LOOP
• The for loop is often the tool you will
use when you want to create a loop.
• The for loop has the following syntax:
for (statement 1; statement
2; statement 3) {
code block to be executed
}
Z
JavaScript FOR LOOP
•Often this is the case when working with
arrays:
• We can write:
text += cars[0] + "<br>";
text += cars[1] + "<br>";
text += cars[2] + "<br>";
text += cars[3] + "<br>";
text += cars[4] + "<br>";
text += cars[5] + "<br>";
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
Z
JavaScript FOR LOOP
• Statement1 is executed before the loop
(the code block) starts.
• Statement2 defines the condition running
the loop (the code block)
• Statement3 is executed each time after
the loop (the code block) has been
executed.
• Example:
for (i = 0; i < 5; i++) {
text += "The number is " + i
+ "<br>";
}
Z
JavaScript FOR/IN LOOP
• The JavaScript for/in statement loops
through the properties of an object:
var person = {fname:"John",
lname:"Doe", age:25};
var text = "";
var x;
for (x in person) {
text += person[x];
}
Z
JavaScript WHILE LOOP
• The while loop loops through a block of
code as long as a specified condition is
true.
• Syntax:
while (condition) {
code block to be executed
}
Z
JavaScript WHILE LOOP
• Example:
• The code in the loop will run over and
over again, as long as variable (i) is less
than 10.
while (i < 10) {
text += "The number is " + i;
i++;
}
Z
JavaScript DO/WHILE LOOP
• The do/while loop is a variant of the
while loop. This loop will execute the
code block once, before checking if the
condition is true, then it will repeat the
loop as long as the condition is true.
• Syntax:
do {
code block to be executed
}
while (condition);
Z
JavaScript DO/WHILE LOOP
• Example:
• The loop will always be executed at least
once, even if the condition is false,
because the code block is executed before
the condition is tested:
do {
text += "The number is " + i;
i++;
}
while (i < 10);
Z
Let’s call it a day,
Thank you!

More Related Content

What's hot

Looping statement
Looping statementLooping statement
Looping statement
ilakkiya
 

What's hot (20)

JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Javascript
JavascriptJavascript
Javascript
 
Java script
Java scriptJava script
Java script
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Java script errors &amp; exceptions handling
Java script  errors &amp; exceptions handlingJava script  errors &amp; exceptions handling
Java script errors &amp; exceptions handling
 
Javascript
JavascriptJavascript
Javascript
 
Looping statement
Looping statementLooping statement
Looping statement
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Javascript
JavascriptJavascript
Javascript
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 

Viewers also liked

Viewers also liked (8)

She Looks Just Like Me 2017
She Looks Just Like Me 2017She Looks Just Like Me 2017
She Looks Just Like Me 2017
 
Evolutionary Algorithms
Evolutionary AlgorithmsEvolutionary Algorithms
Evolutionary Algorithms
 
High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010
 
Fundamental JavaScript [In Control 2009]
Fundamental JavaScript [In Control 2009]Fundamental JavaScript [In Control 2009]
Fundamental JavaScript [In Control 2009]
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
 
High Performance JavaScript - WebDirections USA 2010
High Performance JavaScript - WebDirections USA 2010High Performance JavaScript - WebDirections USA 2010
High Performance JavaScript - WebDirections USA 2010
 
JavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternJavaScript Prototype and Module Pattern
JavaScript Prototype and Module Pattern
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 

Similar to JavaScript Conditional Statements

Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
Connex
 
Intro to javascript (6:19)
Intro to javascript (6:19)Intro to javascript (6:19)
Intro to javascript (6:19)
Thinkful
 

Similar to JavaScript Conditional Statements (20)

Unit - 4 all script are here Javascript.pptx
Unit - 4 all script are here Javascript.pptxUnit - 4 all script are here Javascript.pptx
Unit - 4 all script are here Javascript.pptx
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptx
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Build tic tac toe with javascript (4:11 dc)
Build tic tac toe with javascript (4:11 dc)Build tic tac toe with javascript (4:11 dc)
Build tic tac toe with javascript (4:11 dc)
 
JavaScript Comprehensive Overview
JavaScript Comprehensive OverviewJavaScript Comprehensive Overview
JavaScript Comprehensive Overview
 
MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS
MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JSMSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS
MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS
 
tick cross game
tick cross gametick cross game
tick cross game
 
Intro to JavaScript - Week 3: Control Statements
Intro to JavaScript - Week 3: Control StatementsIntro to JavaScript - Week 3: Control Statements
Intro to JavaScript - Week 3: Control Statements
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
 
JSLT: JSON querying and transformation
JSLT: JSON querying and transformationJSLT: JSON querying and transformation
JSLT: JSON querying and transformation
 
JavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuanJavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuan
 
csc ppt 15.pptx
csc ppt 15.pptxcsc ppt 15.pptx
csc ppt 15.pptx
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
 
JavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfJavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdf
 
Intro to javascript (6:27)
Intro to javascript (6:27)Intro to javascript (6:27)
Intro to javascript (6:27)
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
JavaScript Looping Statements
JavaScript Looping StatementsJavaScript Looping Statements
JavaScript Looping Statements
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Intro to javascript (6:19)
Intro to javascript (6:19)Intro to javascript (6:19)
Intro to javascript (6:19)
 

More from Marlon Jamera

More from Marlon Jamera (17)

Tables and Forms in HTML
Tables and Forms in HTMLTables and Forms in HTML
Tables and Forms in HTML
 
Images and Lists in HTML
Images and Lists in HTMLImages and Lists in HTML
Images and Lists in HTML
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
ICT in Society
ICT in SocietyICT in Society
ICT in Society
 
ICT in Business
ICT in BusinessICT in Business
ICT in Business
 
The Future of ICT
The Future of ICTThe Future of ICT
The Future of ICT
 
Trends in the Database
Trends in the DatabaseTrends in the Database
Trends in the Database
 
Trends in Database Management
Trends in Database ManagementTrends in Database Management
Trends in Database Management
 
How the Web Works Using HTML
How the Web Works Using HTMLHow the Web Works Using HTML
How the Web Works Using HTML
 
Basic Concept of Database
Basic Concept of DatabaseBasic Concept of Database
Basic Concept of Database
 
Website Basics and Categories
Website Basics and CategoriesWebsite Basics and Categories
Website Basics and Categories
 
Trends In Telecommunications
Trends In TelecommunicationsTrends In Telecommunications
Trends In Telecommunications
 
Software Trends
Software TrendsSoftware Trends
Software Trends
 
Hardware Technology Trends
Hardware Technology TrendsHardware Technology Trends
Hardware Technology Trends
 
Familiarization with Web Tools
Familiarization with Web ToolsFamiliarization with Web Tools
Familiarization with Web Tools
 
Internet Applications
Internet ApplicationsInternet Applications
Internet Applications
 
Introduction to World Wide Web
Introduction to World Wide WebIntroduction to World Wide Web
Introduction to World Wide Web
 

Recently uploaded

一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
pxcywzqs
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
ayvbos
 
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
apekaom
 
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
c6eb683559b3
 
一比一原版澳大利亚迪肯大学毕业证如何办理
一比一原版澳大利亚迪肯大学毕业证如何办理一比一原版澳大利亚迪肯大学毕业证如何办理
一比一原版澳大利亚迪肯大学毕业证如何办理
SS
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
F
 

Recently uploaded (20)

Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime BalliaBallia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
 
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
一比一原版桑佛德大学毕业证成绩单申请学校Offer快速办理
 
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
一比一原版澳大利亚迪肯大学毕业证如何办理
一比一原版澳大利亚迪肯大学毕业证如何办理一比一原版澳大利亚迪肯大学毕业证如何办理
一比一原版澳大利亚迪肯大学毕业证如何办理
 
Down bad crying at the gym t shirtsDown bad crying at the gym t shirts
Down bad crying at the gym t shirtsDown bad crying at the gym t shirtsDown bad crying at the gym t shirtsDown bad crying at the gym t shirts
Down bad crying at the gym t shirtsDown bad crying at the gym t shirts
 
Local Call Girls in Jharsuguda 9332606886 HOT & SEXY Models beautiful and ch...
Local Call Girls in Jharsuguda  9332606886 HOT & SEXY Models beautiful and ch...Local Call Girls in Jharsuguda  9332606886 HOT & SEXY Models beautiful and ch...
Local Call Girls in Jharsuguda 9332606886 HOT & SEXY Models beautiful and ch...
 
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
Dubai Call Girls First Class O525547819 Call Girls Dubai Hot New Girlfriend
Dubai Call Girls First Class O525547819 Call Girls Dubai Hot New GirlfriendDubai Call Girls First Class O525547819 Call Girls Dubai Hot New Girlfriend
Dubai Call Girls First Class O525547819 Call Girls Dubai Hot New Girlfriend
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 

JavaScript Conditional Statements

  • 1. Z Week 12: JavaScript Conditional Statement Subject Code: COMP121 By: Marlon Jamera Email: mbjamera@ama.edu.ph
  • 3. Z Scope of the Lesson • Conditional Statements • The if statement • The else statement • The switch statement • The break Keyword • Different Kind of Loops • JavaScript for loop • JavaScript for/in loop • JavaScript while loop • JavaScript do/while loop
  • 4. Z Learning Outcomes By the end of the lesson, you will be familiar and know how the website works using JavaScripts. • Discuss the introduction to JavaScript and using conditional statements. • Understand the coding syntax using the break keywords. • Explain thoroughly the coding styles of different kinds of loops.
  • 5. Z Conditional Statements • Conditional statements are used to perform different actions based on different conditions. • Very often when we write code, we wanted to perform different actions for different decisions. • We can use conditional statements in our code to do this.
  • 6. Z Conditional Statements • In JavaScript, we have the following conditional statements: • Use if to specify a block of code to be executed, if a specified condition is true • Use else to specify a block of code to be executed, if the same condition is false. • Use else if to specify a new condition to test, if the first condition is false. • Use switch to specify many alternative blocks of code to be executed.
  • 7. Z The IF Statement • Use if to specify a block of code to be executed, if a specified condition is true. • Syntax: if (condition) { block of code to be executed if the condition is true } • Note that if is in lower case. Uppercase letters will generate a JavaScript error.
  • 8. Z The IF Statement • Example: • Make a "Good day" greeting if the hour is less than 18:00: if (hour < 18) { greeting = "Good day"; } • The result of greeting will be: • Good day
  • 9. Z The ELSE Statement • Use else to specify a block of code to be executed, if the same condition is false. if (condition) { block of code to be executed if the condition is true } else { block of code to be executed if the condition is false }
  • 10. Z The ELSE Statement • Example: • If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening": if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } • The result of greeting will be: • Good day
  • 11. Z The ELSE IF Statement • Use else if to specify a new condition to test, if the first condition is false. if (condition1) { block of code to be executed if condition1 is true } else if (condition2) { block of code to be executed if the condition1 is false and condition2 is true } else { block of code to be executed if the condition1 is false and condition2 is false }
  • 12. Z The ELSE IF Statement • Example: • If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a "Good day" greeting, otherwise a "Good evening": if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; }
  • 13. Z The SWITCH Statement • Use switch to specify many alternative blocks of code to be executed. • Syntax: switch(expression) { case n: code block break; case n: code block break; default: default code block }
  • 14. Z The SWITCH Statement • How switch works? • The switch expression is evaluated once. • The value of the expression is compared with the value of each case. • If there is a match, the associated block of code is executed.
  • 15. Z The SWITCH Statement • Example: • The getDay() method returns the weekday as a number between 0 and 6. (Sunday=0, Monday=1, Tuesday=2 ..) • Use the weekday number to calculate weekday name.
  • 16. Z The Break Keyword • When the JavaScript code interpreter reaches a break keyword, it breaks out of the switch block. • This will stop the execution of more code and case testing inside the block. • Note: When a match is found, and the job is done, it is time for a break. There is no need for more testing.
  • 17. Z Different Kinds of Loops • JavaScript supports different kinds of loops: • for – loops through a block of code a number of times. • for/in – loops through the properties of an object. • while – loops through a block of code while a specified condition is true. • do/while - also loops through a block of code while specified condition is true.
  • 18. Z JavaScript FOR LOOP • Loops can execute a block of code a number of times. • Loops are handy, if you want to run the same code over and over again, each time with a different value. Often this is the case when working with arrays: text += cars[0] + "<br>"; text += cars[1] + "<br>"; text += cars[2] + "<br>"; text += cars[3] + "<br>"; text += cars[4] + "<br>"; text += cars[5] + "<br>";
  • 19. Z JavaScript FOR LOOP • The for loop is often the tool you will use when you want to create a loop. • The for loop has the following syntax: for (statement 1; statement 2; statement 3) { code block to be executed }
  • 20. Z JavaScript FOR LOOP •Often this is the case when working with arrays: • We can write: text += cars[0] + "<br>"; text += cars[1] + "<br>"; text += cars[2] + "<br>"; text += cars[3] + "<br>"; text += cars[4] + "<br>"; text += cars[5] + "<br>"; for (i = 0; i < cars.length; i++) { text += cars[i] + "<br>";
  • 21. Z JavaScript FOR LOOP • Statement1 is executed before the loop (the code block) starts. • Statement2 defines the condition running the loop (the code block) • Statement3 is executed each time after the loop (the code block) has been executed. • Example: for (i = 0; i < 5; i++) { text += "The number is " + i + "<br>"; }
  • 22. Z JavaScript FOR/IN LOOP • The JavaScript for/in statement loops through the properties of an object: var person = {fname:"John", lname:"Doe", age:25}; var text = ""; var x; for (x in person) { text += person[x]; }
  • 23. Z JavaScript WHILE LOOP • The while loop loops through a block of code as long as a specified condition is true. • Syntax: while (condition) { code block to be executed }
  • 24. Z JavaScript WHILE LOOP • Example: • The code in the loop will run over and over again, as long as variable (i) is less than 10. while (i < 10) { text += "The number is " + i; i++; }
  • 25. Z JavaScript DO/WHILE LOOP • The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. • Syntax: do { code block to be executed } while (condition);
  • 26. Z JavaScript DO/WHILE LOOP • Example: • The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested: do { text += "The number is " + i; i++; } while (i < 10);
  • 27. Z Let’s call it a day, Thank you!

Editor's Notes

  1. Left angle bracket < Right angle bracket >
  2. <html> <body> <p>Display "Good day!" if the hour is less than 18:00:</p> <p id="demo">Good Evening!</p> <script> if (new Date().getHours() < 18) { document.getElementById("demo").innerHTML = "Good day!"; } </script> </body> </html>
  3. <html> <body> <p>Click the button to display a time-based greeting:</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var hour = new Date().getHours(); var greeting; if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } document.getElementById("demo").innerHTML = greeting; } </script> </body> </html>
  4. <html> <body> <p>Click the button to get a time-based greeting:</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var greeting; var time = new Date().getHours(); if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; } document.getElementById("demo").innerHTML = greeting; } </script> </body> </html>
  5. <html> <body> <p id="demo"></p> <script> var day; switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break; } document.getElementById("demo").innerHTML = "Today is " + day; </script> </body> </html>
  6. <html> <body> <p id="demo"></p> <script> var cars = ["BMW", "Volvo", "Saab", "Ford", "Mazda"]; var text = ""; var i; for (i = 0; i < cars.length; i++) { text += cars[i] + "<br>"; } document.getElementById("demo").innerHTML = text; </script> </body> </html>
  7. <html> <body> <p>Click the button to loop through a block of code five times.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var text = ""; var i; for (i = 0; i < 5; i++) { text += "The number is " + i + "<br>"; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>
  8. <html> <body> <p id="demo"></p> <script> var txt = ""; var person = {fname:"John", lname:"Doe", age:25}; var x; for (x in person) { txt += person[x] + " "; } document.getElementById("demo").innerHTML = txt; </script> </body> </html>
  9. <html> <body> <p>Click the button to loop through a block of code as long as i is less than 10.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var text = ""; var i = 0; while (i < 10) { text += "<br>The number is " + i; i++; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>
  10. <html> <body> <p>Click the button to loop through a block of code as long as i is less than 10.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var text = "" var i = 0; do { text += "<br>The number is " + i; i++; } while (i < 10) document.getElementById("demo").innerHTML = text; } </script> </body> </html>
  11. http://www.tizag.com/javascriptT/javascriptevents.php