DEV Community

Sowntharya
Sowntharya

Posted on

Control Statement in JavaScript

Control statement used to control the flow of execution of the program based on certain condition.
They allow program to make decision , repeat action or jump to different part of the code.

Types of statements

  1. Conditional statement : used to choose execution path based on a condition. conditional/selection/decision making statement.
  • if statement : If the condition is true statement will be executed. one condition - yes or no
let age = 20;
if(age >= 18){
console.log("eligible");  //eligible
}
Enter fullscreen mode Exit fullscreen mode
  • if else statement : If the condition is true statement will be executed otherwise else part will be execute.
let age =16;
if(age>=18){
console.log("eligible");
}
else{
console.log("not eligible"); //not eligible
}
Enter fullscreen mode Exit fullscreen mode
  • else if statement : If the condition is true statement will be execute if its false else if will execute if none is true then else part will execute.
let mark =75;
if(mark>=90){
console.log("A grade");
}
else if (mark>=70){
console.log("B grade");// B grade
} 
else{
console.log("C grade"); 
Enter fullscreen mode Exit fullscreen mode
  • Nested if statement : If statement inside another if statement. Both the condition should be true.
let age = 22;
let id = true;
if(age>=18){
if(id==true){
console.log("allowed");// allowed
}
}
Enter fullscreen mode Exit fullscreen mode
  • switch statement : Multiway branch statement. select a case based on a value.
let day = 2;
switch(day){
case 1 :
console.log("Monday");
break;
case 2 :
console.log("Tuesday");  //Tuesday
break;
case 3 :
console.log("Wednesday");
break;
Enter fullscreen mode Exit fullscreen mode

2.Looping Statement : Used to repeat a block of code multiple times, until a condition is true.

  • while loop : used to repeat/iterate a part of program repeatedly until the specified condition is true. If condition become false loop automatically stop. (no. of iteration is not fixed -we use)
let i=1;
while(i<=5){
console.log(i);  // 1 2 3 4 5
i++; 
}
Enter fullscreen mode Exit fullscreen mode
  • do-while loop / exit control loop : used to check the condition later, want to execute the block atleast once.
let i = 1;
do {
console.log(i); //1
i++;
}
while(i<=3);
Enter fullscreen mode Exit fullscreen mode
  • for loop : use to perform a task in single line. initialization, condition, increament/decreament.
for(let i=1; i<=3: i++){
console.log(i);
}

output:
1
2
3
Enter fullscreen mode Exit fullscreen mode

3.Jump statement : used to break out of loops and switch statement.

  • break statement : used to break the current flow of the program.
for(let i=1; i<=5; i++){
if(i===3){
break;
}
console.log(i);  //1 2
}
Enter fullscreen mode Exit fullscreen mode
  • continue : It doesn't break the loop, its skip the specific part of the loop and jump the next iteration of the loop.
for(let i=1; i<=5; i++){
if(i===3){
continue;
}
console.log(i);  //1 2 3 4 5
}
Enter fullscreen mode Exit fullscreen mode
  • return statement : ends a function and can send a value back to the place where the function was called.
function add(a, b){
return a+b;
}
let result = add(10,20);
console.log(result);  // 30
Enter fullscreen mode Exit fullscreen mode

Top comments (0)