JavaScript Part-6: JavaScript Control Structure

JavaScript Part-6: JavaScript Control Structure
Image

What is JavaScript Control Structure?

Control Structure in JavaScript is the control structure of programs or codes or some of the statements with which we control the program flow or determine what to do next to the program.

What are JavaScript Control Structures?

Below is the list of Control Structures:

Control Structure List

Image

What is the if statement in JavaScript Control Structure?

If statement in JavaScript is a condition or condition, any part of the program will be executed or not? It determines. Simply put, let’s say you go to the store to buy apple fruit, and decide not to come back if you don’t get apple fruit or buy any other fruit instead. This real life image is solved with the if statement in JavaScript. Notice the image below:

Image

How does If Statement work in JavaScript?

   > If Statement in JavaScript will only work when a Logical Condition is true,

  >  When the logical condition is false, skip or skip the code in the browser if Statement block.

  >  If Statement is also called One way decision.

Flow Chart of if statement in JavaScript

Notice the following flowchart to understand how If Statement works in JavaScript:

Image

What is the Syntax of if Statement?

Image

What is the if-else Statement in JavaScript?

The other statement in JavaScript is the opposite of if, i.e. if for some reason the if statement fails to meet the conditions or if the if statement is not true, then the statement used to determine which part of the program will execute its alternative, is called else. statement. In a simpler way, let’s say you go to the store to buy an apple or an orange fruit, and if you don’t get an apple fruit, you buy an orange fruit instead of an apple. This real-life image is solved in JavaScript with an if-else statement. Notice the image below:

Image

How does If-else Statement work in JavaScript?

    In JavaScript, when the logical condition of the if statement is false, the browser skips or avoids the code within the if statement block and will run the code within the else statement block.

Flow Chart of if-else statement in JavaScript

Notice the flowchart below to understand how the If-else Statement works in JavaScript:

Image

What is the Syntax of if-else Statement?

Image

What else if statement in JavaScript Control Structure?

In JavaScript, the else if statement is to check all the conditions in one or more of the conditions and give the opportunity to execute the program of the first condition that will be fulfilled. And the next condition is no longer checked. Simply put, let’s say you go to the store to buy an apple, an orange, a grape or a banana, In this case, your first choice is apples, and if you can’t find apples, bring oranges, if you can’t find oranges, bring grapes, and if you can’t find grapes, bring bananas. This real life image is solved in JavaScript with else if statement. Notice the image below:

Image

How else does Statement work in JavaScript?

  1.   if and not if else if no condition is true, then else if is used.
  2.   If one of the many logical else if is else if true, then the browser will execute the code in that else if block. And if any else if e is not true, by default else block will execute.
  3.  Another thing to know is that if many of the else ifs become more than if if true, then only the first else if will be executed.

Flow Chart of else if statement in JavaScript

The following flowchart easily explains how else if works:

Image

What is the Syntax of else if Statement?

Image

What is the switch statement in JavaScript Control Structure?

Image

The switch in JavaScript is the same as else if statement, the difference is that else if statement gradually checks all the conditions or conditions and the first condition that will be fulfilled, gives the opportunity to execute the program. And the next condition is no longer checked. And instead of checking all the conditions, the switch executes the condition by jumping. Does not check any other condition.

Flow Chart of switch statement in JavaScript

Image

What is the Syntax of switch Statement?

Image

www.facebook.com/amidur.rahman.7

Is there a difference between JavaScript else if and switch statement?

Yes there are several differences between JavaScript else if and switch statement:

Switch                                                                       VSelse if/elseif
The switch executes the match by jumping instead of checking all the conditions. Does not check any other condition.The else if / elseif statement gradually checks all the conditions and conditions and the first condition that will be fulfilled, gives the opportunity to execute the program. And the next condition is no longer checked.  
switch always see if (equality) is equalelse if always see if boolean is true
Switch faster than else ifelseif slower than switch
In the case of many choices, the switch statement is much more compact or simple.Many of the choices are relatively difficult elsewhere.
It is essential to use break statement in switch case  else if statement break statement should not be used.
The value of the variable used in the expression of the switch only supports integer data type.  The value of the variable used in the expression of elseif if supports both integer and character data type.
The switch statement cannot check the range of multiple values 
You can use only single expression in case of multiple choice in switch statement.You can use different expressions for each choice in the else if statement.

What is a for loop in JavaScript Control Structure?

Image

In JavaScript, a for loop is a task that is repeated or executed until a certain condition is met. In the for loop, the value initialization, condition and increment of the variable are done in the same line.

What is JavaScript for loop flowchart?

Image

What is for loop Syntax in JavaScript?

Image
Image

However, if you want, you can work with many values ​​in the first statement:

Image

If you want, you can also write the first statement like this:

Image

If you want, you can write the second statement without it. In that case you need to use break inside the loop:

Image

If you want, you can write the third statement without it.

Image

If you want, you can write any statement without it.

Image

What is For / In loop in JavaScript?

In JavaScript, the For / In loop is specifically used to perform loops on every element of the JavaScript array and object.

What is the for / in loop flowchart in JavaScript Control Structure?

Image

Example 1 with Object:

Image

Example 2 with array:

Image

What is the while loop in JavaScript Control Structure?

Image

In JavaScript, while loop is the same as for loop, meaning: a certain task is repeated or executed until a certain condition is met. The difference is that in the while loop the value initialization, condition and increment of the variable is done in different lines.

What is a while loop flowchart in JavaScript?

Image

What is the while loop syntax in JavaScript?

Syntax

Image

Example

Image

What is the do-while loop in JavaScript Control Structure?

In JavaScript, the do-while loop is the same as the while loop, meaning: a certain task is repeated or executed until a certain condition is met. In the while loop, the value initialization, condition and increment of the variable are done in different lines. The difference is that if the condition is not true in the while loop, the code block of while cannot be executed. And does not show any results. But in do-while, even if the condition is not true, it executes the code block 1 time, and can show the result.

What is the flowchart of do-While loop?

Image

What is do do loop Syntax in JavaScript?

Image

Notice the following example

Image

What is a break statement in JavaScript Control Structure?

In JavaScript, a break statement is used to immediately execute a program and exit the program if a certain condition is true. In JavaScript, break statement is used in loop or switch statement.

What is the flow chart of break statement in JavaScript?

Image

What is the Syntax of break statement in JavaScript?

How does a break statement work in JavaScript?

In JavaScript, break statement is usually used in loop or switch statement.

Image

Example of a break statement in JavaScript:

In the following example, when the value of i is 6, the program will immediately stop its execution. So here the result will be 1-5

Image

What is the continue statement in JavaScript Control Structure?

In JavaScript, if a certain condition is true, the continue statement is used to skip the immediate program execution and move on to the next program.

What is the flow chart of continue statement in JavaScript?

Image

How does the continue statement work in JavaScript?

In JavaScript, the continue statement is usually used in the loop. In other words, if a certain condition is true in the loop, then skip the immediate program execution and the continue statement works to go to the next program. Notice the image below

Image

Example of continue statement in JavaScript:

In the following example, when the value of i is 6, the program immediately skips its execution and moves on to the next program. So here comes the result 1 2 3 4 5 7 8 9 10

Image

Leave a Reply

Your email address will not be published. Required fields are marked *