0% found this document useful (0 votes)
5 views

Javascript-condional and Decision Making (1)

The document explains conditional statements in programming, focusing on if, else if, and else statements, along with their syntax and examples. It also covers the concepts of scope, switch case statements, and the differences between if-else and switch statements. Additionally, it introduces loops, including for loops, while loops, and do/while statements, with examples illustrating their use.

Uploaded by

Vaishnavi Gajula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Javascript-condional and Decision Making (1)

The document explains conditional statements in programming, focusing on if, else if, and else statements, along with their syntax and examples. It also covers the concepts of scope, switch case statements, and the differences between if-else and switch statements. Additionally, it introduces loops, including for loops, while loops, and do/while statements, with examples illustrating their use.

Uploaded by

Vaishnavi Gajula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Conditional and Decision

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:

for( initiating, condition, action(++ or --){


Statement to perform
}
For Loop Example 1
• The loop will execute continuously until it react the condition
For Loop Example 2
• Here checking the length of the array, it will continue till the array
ends
•s
While Loop
It will keep on executing until it becomes false
While Loop Example
• Untill the condition becomes false it keep on executing the while loop
While loop example 2
• In this example we are getting the password if user enter the password
it will give the alert of your password is correct otherwise we will get
the prompt used to ask the password again and again
Do/While Statement
• The do/while statement creates a loop that executes a block of code
once, before checking if the condition is true, then it will repeat the
loop as long as the condition is true.
• The do/while statement is used when you want to run a loop at least
one time, no matter what.
Do.. While.. Loop example
• First it will print the ‘i’ value then only it check the condition
Do.. While.. Loop Example 2
• In this example first it give the alert then only it check the condition
Good Job
Next Topic: Object and Array Methods

You might also like