{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

JS+Loops+1 (1) - for loop • A for loop has three parts...

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

View Full Document Right Arrow Icon
CS 170:JS Loops Kristian Stout [email protected]
Background image of page 1

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

View Full Document Right 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 2
if versus while if (condition) } statements within { } get executed just once while (condition) {…} statements within { } get executed as long as  condition is true 
Background image of page 3

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

View Full Document Right Arrow Icon
do loop Syntax: do { some statements; increment; } while (condition); Statements within { } get executed at least once. The condition  decides whether the next iteration is to be performed or not.
Background image of page 4
Background image of page 5

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

View Full Document Right Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for loop • A for loop has three parts separated by semicolons. for (initialization; continuation; increment) { some statements; } Example for (i=0;i<5;i=i+1) {…} JS Sample Problem • Write an algorithm for a program that will satisfy the following requirements: ▫ Asks a user how many numbers they want to calculate ▫ Compute the sum of the numbers from 1 to the number entered in the step above ▫ Prints out the sum • Create a flowchart for the above algorithm • Implement the above algorithm and flowchart using Javascript...
View Full Document

{[ snackBarMessage ]}