Javascript-condional and Decision Making (1)
Javascript-condional and Decision Making (1)
Making
If, else if, else Statement
Definition • if Statements in Program allows us to tell the
computer to perform alternative actions based
on a certain set of results.
• Verbally, we can imagine we are telling the
computer:
• "Hey if this case happens, perform some action"
• We can then expand the idea further with elif
and else statements, which allow us to tell the
computer:
• "Hey if this case happens, perform some action.
Else if another case happens, perform some
other action. Else-- none of the above cases
happened, perform this action"
Syntax
if (condition) {
statement
} else {
statement
}
Example 1 – if and else statement
• This example by default we make true to if statement
• We are getting error because the varTwo is local scope
Example 2 – if and else statement
• This example by checking the values
If Else example 3
• Checking the job
status
If…elif..else
• Let's get a fuller picture of how far if, elif, and else can take us!
• We write this out in a nested structure. note for how the if,elif,and else
line up in the code. This can help you see what if is related to what elif
or else statements.
Syntax
if (condition) {
statement
} else if (condition) {
statement
} else {
statement
}
Example 1 – if ,else if and else statement
• In this example first it will check the if statement then go for else if
and then it go to else statement
Example for Logical And and Logical OR
• Checking two conditions
Example for Logical And and Logical OR
• In this example, if the person is
below 13 is boy
• In between 13 to 20 is teenager
• In between 20 to 30 is young
man
• More than 30 is a man
Scope
• We have the three different kind of Scope
• Lexical Scope (Static Scope)
• Global Scope - Defined outside of all code blocks
• Local Scope - Defined inside a code block
• If you can access the variable from any point of the code is Global
Scope
• If you can access the variable from only one block is Local Scope
Global Local Scope
• Inside age value is Different Local Scope
• Scope is 40
• Outside Age value is different Global Scope
• Scope is 50
Global and Local Variable
• In this example each statement execute the different block of age
Switch Case Statement
• The switch statement evaluates
an expression, matching the
expression's value to a case
clause, and executes statements
associated with that case, as
well as statements in cases that
follow the matching case.
Switch Case Statement
• By default it is entering in to the switch
statement
• And checking the condition if all the
above condition becomes false we will
be getting the default
IF ELSE vs Switch
BASIS FOR
IF-ELSE SWITCH
COMPARISON
Basic Which statement will be executed depend upon Which statement will be executed is
the output of the expression inside if statement. decided by user.
Expression if-else statement uses multiple statement for switch statement uses single expression
multiple choices. for multiple choices.
Testing if-else statement test for equality as well as for switch statement test only for equality.
logical expression.
Ternary Operator
• In the ternary we are making a single line for condition true or false
• If age is mre than 18 he can go out else he can’t simple if else we are
making in a line by using ‘?’
• Syntax: condition ? statement if True (like if): statement if false (like
else)
• Example:
Definition
• for-loop (or simply for loop) is a control
flow statement for specifying iteration, which
allows code to be executed repeatedly.
• Syntax: