For Loops the for Loop the for Statement the for Statement

For Loops the for Loop the for Statement the for Statement

The for Loop • Another loop statement, for, is best for when you can determine in advance how many times you need to execute the loop (counting loop). For Loops • The for statement includes the three parts needed for loops: initialize, test, and update. 15-110 Summer 2010 • All this information is conveniently placed at the beginning of the loop. Margaret Reid-Miller • All three loop statements (while, do, and for) are functionally equivalent. Summer 2010 15-110 (Reid-Miller) 2 The for statement The for statement • The form of the for statement is • The form of the for statement is for (<initialize>; <boolean_expression>; <update>) for (<initialize>; <boolean_expression>; <update>) <statement> <statement> • First, the initialize statement is executed. • It is equivalent to • If boolean_expression evaluates to true, then statement (body of loop) is executed, followed by the <initialize>; update statement. while (<boolean_expression>) { <statement> • The loop repeats until the boolean_expression <update>; executed after evaluates to false. } statement (body of loop) Summer 2010 15-110 (Reid-Miller) 3 Summer 2010 15-110 (Reid-Miller) 4 The for Flowchart A for Loop Example initialize n = 4 !int sum = 0;! sum i !for (int i = 1; i <= n; i++) {! 0 false ! !sum += i*i;! 1 boolean_expression i <= n ? !}! 1 !System.out.println(sum);! 2 update true i <= n ? Which variable is the loop 5 3 statement control variable? i <= n ? 14 (body of loop) 4 i <= n ? 30 5 i <= n ? Summer 2010 15-110 (Reid-Miller) 5 Summer 2010 15-110 (Reid-Miller) 6 Another for Loop Example Scope n = 11 sum i • The scope of a variable is the area within a program that !int sum = 0;! 0 can reference the variable. !for (int i = 1; i <= n; i+=3) {! 1 • The scope depends on where the variable is declared.! ! !sum += i;! i <= n ? 1 !}! !int sum = 0;! 4 !System.out.println(sum);! i <= n ? !for (int i = 1; i <= n; i++) {! Scope of 5 ! !sum += i*i;! 7 variable i i <= n ? !}! 12 !System.out.println(sum);! 10 i <= n ? 22 13 i <= n ? Summer 2010 15-110 (Reid-Miller) 7 Summer 2010 15-110 (Reid-Miller) 8 Scope Nested Loops !int sum = 0;! • A loop can have another loop inside of it. !int i;! • For each iteration of the outside loop, the inside loop !for (i = 1; i <= n; i++) {! runs completely. ! !sum += i*i;! Scope of • Often it is easiest to read from the inside out. !}! variable i • Example: !System.out.println(“Sum of first “ + (i-1)! ! + “ integers squared is “ + sum);! How many lines are printed? for (int i = 1; i <= 5; i++) {! for (int j = 1; j <= 3; j++) {! System.out.println(i + " " + j);! }! What happens if we } write println(i + j)? Summer 2010 15-110 (Reid-Miller) 9 Summer 2010 15-110 (Reid-Miller) 10 Palindromes Which Loops? • A palindrome is word, phrase, or sequence that reads • for loops are more natural when we know how many the same backwards as forwards. iterations we need (definite or counting loops). Examples: • Example: Bob by Weird Al Yankovic • Print "*" 10 times • Print the even numbers between 10 and the value of n (A parody of Bob Dylan's Subterranean Homesick Blues) • while and do loops are more natural when we want to keep looping until some outcome (indefinite or result controlled loops). http://www.youtube.com/watch?v=Nej4xJe4Tdg Examples: • Prompt the user until the user inputs the data in the correct How would you test whether a string is a palindrome? form. • Continue looping until we reached a million dollars. Summer 2010 15-110 (Reid-Miller) 11 Summer 2010 15-110 (Reid-Miller) 12 .

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