1 / 23

# Looping - PowerPoint PPT Presentation

Looping. Yong Choi School of Business CSU, Bakersfield. Objectives. Learn about the loop structure Use a while loop Use shortcut arithmetic operators Use a for loop Learn how and when to use a do…while loop Learn about nested loops. Learning about the Loop Structure.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Looping' - dai

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Looping

Yong Choi

CSU, Bakersfield

• Learn about the loop structure

• Use a while loop

• Use shortcut arithmetic operators

• Use a for loop

• Learn how and when to use a do…while loop

• Loop: A structure that allows repeated execution of a block of statements

• Loop body: A block of statements; as long as the expression is true, the loop body executes

• Iteration- One execution of any loop

• while Loop: execute a body of statements continually as long as the Boolean expression continues to be true

• Consists of the keyword while followed by a Boolean expression within parentheses followed by the body of the loop

• Use when you need to perform a task a predetermined number of times

• Incrementing – Altering a loop by adding one to loop control variable

• Decrementing – Altering a loop by subtracting one from a loop control variable

public class loopExample {

public static void main (String[] args ) {

int count = 1;// start count out at one

while ( count <= 3 )// loop while count is <= 3

{

System.out.println( "count is:" + count );

count = count + 1;// add one to count, same as (count++)

}

System.out.println( "Done with the loop" ); } }

while ( condition )

loop body // a statement or block statement

• When the condition is true, the loop body is exectued.

• When the condition is false, the loop body is skipped, and the statment after the loop is executed.

• Once execution has passed to the statement after the loop, the while statement is finished, at least for now.

• If the condition is false the very first time it is evaluated, the loop body will not be executed even once.

To increase a variable’s value by exactly one:

• prefix ++

• Used before the variable name

• ++someValue;

• postfix ++ (recommend)

• Used after the variable name

• anotherValue++;

int count = 0; // count is initialized

while ( count <= 6 ) // count is tested

{

System.out.println( "count is:" + count );

count = count + 2; // count is changed by 2

}

System.out.println( "Done counting by two's." );

• The loop control variable in a counting loop can be changed by a negative value.

• Here is a program fragment that decrements the loop control variable at the bottom of each iteration:

int count = 2; // count is initialized

while ( count >= 0 ) // count is tested {

System.out.println( "count is:" + count );

count = count - 1; // count is changed by -1 } System.out.println( "Done counting down." );

• What’s the result of below loop?

int count = 13;

int decrement = -1;

while ( count >= 0 )

{

System.out.println( "count is:" + count );

count = count - decrement;

}

System.out.println( "Count was " + count + " when it failed the test");

• For loop: A special loop that is used when a definite number of loop iterations is required

• Keyword for

• Use a set of parentheses

• Three sections within parentheses

• Initializing the loop control variable

• Testing the loop control variable

• Updating the loop control variable

public class loopExample {

public static void main (String[] args ) {

int count, sum;

sum = 0;

for ( count = 0; count <= 5; count++ )

{

sum = sum + count ;

System.out.print( count + " " );

}

System.out.println( "sum is: " + sum );

• Java (and several other languages) has a for statement which combines the three aspects of a loop into one statement. In general, it looks like this:

for ( initialize ; test ; change )

loopBody ;

• The initialize, test , and change are statements or expressions that (usually) perform the named action. The loopBody can be a single statement or a block statement.

• Here is an example of a for statement:

for ( count = 0; count < 10; count++ )

System.out.print( count + " " );

int count,

sum;

sum = 0;

for ( count = 0; count <= 5; count++ )

{

sum = sum + count ;

System.out.print( count + " " );

}

System.out.println( "sum is: " + sum );

While loop

int count,

sum;

sum = 0;

count = 0;

while ( count <= 5 )

{

sum = sum + count ;

System.out.print( count + " " );

count++ ;

}

System.out.println( "sum is: " + sum );

Side By Side

• The while loop can be used to implement any loop.

• However, the for loop is very convenient.

• The do…while loop is occasionally convenient.

• Of the three looping statements, it is used the least.

• Some programmers prefer not to use it at all.

Learning How and When to Use a do…while loop

• Checks at the bottom of the loop after one repetition has occurred

• Bottom-driven loop

• Loop body executes at least one time

• The loop starts with the keyword do

• The body of the loop is contained within curly braces

int count = 0; // initialize count to 0

do

{

System.out.println( count ); // loop body: includes code to

count++ ; // change the count

}

while ( count < 10 ); // test if the loop body should be

// executed again.

import java.io.* ;

public class SqrtCalc {

public static void main( String[] args ) throws IOException { String chars ;

double x;

chars = "yes" ;

while ( chars.equals( "yes" ) || chars.equals( "YES" ) ||

chars.equals( "y" ) || chars.equals( "Y" ) )

{

System.out.print("Enter a number-->");

chars = stdin.readLine(); x = (Double.valueOf(chars)).doubleValue(); System.out.println("Square root of " + x + " is " + Math.sqrt( x ) ); System.out.print("Do you wish to continue? (yes or no) -->");