javascript Control flow statements

javascript Control flow statements

Hi everyone, Let's learn javascript Control flow statements

  1. Conditional Statement :

    - If statements

    - If else statements

    - nasted if statements

  • If statement :

    - if statement is to make decision and execute statements conditionally.

    Syntax :

if (condition) {

       'statement to be executed if condition true.'

    }

Here a javascript expression is evaluted. if the resulting value is true, the given statement are excuted. if the expression is false, then no statement would be not executed. most of the times, you will use comparision operators while making decisions.

Example :

let a = 30
let b = 40

if (a < b) {
    console.log(`a is less than b `); // a is less than b
}
  • if else statement :

    - use the statement to execute a statement if a logical condition is true. use the optional else clause to execute a statement if the condition is false and if statement as below :

    Syntax :

      if (condition) {
          statement_1;
      }else{
          statement_2;
      }
    

    Here the condition can be any expression that evaluates to true or false. if condition evaluates to true, statements_1 is executed; otherwise, statement_2 is executed.

    You may also compound the statement using else if to have multiple conditions tasted in sequence, as follows;

      if (condition_1) {
          statement_1;
      }else if(ondition_2){
          statement_2;
      }
      }else if(ondition_n){
          statement_n;
      }else{
          statement_last;
      }
    

    in the case of multiple conditions only the first logical condition which evaluated to true will be executed. to execute multiple statements, group them within a block statement ({......}). in general, its is good practice to always use block statements, especially when nesting if statements.

  • Nasted if statements :

    - nesting if/else statements helps to organize and isolate conditions in order to avoid testing the multiple the same condition twice or to minimize the number of times various tests need to be performed.

    - by using if statement with both comparison and logical operator, we can set up code that will be run if a specific combination of conditions is met.

    Example :

let a = 10;
let b = 10;
let c = 1;
let answer;

if (a == b){
    if (a == c){
        answer = 'all are equal';
}else{
    answer = 'a and b are equal';
}else{
    if (a == c) {
    answer = 'a and c are equal';
}else{
    f (b == c){
    answer = 'b and c are equal';
    }else{
        answer = 'all are diffrent';
      }
    } 
}
consloe.log(answer);
  1. Looping Statment :

    - While statement

    - do....while statemen

    - for statement

  • While statement :

    a while statement execute its statements as long as a specified condition evaluates to true. A while statement looks as below :

      while (condition) {
          code to be excuted
      }
    

    if the condition becomes false, statement within the loop stops executing and control passes to the statement following the loop.

    The condition test occurs before statement in the loop is executed.

    If the condition return true, statement is executed and the codition is tested again.

    example :

      while (i <= 5)
          {
              console.log(`the number is, ${i}`)
              i++
          }
    
    • do...while statement :

      - The do...while statement is variant of the while loop. this loop will always execute a block of code ONCE, and then it will repeat the loop as long as the specified condition true . this loop will always be executed at least once, even if the condition is false, because the is executed before the condition is tested.

        let i = 0
        do 
         {
                console.log(`the number is, ${i}`)
                i++
         } 
         while (i <= 5)
      
      • For statement :

        - Most frequently used loop in javascript is the for loop. the for statement will execute a block of code specified number of times.

        Syntax:

          for (initilization; condition; increment)
          {
              code to be execute
          }
        

        - it consist of three parts separated by semicoloumns,

        initialize ( i = 5); condition ( i < 5); increment ( i++ ).

  1. Switch statement :

    - The switch statement is used to execute one code from multiple expressions. it is just like else if statement, but its conveniet than if....else..if because it can be used with numbers, character etc.

    - The objective of a switch statement is to give an expression to evaluate and several different statement to execute based on the value of the expression.

    - The intrepreter checks each case against the value of the expression untill a Match is found. If nothing matches, default condition will be used.

    Syntax :

     switch (expression){
     case value1;
         code to be executed;
         break;
     case value2;
         code to be executed;
         break;
     default:
         code to be executed if above values are not matched;
     }
    

    - The break statements indicates the end of a particular case.

.

.

.

Thanks for reading !

Did you find this article valuable?

Support Danish Rayeen by becoming a sponsor. Any amount is appreciated!