80 likes | 529 Views
Repetition Statements. repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement repeats a block until boolean expression becomes false checks upfront before 1 st time do statement
E N D
Repetition Statements • repeat block of code until a condition is satisfied • also called loops • Java supports 3 kinds of loops: • while statement • repeats a block until boolean expression becomes false • checks upfront before 1st time • do statement • repeats a block until boolean expression becomes false • checks at the and, after 1st time • for statement • contains initialization statement • contains end statement (e.g. increment) • repeats a block until boolean expression becomes false
while Statement • syntax: • while(<boolean expression>) { //code} • the body can contain several statements • theexpression is evaluated • if it is false then the code doesn't get executed • while it is true, the code is executed • and control goes back to evaluation of the expression • once expression is false then control continues • code after the next statement after curly bracket is executed • if there is any one statement in the body, you can omit {} • but we recommend to use the curly bracket in any case • it's less errorprone
do Statement • syntax: • do { //code} while (<boolean expression>); • the body can contain several statements • the code is executed • then theexpression is evaluated • while it is true, control goes back to beginning of the code • once it is false, control continues • code after the next statement after curly bracket is executed • if there is any one statement in the body, you can omit {} • but we recommend to use the curly bracket in any case • it's less errorprone
for Statement • syntax: • for (<init statement>; <condition>; <end statement>) { //code} • the body can contain several statements • the init statement is executed • then thecondition is evaluated • while it is true, code and then end statement are executed • and control goes back to evaluation of the expression • once it is false, control continues • code after the next statement after curly bracket is executed • if there is any one statement in the body, you can omit {} • but we recommend to use the curly bracket in any case • it's less errorprone
for Statement • most often used form: • for (<declare var>; <check var>; <increment var>) { //code} • for (int i = 0; i < n; i++) {…} • goes through values 0, 1, 2, …, n-1 • for (int i = 0; i < n; i += 2) {…} • goes through values 0, 2, 4, 8, …, n-1 • for (int i = n - 1; i >= 0; i--) {…} • goes through values n-1, n-2, …, 2, 1, 0 • e.g. suppose we have a linked list of nodes • starts with a head node • each node has a next field that points to the next node in the list • for (Node node = head; node != null; node=node.next) {…}
Repetition Statements: Scope, etc. • the scope of variables is the entire repetition statement • variable declared within parentheses ()is valid in block {} • e.g. for (int i = 0; i < n; i++) { System.out.println (i + " sq = " + i * i); } • note that condition within all loops tests for true • repeat while condition is true • stop when condition is false • beware of endless loops • make sure the condition becomes true eventually • it you really, really need an endless loop: • while (true) {…} • there is a for-each loop • see later
break, continueStatements • break immediately finishes the loop • control continues after the loop's body • continueskips the remaining statements in the loop • but doesn't finish the loop • control continues with evaluation of the condition • next iteration • return immediately finishes the entire method • control continues after the method call • typically within an if statement • e.g.: for (int i = 0; i < 10; i++) { if (shape(i).isOutside()) {continue;} shape(i).draw()) } • use condition rather then break