doWhiles - When to use a do while? When to use a while? 4...

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

View Full Document Right Arrow Icon
ANNOUNCEMENTS No posting of code in the forum Check class announcements daily 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 INCREMENT/DECREMENT OPERATORS ++ increases value by one x++ x = x + 1 -- decreases value by one x-- x = x – 1 Pre/post version ++x vs. x++ --x vs. x--
Background image of page 2
3 DO WHILE STATEMENT do while statement – Allows repetition of a set of statements Can be seen as “do as long as” Basic Form do { statement // executed as long as expression is true } while (expression); Notice the semicolon after the expression in parenthesis Executes the statement at least once You don’t need the { } if you only need to execute one statement Example: DoWhileNumbers.html Example: DoWhile.html Any type of statements (including do whiles) in a do while
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 DocumentRight 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: When to use a do while? When to use a while? 4 EMPTY STATEMENT Empty statement: represented by a semicolon It does nothing Example: if (x === 100) ; else { // task } Notice we dont want to have code like the one above. We could rewrite it as follows if (x !== 100) // task If you add a semicolon to a while statement (not a do while) you may generate an infinite loop. Be careful 5 CODING EXAMPLE (PASSWORD READING) Lets write a program that reads a password and allows a maximum of two attempts We will start with just reading the password and looping until a valid password is provided IN-CLASS LAB (20 MINUTES) 6...
View Full Document

This note was uploaded on 01/12/2012 for the course CMSC 122 taught by Professor Staff during the Fall '08 term at Maryland.

Page1 / 6

doWhiles - When to use a do while? When to use a while? 4...

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