Lecture15_JavaScript4

Lecture15_JavaScript4 - Why do we need loops? To do things...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Why do we need loops? To do things repeatedly : Examples Printing out ‘n’ even numbers If a wrong username/password is entered, keep asking for correct one. For the JS1 assignment: Allowing a customer to purchase more than one product.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Loops Loops are used to repeat a block of code (Iteration). Loops are used to repeat a block of code a specified number of times while a specified condition is true There are three types: while , do and for
Background image of page 2
Essentials for a loop Iteration variable i Starting point var i = 0 Continuation/Stopping condition i < 10 Increment (Change) size i = i + 1
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
while loop Syntax: while (condition) { some statements; increment; } The code between { } gets executed for as long as the condition is true and breaks out of the loop when condition is false.
Background image of page 4
The program loops while the condition is true. Flow of Control (Execution) Check condition -> if false quit loop
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

Lecture15_JavaScript4 - Why do we need loops? To do things...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online