DEV Community

Cover image for All about Control-Flow in Modern Js (2)
AbdELrahman Mohamed
AbdELrahman Mohamed

Posted on

All about Control-Flow in Modern Js (2)

Hoo-AH , In this article we will go throw Control-flow in js and learn how to use , As i mentioned before in ES6 vars&dataTypes all codes will be in one Js file with examples and results, So,enough videos just take 5 mins to read it.

File will contain topics like:

  • For loops
  • Looping throw data like arrays
  • Wile loop
  • Do While loop
  • If statements
  • If else & else if statement
                Code Written by Abd-Elrahman
// for loops

for (let i = 0; i < 5; i++) // here we gonna print the numbers from 0 and stop at 4 because the condition is i is lessthan 5
{
    console.log(i); // output  0 1 2 3 4  

}

//looping throw data like array
const friends = ['Ahmed','hameed','hassan','ragab'];

for(let name = 0; name < friends.length; name++)
{
    console.log(friends[name]); // output 'Ahmed','hameed','hassan','ragab'
}

//======================================================================================

// while loop

let i = 0; //here we define the start point

while (i < 5) //here we define the condition
{
    console.log('in loop: ',i); // ouput is 
    i++; //here we iterating throw the the condition till it reach 4 

} 
let name = 0 ;
while (name < friends.length) 
{
    console.log(friends[name]); // output 'Ahmed','hameed','hassan','ragab'
    name++;
}

//======================================================================================


// do while loop  it's guarantee that the condition gonna excute one time at least even the condition isn't true!

let x = 5;
do {
    console.log('val of i: ',x); // output is val of i:  5
    x++
} while (x < 5);



let y = 0;
do {
    console.log('val of i: ',y); // output is 0 1 2 3 4 
    y++
} while (y < 5);

//======================================================================================

// if statements

const age = 25 ;

if (age > 20) {
    console.log('your are over 20 years'); // your are over 20 years

}

//======================================================================================

// if else & else if statement
const password = 'password';

if(password.length >= 12)
{
    console.log('that password mighty be strong');
}
else if (password.length >= 8 ) 
{
    console.log('password is long enougha');
}else{
    console.log(' password is not long enough');
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)