Introduction to Programming Instructor: Greg Shaw s1

Introduction to Programming Instructor: Greg Shaw s1

<p>Computer Programming I COP 2210 </p><p>The for Statement </p><p>I. Purpose</p><p>The Java for loop is used to repeat a statement or block of statements (known as the “loop body”) a fixed number of times. </p><p>The for is a “count-controlled loop” because the loop-control variable (i.e. the variable whose value determines whether another iteration is done) is a counter. The loop body is executed one time for each value from some starting value to some ending value, e.g. from 1 to 10 (or from 10 to 1).  The for loop should be used when you know in advance exactly how many times you want to repeat the loop body.</p><p>II. Syntax</p><p> for (initialization action ; boolean expression ; update action) { statements ; } where,</p><p> initialization action = assigning an initial or starting value to the loop-control variable (lcv).</p><p> boolean expression = anything that evaluates to true or false. In a for loop, this means testing the value of the lcv against some final or terminating value.</p><p> update action = incrementing (or decrementing) the lcv. That is, giving it the next value in the series. This statement is executed after each iteration (i.e., after each execution of the loop body). Material originally created by Greg Shaw, modified by Caryl Rahn. statements = any number of Java statements. Naturally, each one must be followed by a semi-colon.</p><p>Note: 1. the parentheses are required, and 2. there is no semi-colon after the closing parenthesis</p><p>Material originally created by Greg Shaw, modified by Caryl Rahn. III. Execution</p><p>1. The initialization action is executed.</p><p>2. The boolean expression is evaluated.</p><p> If it is true, then the loop body is executed and Java proceeds to step 3 (below).</p><p> Otherwise (if it is false), the loop is terminated. I.e., the loop body is skipped and control passes immediately to the statement following the closing brace.</p><p>3. The update action is executed.</p><p>4. The process is repeated from step 2.</p><p>Note: The loop body will be skipped entirely (i.e., executed zero times) if the boolean expression is false the first time it is tested. </p><p>Specific examples of for loops and how they can be used will be given in class.</p><p>Material originally created by Greg Shaw, modified by Caryl Rahn.</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us