DEV Community

Vignesh . M
Vignesh . M

Posted on

JAVASCRIPT CL -6

DAY -6

LOOPING - is used to repated the same action in code or output

  • so , for example will print number 5 in five time

so , in javascript

console.log("5");
console.log("5");
console.log("5");
console.log("5");
console.log("5");
Enter fullscreen mode Exit fullscreen mode
  • So the code will repate so this proble will solve to using loop

    In looping we are
    1. for loop
    2. while loop
    3.do-while loop

HOW TO LOOPING WILL WORKED

*IF THE THE STARTING STAGE THE COUNT = 0 ;
*CONDICITION IS 5 TIME PRINT THE NUMBER 1 IS A OUTPUT
*SO CHECK THE CONDITION IS LESSERTHEN 5 OR NOT IF CONDITION IS TRUE
*THE FIRST OUTPUT IS SHOW BUT WE NEED 5 TIME PRINT IN THE OUTPUT BUT CURRENTLY WE PRINT 1 TIME ONLY
*SO CURRENTLY THE COUNT VALUE IS 1 SO PREVIWES COUNT VALUE 0 TO 1 *SO ADD 1. COUNT = COUNT + 1 ; THEN ONLY WILL GET THE OUT PUT OF 5 *TIME PRINT THE NUMBER 1
*THIS LINE WORKING COUNT = PREVIEWS COUNT VALUE + 1 ;// COUNT = 0 + 1;

EXAMPLE CODE FOR WE NEEDED OUTPUT : FOR NORMAL HUMAN MIND SIDE


     let count = 0;
    if (count < 5){ // (1 < 5 )- TURE 
        console.log("1");
        count = count +1; // 0 +1 = 1
    }

    if (count < 5){ // CURRENT COUNT VALUE (1 < 5) - TRUE
        console.log("1");
        count = count +1; 1+1 = 2
    }

    if (count < 5){ CURRENT COUNT VALUE (2 < 5) - TRUE
        console.log("1");
        count = count +1; 2+ 1 =3
    }

    if (count < 5){ CURRENT COUNT VALUE (3 < 5) - TRUE
        console.log("1");
        count = count +1; 3 + 1 =4
    }

    if (count < 5){ CURRENT COUNT VALUE (4 < 5) - TRUE
        console.log("1");
        count = count +1; 4 + 1 = 5
    }

    if (count < 5){{ CURRENT COUNT VALUE (5 < 5) - FALSE SO CODE WILL ENDED 

    }

Enter fullscreen mode Exit fullscreen mode

SO WE NEED LOOPING TO SOLVE THE ISSUE .BECAUSE WILL SIMPLYFIED THE CODE

WHILE LOOP

let count = 0;
while(count<5){
    console.log("1");
    count = count + 1;
}
 console.log(count);// this code will show the count value 
        output :
        1 1 1 1 1

Enter fullscreen mode Exit fullscreen mode

// SO WE USEING THE LOOPING STATEMENT TO SIMPLYFIED RTHE CODE

THIS CODE ITERATION IS PREVIEWS HUMAN MIND SIDE CODE

TASK 1
NEED OUTPUT IS 1 2 3 4 5

let count = 1;
        while(count<=5){
            console.log(count);
            count++;

        } // OUTPUT IS 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

TASK 2
NEED OUTPUT IS 5 4 3 2 1

let count = 5
        while (count>=1) {
            console.log(count);
            count--;
        }

Enter fullscreen mode Exit fullscreen mode

TASK 3
NEED OUTPUT 0 2 4 6 8 10

 let count = 0
        while (count<=10) {
            console.log(count);
            count = count +2;
        }
Enter fullscreen mode Exit fullscreen mode

TASK 4
NEED OUTPUT 10 8 6 4 2 0

let count = 10
        while (count>=0) {
            console.log(count);
            count= count -2 ;
        }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)