Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?

Similar presentations


Presentation on theme: "Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?"— Presentation transcript:

1 Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?

2 Loops If you need to repeat a sequence of commands a certain number of times, you should use loops.

3 For Loops A for loop repeats a command a certain number of times, i.e. runs the command for a certain set of inputs. for (statement 1; statement 2; statement 3) {     code block to be executed } Statement 1 is executed before the loop (the code block) starts. Statement 2 defines the condition for running the loop (the code block). Statement 3 is executed each time after the loop (the code block) has been executed.

4 For Loops, Example for (i = 0; i < 5; i++) { text += "The number is " + i + "<br>"; } From the example above, you can read: Statement 1 sets a variable before the loop starts (var i = 0). Statement 2 defines the condition for the loop to run (i must be less than 5). Statement 3 increases a value (i++) each time the code block in the loop has been executed.

5 While Loops A while loop repeats a command, while a certain criteria holds true. while (condition) { code block to be executed } Example: while (i < 10) { text += "The number is " + i; i++; }


Download ppt "Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?"

Similar presentations


Ads by Google