Javascript, like most other programming languages, include ways to run blocks of code when something meets a condition. Here, I will describe the most common ways to do so.
<!--more-->
This post is part of my lecture series for Math I/O. There is no pre-reading for this lecture.
Don’t forget to include something in the loop that will eventually make the condition <codeclass="language-javascript">false</code>, otherwise you run into an infinite loop. (Which is a loop that never stops repeating itself; most likely crashing your browser)
If you want to run something a certain amount of times, use a “<codeclass="language-javascript">for"</code> loop. For loops can be broken down into three components: an initiating statement, a condition, and a statement that runs after every loop.
Here you have the initiating statement of <codeclass="language-javascript">var i = 0</code>. From there you check, is <codeclass="language-javascript">i</code> less than 5? Yes, so then we <codeclass="language-javascript">doSomething();</code>. After we <codeclass="language-javascript">doSomething();</code>, we add 1 to <codeclass="language-javascript">i</code>. Now <codeclass="language-javascript">i</code> equals 2. Is <codeclass="language-javascript">i</code> still less than 5? Yes, so we <codeclass="language-javascript">doSomething();</code>. Then we add 1 to <codeclass="language-javascript">i</code> again. This loop will keep happening until <codeclass="language-javascript">i</code> is not less than 5.
Having different control/conditional statements helps keep the state of any application you’re making. Did the user say not to notify them? Then don’t, otherwise (else) notify them. That’s all I have to say for this week. Hope this post helps you get a little more used to this big world called programming.
I recommend that you look at different perspectives of the same concepts. WebCheatSheet.com has a similar post to mine, check out what they had to say [here](http://webcheatsheet.com/javascript/if_else_switch.php).