PIAIC Conditional
PIAIC Conditional
Conditional Structures
Instructor: Mr Muhammad Sajid
Whatsapp: +92333 9211574
There come situations in real life when we need to make some decisions
and based on these decisions, we decide what we should do next.
1. Choosing an Outfit: Imagine you're getting ready for the day and
checking the weather. If it's raining, you'll wear a raincoat. Otherwise,
you might just wear a T-shirt. This decision is based on a condition - the
weather.
2
2. Cooking: When boiling an egg, you might check: If the water is
boiling, put the egg in. Else, wait for a bit more.
3. Traffic Lights: If the traffic light is red, stop. If it's green, go. If it's
yellow, slow down.
For example: We go to an ATM Machine and enter our ATM Card then
ATM Machine checks:
If the account balance is less than 0: Display "Insufficient funds".
Else: Allow the transaction.
3
4
if Statement
"if" checks a condition. If the condition is true, the code inside the "if"
block is executed.
Syntax:
if (condition) {
// code to execute if condition is true
}
Flowchart of if statement:
5
Example: Let's say If the user's age is more than 18, they're allowed
to vote.
Output:
You're allowed to vote!
6
if else Statement
"if-else" checks a condition, if it's true, it executes the "if" block. If not,
it executes the "else" block.
Syntax:
if (condition) {
// code if condition is true
} else {
// code if condition is false
}
7
Example: If a student's score is greater than 50, they pass. Else, they
fail.
Output:
You failed.
8
if-else-if Ladder Statement
The if else if statements are used when the user has to decide among
multiple options. The if statements are executed from the top down. As
soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the final else statement
will be executed. if-else-if ladder is similar to the switch statement.
Syntax:
if (condition1) {
// code for condition1
}
else if (condition2) {
// code for condition2
}
else {
// code if none of the conditions are true
}
9
Flowchart of if-else-if Ladder statement:
10
Example: Giving feedback based on student grades:
Output:
Try harder next time.
11
Nested if else Statement
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
12
Flowchart of Nested if else statement:
13
Example:Checking a number if it is positive or negative :
Output:
Positive number
Even number
14
Switch Statement
Syntax:
switch(expression) {
case value1:
// code for value1
break;
case value2:
// code for value2
break;
default:
// code if none of the values match
}
15
Flowchart of Switch statement:
16
Example:
Output:
Wednesday
17
Note: In a switch statement, if we do not use a break after each case,
execution will continue to the next case and execute the code for that
case as well. For example:
let num = 1
switch(num){
case 1:
console.log("Number is 1");
case 2:
console.log("Number is 2");
default:
console.log("Invalid Number");
}
This will print "Number is 1" and "Number is 2" & "Invalid Number"
since there is no break after case 1 and 2. The break statement prevents
falling through to the next case.So we must use the break in switch
statement for proper working otherwise it will misbehave .
18
default:
console.log('This is a consonant.');
}
In this example, if a letter is any vowel (a, e, i, o, u), it will print "This is
a vowel." since all these cases lead to the same code block.
Conditional Operator
Syntax:
19
Flowchart of Ternary Operator:
Output:false
20