lec11

lec11 - CSE 8A: Lecture 11 More on iteration and branching...

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

View Full Document Right Arrow Icon
Page 1 of 26 CSE 8A, UCSD LEC 11 CSE 8A: Lecture 11 More on iteration and branching for loops Switches break and continue statements (Reading: Savitch, Ch. 3 )
Background image of page 1

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

View Full DocumentRight Arrow Icon
Page 2 of 26 CSE 8A, UCSD LEC 11 Iterative and conditional control control constructs You already know about if and if-else conditional control constructs in Java these are very useful in writing interesting programs: the machine can make choices at runtime You already know about while and do-while -loops in Java these are also very useful in programming: in fact, with while-loops, you can perform any computation at all Now we will look at for-loops (another iterative control construct) and switches (another conditional control construct) in Java These don’t add any new computational power, but they do add convenience in some circumstances
Background image of page 2
Page 3 of 26 CSE 8A, UCSD LEC 11 3 kinds of iterative control construct in Java Java has while loops: int i=0, sum=0; while(i++<100) { sum += scnr.nextInt(); } And do-while loops: int i=0, sum=0; do { sum += scnr.nextInt(); } while(i++<100); And also for loops: int sum=0; for(int i=0; i<100; i++) { sum += scnr.nextInt(); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
Page 4 of 26 CSE 8A, UCSD LEC 11 The for statement A for statement has the form for ( <init_expression> ; <test_expression> ; <iter_expression> ) <statement> When a for statement is executed. .. <init_expression> is executed once. The boolean expression <test_expression> is evaluated. If it is “true”, the loop body <statement> is executed; and then the <iter_expression> is evaluated; and then the for statement is executed again, starting with <test_expression> If it is “false”, execution passes to the statement after the for
Background image of page 4
Page 5 of 26 CSE 8A, UCSD LEC 11 for-loop flowchart init_expression test_expression body_statement iter_expression previous statement True next statement False
Background image of page 5

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

View Full DocumentRight Arrow Icon
Page 6 of 26 CSE 8A, UCSD LEC 11 Declarations in for-loops Note that the initialization expression in a for-loop can be a declaration-with- initialization This can be very convenient, but watch out for the scope rule: in Java, a variable declared in a for-loop initialization expression is visible within the for-loop only // print integers from 0 up to (but not including) howmany void printem(int howmany) { for(int i=0; i<howmany; i++) { System.out.println(i); // okay, in scope of i } System.out.println( "total: " + i); // error! i out of scope }
Background image of page 6
Page 7 of 26 CSE 8A, UCSD LEC 11 Equivalence of iterative control constructs As you know, anything you can do with a do-while loop, you can also do with a while- loop, and vice-versa (you may have to add, remove, or change some statements) Also: anything you can do with a for-loop, you can also do with a while-loop, and vice- versa (by adding, removing and changing some statements) while( <exp> ) <body> equivalent to for(; <exp> ;) <body> for( <init> ; <exp> ; <incr> ) <body> equivalent to { <init> ; while( <exp> ) { <body>
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

lec11 - CSE 8A: Lecture 11 More on iteration and branching...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online