M/F 2:30-3:20 |
CS 1130: Transition to OO Programming Spring 2016 |
Main
About: Overview Announcements Staff Consultants Calendar Materials: Texts DrJava Terminology Lectures: In-Class Web-Based VideoNote Assessment: Grading Assignments Labs Resources: CMS Piazza (link) Piazza (about) Java API Style Guide Academic Integrity |
Conditional Statements and LoopsIntroductionEvery procedural programming language has conditional statements (if-statements) and iterative statements (loops). You should be familiar with the concepts —unless your only previous programming language was a purely functional language. Therefore, our purpose here is to summarize the Java versions of these statements and point you to the places in the text and the CD ProgramLive for longer discussions of these statements. The BlockThe block has the form
Its purpose is to aggregate (collect together) the sequence of statements and declarations into a single statement. It used below in writing conditional and iterative statements. For more information, see Gries/Gries, p. 69, and ProgramLive, p. 1-4 if-statementThe if-statement has the form
The <condition> is a boolean expression. We generally make the <statement> be a block. The following example shows the conventions used in this course for indenting and the placement of the opening brace of the <statement>.
To execute the if-statement, evaluation the <condition> and, if it is true, execute the <statement>. For more information, see Gries/Gries, p. 69, and ProgramLive, p. 1-4. if-else statementThe if-else statement has the form
The <condition> is a boolean expression. We generally make <statement1> and <statement2> be blocks. The following example shows the conventions used in this course for indenting and the placement of the opening braces of the statements.
or
To execute the if-else statement, evaluation the <condition>; if it is true, execute <statement1>; if it is false, execute <statement2>. For more information, see Gries/Gries, p. 69 and ProgramLive, p. 1-4 while-loopThe while-loop has the form
where the <condition> is a boolean expression and the <repetend> is any statement. (Repetend means the thing to be repeated.) We generally make the <repetend> be a block. The following example shows the conventions used in this course for indenting and the placement of the opening brace of the <repetend>.
To execute the while-loop, do the following:
The first execution of the <repetend> is called iteration 0; the second, iteration 1; and so on. For more information, see Gries/Gries, p. 233, and ProgramLive, p. 7-1. for-loopWe don't describe everything about the for-loop but just its use with a single "control variable" that is initialized at the beginning and incremented every iteration of the for-loop. (An iteration is one execution of the repetend —see below.) The for-loop has the form
where the <initialization> is an assignment or an initializing declaration, the <condition> is a boolean expression, the <increment> is an assignment, and the <repetend> is any statement. (Repetend means the thing to be repeated.) We generally make the <repetend> be a block. The following example shows the conventions used in this course
for indenting and the placement of the opening brace of the <repetend>.
Note that a range
Execution of the for-loop is equivalent to execution of the following translation into a while-loop, with one exception. If the initialization is an initializing declaration, the scope of the control variable is the for-loop itself, and the control variable cannot be referenced in statements after the loop. But in the while-loop version, the scope includes the statements following the loop.
For more information, see Gries/Gries, p.78 (a quick introduction) and p. 253, as well as ProgramLive, p. 7-4. |