What are the 3 parts to a for

  • Slides: 13
Download presentation
What are the 3 parts to a for loop for ( initialization; condition; increment

What are the 3 parts to a for loop for ( initialization; condition; increment ) Purpose A for loop is a group of statements that will run a set number of times depending on the loop condition and increment/decrement value. n A for loop has three parts: n ¨ Initialization of variable, ¨ Testing of condition, and ¨ Incrementing of variable.

For Loop for(int j = 3; j <= 79; j++) { 3 parts to

For Loop for(int j = 3; j <= 79; j++) { 3 parts to a for-loop 1. Initializing expression int j = 3; to initialize an iteration variable. 2. Control expression j <= 79; tested before each time the loop is done 3. Step expression j++ increments an iteration variable

The for Loop (cont’d) n Example: Open For. Tester from S Drive compile and

The for Loop (cont’d) n Example: Open For. Tester from S Drive compile and run for (int count=1; count <=10; count ++) { System. out. print(“The value of count is “); System. out. print (count); System. out. println(“. ”); } } What prints? Initialization Testing Change

The for Loop (cont’d) n Uncomment the last print statement compile and run. for

The for Loop (cont’d) n Uncomment the last print statement compile and run. for (int count=1; count <=10; count ++) { System. out. print(“The value of count is “); System. out. print (count); System. out. println(“. ”); } System. out. println(count); } What happens? Initialization Testing Change

Scope: visibility n Scope refers to the visibility of variables. The variable is creating

Scope: visibility n Scope refers to the visibility of variables. The variable is creating inside the for statement and is not visible outside the for. Only visible between the curly braces. for (int count =1; count<=10; count++) {. . . } The scope of count is the body of the loop, and count is undefined outside the loop

Now uncomment the int count = 1 and take the int off of the

Now uncomment the int count = 1 and take the int off of the count variable in the for statement. Compile and run. int count=1; n for (count=1; count <=10; count ++) n { ¨ System. out. print(“The value ¨ System. out. print (count); ¨ System. out. println(“. ”); n n n } System. out. println(count); } of count is “);

Difference between a while and a for statement While is used if you do

Difference between a while and a for statement While is used if you do not know the number of iterations n For is used if you know the number of iterations. n Both conditions are evaluated before the loop is executed. n

Open While. Tester from S Drive. Change it to a for loop /*Name *

Open While. Tester from S Drive. Change it to a for loop /*Name * Period 7 * 8/31/09 * */ public class While. Tester { public static void main(String[]args) { int sum = 0; int num = 1; for(int num=1; num<10; num++){ while (num < 10) { sum +=num; sum += num; System. out. println(num + " time through loop sum is " + sum); } System. out. println(num + " time through loop sum is " + sum); num++; } } }} }

Open Up Dr. Java: public class Count. Down { public static void main (String[]args)

Open Up Dr. Java: public class Count. Down { public static void main (String[]args) { for(int i=20; i>0; i--){ System. out. println("count is; "+ i); }}}

Change count. Down to make it print the following: 1. Write a for loop

Change count. Down to make it print the following: 1. Write a for loop that will print the numbers 3, 6, 12, and 24 for(int i = 3; i <=24 ; i=i+i){ 1. Initialize varaiable 2. Control expression What should it start with? What do we want the condition to be? 3. Increment or step expression How much do we want to increase each time.

Change count. Down to make it print the following: 2. Write a for loop

Change count. Down to make it print the following: 2. Write a for loop that will print the numbers 24, 12, 6, 3 for(int i = 24; i >=3 ; i=i/2){ 1. Initialize varaiable 2. Control expression What should it start with? What do we want the condition to be? 3. Increment or step expression How much do we want to increase each time.

Complete the for. Loop worksheet n Open For. Statements program and create method for

Complete the for. Loop worksheet n Open For. Statements program and create method for 14 -20. Static methods do not require an object be created to call or invoke the method. Use #14 as an example. Follow instructions on worksheet.

Summary n n n The for loop is shorter, and combining the initialization, test,

Summary n n n The for loop is shorter, and combining the initialization, test, and increment in one statement makes it easier to read and verify that it's doing what you expect. The for loop is better when you are counting something. If you are doing something an indefinite number of times, while loop may be the better choice.