Learn JavaScript : if..else Statement (Part 6)
"If..else if..else statements help to program a hard-wired Ai (Artificial Intelligence) with a barbaric learning ability" (Mark Anthony Graham)
What could first come to your mind is a condition. That's correct✅. When you use the if..else statement as a coder, it means you are defining a case and that if the condition of the case is satisfied then the block of code set to that condition would be executed by the machine.
Illustration of a condition
From, the diagram, the condition must return a value of either true or false. If it returns true, it means that the little girl must take the left turn and in case the condition returns false it means the little girl must take the right turn. So in a real-world coding situation, a Coder could program that if a user's input contains an even number, it prompts with an even number notice to inform the user that his or her input was an even number else tell the user that the number inputted is an odd number. Below is a general diagram to help you understand how this works.😀
The above diagram presents a concept in computer programming known as a flowchart. What is a flowchart?
A flowchart is a visual representation or diagram that illustrates the sequence of steps or actions in a process, algorithm, or program. It uses various symbols and arrows to depict the flow of control and decision points within the process.
Flowcharts are commonly used in software development, system analysis, and process engineering to document and communicate the logical flow of a program or a process. They provide a clear and structured representation of how inputs are processed, decisions are made, and outputs are geneTrated."
Let's get to our diagram. The program execution begins from start, thus it starts from the top. Then gets to an if condition that either returns true or false. If the condition is true then the if body (Block of code) is executed, else if the condition returns false then the else body (Block of code) is executed. After either of the condition is satisfied then the interpreter continues its journey down the script. But from the diagram above the execution process was exited.
A sample code of just if..else is being used.
------------------------------------------------------------------------
if (condition) {
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}
-------------------------------------------------------------------------
if..else if
------------------------------------------------------------------------
if (condition) {
// code to execute if the condition is true
} else if (condition 2){
// code to execute if the 2nd condition is true
} else if (condition 3) {
// code to execute if the 3rd condition is true
} else {
/ code to execute if the all the other conditions are false
}
-------------------------------------------------------------------------
In conclusion
Proceed to Part 7 : JavaScript Switch Statement
Comments
Post a Comment