for loop and while loop: more examples

//*for loop and while loop: more examples*// 16-Oct-2022 

//*for loop*//


//example 1
for(i=0; i<4; i++){
    console.log("good mornig")
}

//example 2
for(i=1; i<4; i++){
    console.log("hello")
}

//*for loop on array

//             0         1           2            3          4
let days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']

//example 1
for(i=0; i<days.length; i++){
    console.log("today is best day")
}

//example 2
for(i=1; i<days.length; i++){
    console.log("today is holiday")
}


//printing elements of array with for loop

//example 1
//             0         1           2            3          4
let days1 = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']

// index no. < length of array
// 4 < 5

for(i=0; i<days1.length; i++){
    console.log(days1[i])
}


//example 2
let months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun']

for(i=0; i<months.length; i++){
    console.log(months[i])
}


//*while loop*//


while(4 < 5){
    console.log("hi everyone")
    break
}


// 3 times "hello everyone"

let r1 = 1
while(r1 <= 3){
    console.log("hello everyone")
    r1 ++
}

// 5 times "Hello Virat" 

let r2 = 1
while(r2 <= 5){
    console.log("Hello Virat")
    r2 ++
}


//*use of 'break' in while loop

let r3 = 1
while(r3 <= 10){
    console.log(r3)
    if(r3 == 5){
        break
    }
    r3 ++
}


//*use of 'continue' in while loop

//example 1
let t7 = 11
while(t7 < 16){
    if(t7 == 12){
        t7 ++
        continue  // prints 0, 1, 3, 4
    }
    console.log(t7)
    t7 ++
}

//example 2
let t8 = 20
while(t8 < 31){
    if(t8 == 25){
        t8 ++
        continue
    }
    console.log(t8)
    t8 ++
}


//*use of 'continue' and 'break' in while loop

let t9 = 20
while(t9 < 31){
    if(t9 == 25){
        t9 ++
        continue
    }
    console.log(t9)
    if(t9 == 28){
        break
    }
    t9 ++
}

टिप्पण्या