SlideShare a Scribd company logo
Conditional Constructs
if
if-else
if-elseif-else
Conditional Constructs
• Different situations  Different solutions
• Complex problem  Multiple possible
solutions
Problem with problems
• Finding the best fit solution for each problem
• Check different permutations and
combinations
IF
• If a particular condition is true -> perform a
specific action
• Eg:
– if (fruit == "apple")
fruit_color = "Red";
If-else
• If a particular condition is true -> perform a
specific action otherwise perform next action
• Eg:
– if(fruit == "apple")
fruit_color = "Red";
else
fruit_color = "Orange";
If-elseif-else
• If a particular condition is true -> perform a specific
action
• Otherwise check another condition, if true  perform
that specific action
• If none of above conditions is true  perform a
specific action
• Eg:
if(fruit=="red_apple")
fruit_color = "red";
elseif (fruit=="green_apple")
fruit_color="green";
else
fruit_color="orange";
Using Operators
• Check a specific condition
• Analyse the problem
• Find the best fit solution by checking multiple
conditions simulaneously
• Form relationship between different
conditions to be analysed
• Do arithmetic calculations
Operators
X = 10
Operand
Operator
Operand
ASSIGNMENT OPERATOR
=
Assignment Operator
• =
• Assign a value to a variable/constant/array
• Value on the RHS is assigned to the storage on
LHS
• Eg:
apples_bunch = 12;
PI = 3.14;
fruit = "apple";
RELATIONAL OPERATOR
>,<,>=,<=,==,!=
Relational Operator
• Form relationship between two conditions
• Compare the values of parameters
Relational
• To make a comparison while checking a
condition, use relational operators.
Operator Functionality Let’s consider x=5;
y=10;
> More Than x>y; False
< Less Than x<y; True
>= More Than or Equal
To
x>=y; False
<= Less Than or Equal
To
x<=y; True
== Equal to x==y; False
!= Not Equal to x!=y; True
IF
• If a particular condition is true -> perform a
specific action
• Eg:
if (fruit == 'apple')
fruit_color = 'Red';
LOGICAL OPERATOR
&&, ||, !
Logical Operator
• Form relationship between multiple
conditions
• Also known as combinational operator
– Combines different conditions together to find
solution
– AND- &&
– OR- ||
– NOT- !
Logical
• To combine two or more situations of
conditional checking use, logical operators.
Operator Functionality Condition
Example
&& Both the sides of
operator should
result true
x<y && y!=x True
x<y && y==x False
|| Either side of
operator should
result true
x<y || y!=x True
x<y || y!=x True
! Inverse the result x!<y
False
If-else
• Checking multiple conditions and parameter
values.
• Eg:
if(fruit == "apple" && fruit_color == "red")
fruit = "Red Apple";
else
fruit = 'Orange';
If-elseif-else
• Checking multiple conditions
• Eg:
if(fruit=="apple" && fruit_color == "red")
fruit = "Red Apple";
elseif (fruit=="apple" && fruit_color == "green")
fruit = "Green Apple";
else
fruit = "Orange";
ARITHMETIC OPERATOR
+,-,*,/,%
Arithmetic
• To compute arithmetic value we use
Arithmetic Operators.
Operator Functionality Condition
Example
+ Addition x+y 15
- Subtraction x-y -5
* Multiplication x*y 50
/ Division x/y 0
% Remainder x%y 5
Arithmetic Function
• Eg:
if(fruit=="apple" && fruit_color == "red")
red_apple = red_apple+1;
elseif (fruit=="apple" && fruit_color == "green")
green_apple = green_apple+1;
else
orange = orange+1;
UNARY OPERATOR
+,-,++,--
Unary Operator
• Works with a single operand
• There is no RHS (Right hand side) to the
condition
• Performing certain operations with a
shorthand
Unary Operators
• Used with single operand
Operator Functionality Condition
Example
+ Positive Value X= +15
- Negative Value X= -5
++ Increment X++ or ++X
-- Decrement X-- or --X
! NOT !x
Conditional Constructs
and
Operators used in Javascript
https://www.codewizacademy.com
https://www.facebook.com/codewizacademy/
https://www.instagram.com/codewizacademy/

