Js | Decision Making: | if | if Else | Else if |

Decision Making: if, if…else, else if

Decision making or control flow in programmming is the way we coontrol the execution
of code based on certain conditions.this allows the program to make choicces and execute diff code paths.

Example 1: if s…


This content originally appeared on DEV Community and was authored by Ranjith Jr

Decision Making: if, if...else, else if


Decision making or control flow in programmming is the way we coontrol the execution
of code based on certain conditions.this allows the program to make choicces and execute diff code paths.

Example 1: if statement
syntax:
if (condition){

  //code block 

}

Example:

             f    f   t
let temp =  #24  #25  25;


if (temp>=25);
{

console.log("it is hot outside");

} 

Example 2: if...else statement

syntax:

if (condition){

  //code block 

}
else{

   //code block 

}
Example 1:
if (temp>=25);

{

console.log("it is hot outside");

}
else{

console.log("it is cold outside");

}
Example 2:
let isRaining= #true false;

if (isRaning);
{

console.log("take an umbrella");

}
else{

console.log(" you don't need an umbrella");

}
Example 3: else if statement
syntax:
if (condition){

  //code block 

}
else if(condition){

   //code block 

}
else{

   //code block 

}
Example:
let time=14;

if (time<12){

console.log("good morning"); 

}
else if(time<18){

 console.log("good afternoon");  

}
else{

 console.log("good evening");  

}
Example 4: Nested if statements
Variables
let age= 16;
let iswithparents=true;
let idproof=true;
Decision logic

if (age>=18);{

  if(idproof);
    console.log("you can visit the mall and can able to watch the movie"); 
  } else{
     console.log("you can visit te mall"); 
  }
}else{
     if(iswithparents);
        console.log("you can visit the play area"); 
  } else{
     console.log("you are not allowed in the play are"); 
  }

}
Example:
let day=1;

if(day===1){
  console.log("monday"); 
}
else if(day===2){
  console.log("thuesday"); 
}
else if(day===3){
  console.log("wenday"); 
}
else if(day===4){
  console.log("thuesday"); 
}
else if(day===5){
  console.log("friday"); 
}
else{
console.log("in valid"); 
}
Switch Statement
syntax:
switch(vale){
  case 1:
     //code block  
      break;
  case 2:    
    //code block  
       break;
  case 3:
      //code block  
       break;
  Default:
      //code block  
       break;
}
Exmple1:
let day=3;

switch(day){

  case 1:
      console.log("monday"); 
      break;
  case 2:
      console.log("thuesday"); 
      break;
  case 3:
      console.log("wensday"); 
      break;
  case 4:
      console.log("thresday"); 
      break;
  case 5:
      console.log("friday"); 
      break;
   default:
      console.log("invalid"); 
}

output:

wensday
Exmple2:
let choice='tea';

switch(choice){

   case 'coffee':
      console.log("you choose coffee monday"); 
      break;
   case 'tea':
      console.log(" you choose tea thuesday"); 
      break;
    case 'water':
      console.log(" you choose water thuesday"); 
      break;
    default:
      console.log("invalid"); 
}

output:

you choose tea 
Ternary Operator
short hand if else
let isadmin =true;

/*if(isadmin){
  console.log("am  admin");
}
else{
   console.log("am  user");
} */
                 true        .
let userrole = isadmin ? " am admin ":" am user ";
console.log(userrole);

let mark=60;
     var        cond      if       else
let result = mark>=30 ? " pass ":" fail ";
              //true
//let result = 25>=30 ? " pass ":" fail ";

console.log(result); 

output:

pass


This content originally appeared on DEV Community and was authored by Ranjith Jr


Print Share Comment Cite Upload Translate Updates
APA

Ranjith Jr | Sciencx (2024-08-31T09:15:31+00:00) Js | Decision Making: | if | if Else | Else if |. Retrieved from https://www.scien.cx/2024/08/31/js-decision-making-if-if-else-else-if/

MLA
" » Js | Decision Making: | if | if Else | Else if |." Ranjith Jr | Sciencx - Saturday August 31, 2024, https://www.scien.cx/2024/08/31/js-decision-making-if-if-else-else-if/
HARVARD
Ranjith Jr | Sciencx Saturday August 31, 2024 » Js | Decision Making: | if | if Else | Else if |., viewed ,<https://www.scien.cx/2024/08/31/js-decision-making-if-if-else-else-if/>
VANCOUVER
Ranjith Jr | Sciencx - » Js | Decision Making: | if | if Else | Else if |. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/31/js-decision-making-if-if-else-else-if/
CHICAGO
" » Js | Decision Making: | if | if Else | Else if |." Ranjith Jr | Sciencx - Accessed . https://www.scien.cx/2024/08/31/js-decision-making-if-if-else-else-if/
IEEE
" » Js | Decision Making: | if | if Else | Else if |." Ranjith Jr | Sciencx [Online]. Available: https://www.scien.cx/2024/08/31/js-decision-making-if-if-else-else-if/. [Accessed: ]
rf:citation
» Js | Decision Making: | if | if Else | Else if | | Ranjith Jr | Sciencx | https://www.scien.cx/2024/08/31/js-decision-making-if-if-else-else-if/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.