× back

Control Statement & Loop

If...Else

                  
var tomr = 'rain';
if(tomr == 'rain')
{
    console.log('Take a raincoat');
} else {
    console.log('No need to take a raincoat');
}
                  
              

Challenge

Write a program that works out whether if a given year is a leap year or not?

                       
                       
var year = 2004;
if(year % 4 === 0){
    if(year % 100 === 0){
        if(year % 400 === 0){
            console.log('Leap year');
        } else {
            console.log('Not a leap year');
        }
    }
    else {
        console.log('Leap year');
    }
} else {
    console.log('Not a leap year');
}
                       
                   

What is truthy and falsy values in JavaScript?

  • We are total 5 falsy values in JavaSciprt:
    1. 0
    2. ""
    3. undefined
    4. null
    5. NaN
                       
if(score = 0) { // we are comparing, we are assigning and 0 is false.
    console.log("OMG, we loss the game");
} else {
    console.log("OMG, we won the game");
}
                       
                   

Conditional (ternary) operator

                                      
variableName = (condition) ? value1:value2
                   
               
                   
var age = 16;
console.log((age >= 18) ? "You can vote" : "You can't vote");
                   
               

Switch Statement

1st without using switch find the area of circle, triangle and rectangle.

                   
var shape = 'circle'

if(area == "circle"){
        console.log("the area of the circle is : " + PI*r**2);
}else if(area == "triangle"){
        console.log("the area of the triangle is : " + (l*b)/2);
}else if(area == "rectangle"){
        console.log("the area of the rectangle is : " + (l*b));
}else{
        console.log("please enter valid shape");
}
                   
               

Using switch case

                   
var shape = 'circle' ;
var PI = 3.142, l=5, b=4, r=3;

switch(shape){
    case 'circle': 
    console.log("the area of the circle is : " + PI*r**2);
    break;

    case 'triangle':
    console.log("the area of the triangle is : " + (l*b)/2);
    break;

    case 'rectangle':
    console.log("the area of the rectangle is : " + (l*b));
    break;

    default:
    console.log("please enter valid shape");
}
                   
               

Break and continue

Loops

While loop statement

  • The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true.
                                  
var num = 20;
while(num <= 10) {
    console.log(num); // infinite loop
    num++;
    // num--;
}
                    
                

Do-While Loop statement

                    
var num = 5;
do {
    console.log(num); 
    num++;
} while (num <= 10)
                    
                

For Loop

                                  
for(var num = 0; num <= 10; num++)
{
    console.log(num);
}
                    
                

Challenge Time

JavaScript program to print table for a given number (8)?

                       
var tableof = 8
for(var i = 1; i <= 10; i++) {
    console.log(tableof + " * " + i + " = " + tableof * i);
}