FOP: While Loops
While Loops: Loops are an extremely important programming concept that is useful for repeating certain lines of code over and over again. Today we will be focusing on the WHILE loop While something is true, keep repeating until that something is false. var count = 0; while( count < 5){ console.log(count); count = count + 1; }
Infinite Loops: Depending on how you structure your loops, you may run into the problem of your loop running forever which is a big problem. If your Boolean expression never becomes false then your code will run forever which may cause your browser or computer to crash. var count = 0; while( count < 5 ){ // Infinite Loop because count will remain zero every iteration console.log( count ); // Since count never changes the Boolean expression will remain true } //The code will just print out 0 forever If you are going to use a loop you MUST make sure that the Boolean expression controlling the loop can eventually become false. That’s up to you the programmer.
Debug Console Feature: If you run your program you can check the value of your variables at the end of the program by simply typing the name of the variable in your debug console.
Debug Console Feature: You can also have your code stop at specific lines while your program is running so that you can see if your program is behaving the way you intended. To do this just click on the number of the line in your code that you want to stop at. Now when you run your code it will stop at line 3 and for you to “Continue”, you just click or “Step over” to resume your code
While Loops and IF Statements: In a while Loop you can use similar Boolean expressions like you would in an IF- statement. Look at the example below: var die1 = -1; var die2 = -1; while( die1 < 3 && die2 < 3){ die1 = randomNumber(1,6); die2 = randomNumber(1,6); } While loops and IF-statements are fairly similar in how they are structured and the way each evaluates Boolean expressions. While loops just repeat over and over until the condition becomes false.
Math Operators: ++ Operator -- Operator var count = 0; count++; //Now count is 1 -- Operator var count = 10; count--; // Now count is 9
Math Operators: += Operator -= Operator var count = 0; count += 3; // Now count is 3; count+=3;` -= Operator var count = 10; count -= 4; //Now count is 6 Count-=10;
IF statements within Loops: You can combine multiple programming concepts together as you are programming. You can place a loop inside a loop You can place a conditional( IF ) statement within a loop You can place a loop inside a conditional ( IF ) statement You can place a conditional( IF ) statement inside a conditional( IF ) statement. Your assignment today is to complete both Stage 11 and Stage 12 In stage 11 it is encouraged you try to finish puzzle 12 and puzzle 13 but it isn’t mandatory In stage 12 is where you will be given less guidance and you will learn a lesson about probability You will have an IF-statement within some of your loops so make sure you understand how to properly place these conditional statements.