More Related Content

More from Codewizacademy

Rules and policies for course
Rules and policies for courseRules and policies for course
Rules and policies for course
Codewizacademy
 
Content Creation
Content CreationContent Creation
Content Creation
Codewizacademy
 
Taxonomies
TaxonomiesTaxonomies
Taxonomies
Codewizacademy
 
Course outline
Course outlineCourse outline
Course outline
Codewizacademy
 
Course development
Course developmentCourse development
Course development
Codewizacademy
 
Looping
LoopingLooping
Displaying message on web page in Javascript
Displaying message on web page in JavascriptDisplaying message on web page in Javascript
Displaying message on web page in Javascript
Codewizacademy
 
Storage in programming
Storage in programmingStorage in programming
Storage in programming
Codewizacademy
 
Programming fundamentals through javascript
Programming fundamentals through javascriptProgramming fundamentals through javascript
Programming fundamentals through javascript
Codewizacademy
 
Cyber Security
Cyber SecurityCyber Security
Cyber Security
Codewizacademy
 
Website Security
Website SecurityWebsite Security
Website Security
Codewizacademy
 
Website Cookies
Website CookiesWebsite Cookies
Website Cookies
Codewizacademy
 
GDPR- General Data Protection Regulation
GDPR- General Data Protection RegulationGDPR- General Data Protection Regulation
GDPR- General Data Protection Regulation
Codewizacademy
 
CSS- Cascading Style Sheet
CSS- Cascading Style SheetCSS- Cascading Style Sheet
CSS- Cascading Style Sheet
Codewizacademy
 
HTML- Hyper Text Markup Language
HTML- Hyper Text Markup LanguageHTML- Hyper Text Markup Language
HTML- Hyper Text Markup Language
Codewizacademy
 
Languages of Internet
Languages of InternetLanguages of Internet
Languages of Internet
Codewizacademy
 
WordPress.com dashboard
WordPress.com dashboardWordPress.com dashboard
WordPress.com dashboard
Codewizacademy
 
What is a Database
What is a DatabaseWhat is a Database
What is a Database
Codewizacademy
 
Protocols
ProtocolsProtocols
Protocols
Codewizacademy
 
TLD, Sub Domain
TLD, Sub DomainTLD, Sub Domain
TLD, Sub Domain
Codewizacademy
 

More from Codewizacademy (20)

Rules and policies for course
Rules and policies for courseRules and policies for course
Rules and policies for course
 
Content Creation
Content CreationContent Creation
Content Creation
 
Taxonomies
TaxonomiesTaxonomies
Taxonomies
 
Course outline
Course outlineCourse outline
Course outline
 
Course development
Course developmentCourse development
Course development
 
Looping
LoopingLooping
Looping
 
Displaying message on web page in Javascript
Displaying message on web page in JavascriptDisplaying message on web page in Javascript
Displaying message on web page in Javascript
 
Storage in programming
Storage in programmingStorage in programming
Storage in programming
 
Programming fundamentals through javascript
Programming fundamentals through javascriptProgramming fundamentals through javascript
Programming fundamentals through javascript
 
Cyber Security
Cyber SecurityCyber Security
Cyber Security
 
Website Security
Website SecurityWebsite Security
Website Security
 
Website Cookies
Website CookiesWebsite Cookies
Website Cookies
 
GDPR- General Data Protection Regulation
GDPR- General Data Protection RegulationGDPR- General Data Protection Regulation
GDPR- General Data Protection Regulation
 
CSS- Cascading Style Sheet
CSS- Cascading Style SheetCSS- Cascading Style Sheet
CSS- Cascading Style Sheet
 
HTML- Hyper Text Markup Language
HTML- Hyper Text Markup LanguageHTML- Hyper Text Markup Language
HTML- Hyper Text Markup Language
 
Languages of Internet
Languages of InternetLanguages of Internet
Languages of Internet
 
WordPress.com dashboard
WordPress.com dashboardWordPress.com dashboard
WordPress.com dashboard
 
