if-else statement
The if and else statements form the basic control flow constructs in programming that support conditional execution of code on the basis of Boolean logic (true or false).
Analogy:
Agar kisi state me voting ho rahi hai, toh har party ka nishkarsh Boolean pe hi niklega:
True: Sarkar banegi.
False: Sarkar nahi banegi.
Coding me developer decision leta hai:
Kaunsi condition par true ya false evaluate hoga.
Logic aur rules developer define karta hai
Voting me janta decide karti hai:
Kis party ko vote dena hai.
Majority vote ke basis par sarkar banne ka faisla janta ka hota hai.
const bjpVictory = "Narendra Modi";
const leader = "Narendra Modi";
if (bjpVictory === leader) {
console.log(`BJP has won the election Congratulations to ${leader}`);
} else {
console.log("BJP has lost the election");
}
So, we can apply multiple conditions using if else within a same scope.
Now, let's understand this with the snippet code of the Delhi assembly election.
const totalSeat = 70
const majority = Math.ceil(totalSeat/2);
if(36 >= majority){
console.log("BJP has won the Delhi election");
}else if(35 < majority){
console.log("AAP has lost the Delhi election");
}else if(35 < majority){
console.log("Congress has lost the Delhi election");
}else{
console.log("other party won the Delhi election")
}
We can use else if conditions as many times as needed, and the condition that evaluates to true will execute. Once a condition is true, the program will stop checking the remaining conditions and exit from that point.
switch-case
A switch-case is like a shortcut for checking a variable's value and running specific code based on that value. Instead of writing multiple if-else conditions, you write one switch block with multiple case options.
In simple language, switch-case and if-else are like brothers.
If-else and switch-case analogies might seem similar, but their concepts are different.
Analogy:
Delhi me voting ke ek mahine pehle BJP house me modi ji ke sath meeting hoti hai aur yeh nishkarsh nikalta hai ki:
agar jyada really karenge toh election me jayda seat milegi.
if-else = Yahan modi ji jyada really karenge fayda hai toh lekin janta ka mood bhi accha hona chahiye, janta ko modi ji baatein achi lagegi tabhi unko majority milegi.
switch-case = Yahan aisa kuch nahi jitni jyada modi ji ko really utni jyada seat.
example - agar modi ji 15 really karenge toh 60+ seat,agar 10 karenge toh 45+ seat.function delhiWon(modijiReally){ switch(true){ case modijiReally >= 15: return "BJP will win 60+ seats"; case modijiReally >= 10: return "BJP will win 45+ seats"; case modijiReally >= 5: return "BJP will win 20+ seats"; default: return "Bjp will win 0 seats"; conole.log(delhiWon(15)) // BJP will win 60+ seats conole.log(delhiWon(11)) // BJP will win 50+ seats conole.log(delhiWon(7)) // BJP will win 20+ seats
Here, return is used, so break is not needed.
This is because if either return or break is used, the code will not move to the next condition.switch : The switch statement is used in programming to compare a single value or expression against multiple possible cases. It's an alternative to writing multiple if-else statements when checking one variable against many possible values or conditions.
case : A case is where you write different conditions for a switch expression or variable. If the value of the expression matches a particular case, the corresponding block of code for that case executes.
default : default is optional code if no one case match then default code block executes.