Learn JavaScript : if..else Statement (Part 6)

If-else statement image


"If..else if..else statements help to program a hard-wired Ai (Artificial Intelligence) with a barbaric learning ability" (Mark Anthony Graham)


In this section of the JavaScript tutorials, we will be looking at if..else statements in JavaScript. I recommend you learn our previous tutorial on JavaScript Operators (link). Remember each concept in JavaScript builds upon the other. With that being settled, let's look at the general understanding of an if..else statement

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

But there is another kind of the if..else statement I have not showed you yet. This one is the if..else if.. 
the else..if gives a coder more room to add extra conditions to assess a more complicated kind of situation.

Kindly spend a little time to understand the code written below.

------------------------------------------------------------------------

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

A summary of what we haved learned today on JavaScript if..else if..else

1. Condition Evaluation: The condition within the parentheses is evaluated, resulting in either true or false.

2. Execution of Blocks:
   - If the condition is true, the code inside the first block (if block) is executed.
   - If the condition is false, the code inside the else block (optional) is executed.

3. Program Flow:
   - After executing the appropriate block, the program continues to the statement following the if-else statement.
   - If there is no else block and the condition is false, the program proceeds without performing any specific actions.

In short, an if-else statement allows the program to execute different code blocks based on the evaluation of a condition, and it provides a way to control the flow of the program based on true or false conditions.

HaPpY Coding😀


Proceed to Part 7 : JavaScript Switch Statement


 

Comments

followers

Popular posts from this blog

The Beauty Of Using Flutter With Dart For Mobile Development

How to compress files in PHP

Choosing the Best Password Hashing Algorithm for PHP