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

JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced FunctionsWebStackAcademy
 
javascript objects
javascript objectsjavascript objects
javascript objectsVijay Kalyan
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - OperatorsWebStackAcademy
 
Html events with javascript
Html events with javascriptHtml events with javascript
Html events with javascriptYounusS2
 
Java script errors &amp; exceptions handling
Java script  errors &amp; exceptions handlingJava script  errors &amp; exceptions handling
Java script errors &amp; exceptions handlingAbhishekMondal42
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Aaron Gustafson
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 

What's hot (20)

JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Html events with javascript
Html events with javascriptHtml events with javascript
Html events with javascript
 
Html form tag
Html form tagHtml form tag
Html form tag
 
Java script errors &amp; exceptions handling
Java script  errors &amp; exceptions handlingJava script  errors &amp; exceptions handling
Java script errors &amp; exceptions handling
 
Java script
Java scriptJava script
Java script
 
Java script
Java scriptJava script
Java script
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
 
Js ppt
Js pptJs ppt
Js ppt
 
Html forms
Html formsHtml forms
Html forms
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 

Viewers also liked

She Looks Just Like Me 2017
She Looks Just Like Me 2017She Looks Just Like Me 2017
She Looks Just Like Me 2017Reem Alattas
 
Evolutionary Algorithms
Evolutionary AlgorithmsEvolutionary Algorithms
Evolutionary AlgorithmsReem Alattas
 
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 2010Nicholas Zakas
 
Fundamental JavaScript [In Control 2009]
Fundamental JavaScript [In Control 2009]Fundamental JavaScript [In Control 2009]
Fundamental JavaScript [In Control 2009]Aaron Gustafson
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions Reem Alattas
 
High Performance JavaScript - WebDirections USA 2010
High Performance JavaScript - WebDirections USA 2010High Performance JavaScript - WebDirections USA 2010
High Performance JavaScript - WebDirections USA 2010Nicholas Zakas
 
JavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternJavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternNarendra Sisodiya
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 

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

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...Edureka!
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptxStefan Oprea
 
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)Daniel Friedman
 
JavaScript Comprehensive Overview
JavaScript Comprehensive OverviewJavaScript Comprehensive Overview
JavaScript Comprehensive OverviewMohamed Loey
 
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 JSMoataz_Hesham
 
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 StatementsJeongbae Oh
 
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 2Mohd Harris Ahmad Jaal
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building BlocksCate Huston
 
JSLT: JSON querying and transformation
JSLT: JSON querying and transformationJSLT: JSON querying and transformation
JSLT: JSON querying and transformationLars Marius Garshol
 
JavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuanJavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuanWei-Yuan Chang
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java BasicsFayis-QA
 
JavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfJavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfkatarichallenge
 
Intro to javascript (6:27)
Intro to javascript (6:27)Intro to javascript (6:27)
Intro to javascript (6:27)David Coulter
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
 
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)

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...
 
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

Tables and Forms in HTML
Tables and Forms in HTMLTables and Forms in HTML
Tables and Forms in HTMLMarlon Jamera
 
Images and Lists in HTML
Images and Lists in HTMLImages and Lists in HTML
Images and Lists in HTMLMarlon Jamera
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptMarlon Jamera
 
Trends in the Database
Trends in the DatabaseTrends in the Database
Trends in the DatabaseMarlon Jamera
 
Trends in Database Management
Trends in Database ManagementTrends in Database Management
Trends in Database ManagementMarlon Jamera
 
How the Web Works Using HTML
How the Web Works Using HTMLHow the Web Works Using HTML
How the Web Works Using HTMLMarlon Jamera
 
Basic Concept of Database
Basic Concept of DatabaseBasic Concept of Database
Basic Concept of DatabaseMarlon Jamera
 
Website Basics and Categories
Website Basics and CategoriesWebsite Basics and Categories
Website Basics and CategoriesMarlon Jamera
 
Trends In Telecommunications
Trends In TelecommunicationsTrends In Telecommunications
Trends In TelecommunicationsMarlon Jamera
 
Hardware Technology Trends
Hardware Technology TrendsHardware Technology Trends
Hardware Technology TrendsMarlon Jamera
 
Familiarization with Web Tools
Familiarization with Web ToolsFamiliarization with Web Tools
Familiarization with Web ToolsMarlon Jamera
 
Internet Applications
Internet ApplicationsInternet Applications
Internet ApplicationsMarlon Jamera
 
Introduction to World Wide Web
Introduction to World Wide WebIntroduction to World Wide Web
Introduction to World Wide WebMarlon 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

PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationLinaWolf1
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一z xss
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 
NSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationNSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationMarko4394
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书rnrncn29
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhimiss dipika
 
Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Sonam Pathan
 
Q4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxQ4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxeditsforyah
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作ys8omjxb
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Sonam Pathan
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa494f574xmv
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Paul Calvano
 

Recently uploaded (17)

PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 Documentation
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 
NSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationNSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentation
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhi
 
Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170
 
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
 
Q4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxQ4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptx
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24
 

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