So as you already know I am JS lover(JavaScript) and yesterday my js planned for today that we have to go to park early morning and she used switch statement to express her feelings.If you didn't know the context, you can read yesterday rose day story here is the link for you- devkeycode.hashnode.dev/rose-day-feb-7th-co..
So,today js wake up at 7:30am and as per the plan,we have to go to the park. As today is Feb-8th -Propose Day and I was assuming that my lover js will propose me at the park and have made special plan for me. I was wrong at first statement,but later statement was correct and you will get to know in few lines.
When we reached the park, my lover js simply sit on the bench and told me to jogs 10 round around the park and at each round I have to say this to her - "Next time , I will bring a rose for my rosey".I thought how will I know how many rounds(loops) I have completed and for that my lover js stated "don't worry , I have several things to count the loops". And I also get started as yesterday i didn't boought a rose for her, so I agreed.
So, she bring a "for loop" out of her bag that she brought with her, and also set a "count to 0" and on each iteration ,she increased the count by 1 when I loudly said what she want me to say to her after I completed a round of the park.
for (let i = 0; i < 10; i++) {
//say i will bring a rose for my rosey.
console.log("Next time , I will bring a rose for my rosey - JavaScript");
}
Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript. Next time , I will bring a rose for my rosey - JavaScript.
After 10 round jogging around the park,I aksed her are you happy now,She just keep silent and she didn't stop here and demanded another thing , this time she brought another "while loop" from her bag.I don't know how many things she holds in her bag for looping only. This time she make a variable count and also set it to 0 and on each iteration ,she increased the count by 1 when I loudly said what she want me to say to her after I completed a round of the park.
let count = 0;
while (count < 10) {
console.log("Next time, I will definitely bring a rose!");
count++;
}
Next time, I will definitely bring a rose! Next time, I will definitely bring a rose! Next time, I will definitely bring a rose! Next time, I will definitely bring a rose! Next time, I will definitely bring a rose! Next time, I will definitely bring a rose! Next time, I will definitely bring a rose! Next time, I will definitely bring a rose! Next time, I will definitely bring a rose! Next time, I will definitely bring a rose!
So , I found out that my lover js has 2 ways of looping -one is "for loop" which takes 3 arguments , first one is the initialiser ,second one is the condition which will check on each iteration whether to continue or stop the iteration and third one is updation statement which will update the variable ,so it doesn't become an infinite loop. At least She counted and incresed the count by 1 on each iteration,if she doesn't count , then where I will be I can't be dream of(infinite loop situtation). Another one is "while loop" ,where before starting its definition, we declare a count variable(globally) and set it to 0 and pass the condition as the argument till when we want the while loop to run and then inside the while loop body,we increment the count by 1 so conditon can become false after certain time , see how lucky I am ,Js take care of me, she incrments the count by 1 and stops when count reaches 10. But sometime she can be mad at you and can cause an infinite loop if not properly managed,so be carefull while giving instruction. I was partially write about 2 ways of looping, and you will come to know as my js lover has 3rd way of looping also and that is using "do-while" loop and now one more 10 rounds of park I have to do.
let count = 0;
do {
console.log("JavaScript, I will listen to you.");
count++;
} while (count < 10);
JavaScript, I will listen to you. JavaScript, I will listen to you. JavaScript, I will listen to you. JavaScript, I will listen to you. JavaScript, I will listen to you. JavaScript, I will listen to you. JavaScript, I will listen to you. JavaScript, I will listen to you. JavaScript, I will listen to you. JavaScript, I will listen to you.
Even after all this rounds of park, she was not looking happy. Now I told her that I will only say whatever she will say to me, but only for even counts since now i was heavely breathing, so I used continue statement here,continue will skip the rest part of the code in the current iteration and will go to next iteration.
for (let i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
console.log("Today,I will listen to you.");
}
Today,I will listen to you. Today,I will listen to you. Today,I will listen to you. Today,I will listen to you. Today,I will listen to you.
Now again she was looking at me, she thought oh! he is acting smart now I am saying this by seeing her looks only.I was too tired after so many rounds so I told her I will break the loop at first even count only, she said ok,but she was very clever as she already knew that 0%2 will give 0 , (% is an operator in her js language that she called by name "modulus operator" and used it to calculate remainder among 2 numbers.), so she started this time count with 1.
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
break;
}
console.log("You are the one!");
}
You are the one!
And after this,she again looking at me but this time I said to her I will not do any more round of the park.But Js being Js, conviced me finally to do one more round but I was also clever so I said this time I will set initial count variable and you set condition to count less than 10, she said ok. In my mind, i was thinking I will make count to 12, then how will loop runs,loop condition will be false initially only,so I will not have to run at the park and say whatever she wants me to say. So she started this time with this , you only look at the result , I can't even say it because I have to do one more round of the park and thats why this article got late.
let i = 12;
do {
console.log("ok! JS you are the boss!");
} while (i <= 10);
ok! JS you are the boss!
So, JS used here dowhile loop which is exit-controlled loop , by this I means it will run at least one ie, the whatever inside the body of dowhile loop will be executed ,no matter condition is true or false. It's not like for loop and while loop, which are entry-controlled loops, which will run only if condition is true.
And this is how Propose Day spent with JS-JavaScript. Tomorrow is the chocolate day,Let's see what waiting!!Stay Tuned.