What is a Database
What is a DatabaseWhat is a Database
What is a Database
 
Protocols
ProtocolsProtocols
Protocols
 
TLD, Sub Domain
TLD, Sub DomainTLD, Sub Domain
TLD, Sub Domain
 

Recently uploaded

Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 

Recently uploaded (20)

Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 

Conditional Constructs in Javascript

  • 2. Conditional Constructs • Different situations  Different solutions • Complex problem  Multiple possible solutions
  • 3. Problem with problems • Finding the best fit solution for each problem • Check different permutations and combinations
  • 4. IF • If a particular condition is true -> perform a specific action • Eg: – if (fruit == "apple") fruit_color = "Red";
  • 5. If-else • If a particular condition is true -> perform a specific action otherwise perform next action • Eg: – if(fruit == "apple") fruit_color = "Red"; else fruit_color = "Orange";
  • 6. If-elseif-else • If a particular condition is true -> perform a specific action • Otherwise check another condition, if true  perform that specific action • If none of above conditions is true  perform a specific action • Eg: if(fruit=="red_apple") fruit_color = "red"; elseif (fruit=="green_apple") fruit_color="green"; else fruit_color="orange";
  • 7. Using Operators • Check a specific condition • Analyse the problem • Find the best fit solution by checking multiple conditions simulaneously • Form relationship between different conditions to be analysed • Do arithmetic calculations
  • 10. Assignment Operator • = • Assign a value to a variable/constant/array • Value on the RHS is assigned to the storage on LHS • Eg: apples_bunch = 12; PI = 3.14; fruit = "apple";
  • 12. Relational Operator • Form relationship between two conditions • Compare the values of parameters
  • 13. Relational • To make a comparison while checking a condition, use relational operators. Operator Functionality Let’s consider x=5; y=10; > More Than x>y; False < Less Than x<y; True >= More Than or Equal To x>=y; False <= Less Than or Equal To x<=y; True == Equal to x==y; False != Not Equal to x!=y; True
  • 14. IF • If a particular condition is true -> perform a specific action • Eg: if (fruit == 'apple') fruit_color = 'Red';
  • 16. Logical Operator • Form relationship between multiple conditions • Also known as combinational operator – Combines different conditions together to find solution – AND- && – OR- || – NOT- !
  • 17. Logical • To combine two or more situations of conditional checking use, logical operators. Operator Functionality Condition Example && Both the sides of operator should result true x<y && y!=x True x<y && y==x False || Either side of operator should result true x<y || y!=x True x<y || y!=x True ! Inverse the result x!<y False
  • 18. If-else • Checking multiple conditions and parameter values. • Eg: if(fruit == "apple" && fruit_color == "red") fruit = "Red Apple"; else fruit = 'Orange';
  • 19. If-elseif-else • Checking multiple conditions • Eg: if(fruit=="apple" && fruit_color == "red") fruit = "Red Apple"; elseif (fruit=="apple" && fruit_color == "green") fruit = "Green Apple"; else fruit = "Orange";
  • 21. Arithmetic • To compute arithmetic value we use Arithmetic Operators. Operator Functionality Condition Example + Addition x+y 15 - Subtraction x-y -5 * Multiplication x*y 50 / Division x/y 0 % Remainder x%y 5
  • 22. Arithmetic Function • Eg: if(fruit=="apple" && fruit_color == "red") red_apple = red_apple+1; elseif (fruit=="apple" && fruit_color == "green") green_apple = green_apple+1; else orange = orange+1;
  • 24. Unary Operator • Works with a single operand • There is no RHS (Right hand side) to the condition • Performing certain operations with a shorthand
  • 25. Unary Operators • Used with single operand Operator Functionality Condition Example + Positive Value X= +15 - Negative Value X= -5 ++ Increment X++ or ++X -- Decrement X-- or --X ! NOT !x
  • 26. Conditional Constructs and Operators used in Javascript https://www.codewizacademy.com https://www.facebook.com/codewizacademy/ https://www.instagram.com/codewizacademy/