Presentation is loading. Please wait.

Presentation is loading. Please wait.

FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)

Similar presentations


Presentation on theme: "FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)"— Presentation transcript:

1 FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES."

2

3 FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase) you have a pair of round brackets. Inside of the round brackets you need three things: the start value for the loop, the end value for the loop, and a way to get from one number to another. This is called the increment number, and is usually 1. But it doesn't have to be. You can go up in chunks of 10, if you want. After the round brackets you need a pair of curly brackets. The curly brackets are used to section off the code that you want to execute repeatedly. An example might clear things up.

4 FOLLOWING EXAMPLE SHOWS USE OF SIMPLE FOR LOOP. for( ; ; ) { ; }

5 We start by setting up an integer variable, which we've called loopVal. The next line sets up another integer variable. This variable will be used for the end value of the loop, and is set to 11. What we're going to do is to loop round printing out the numbers from 0 to 10. loopVal =0; loopVal < end_value; loopVal++ The first part tells Java at what number you want to start looping. Here, we're assigning a value of zero to the loopVal variable. This will be the first number in the loop. The second part uses some conditional logic:

6 loopVal < end_value This says "loopVal is less than end_value". The for loop will then keep going round and round while the value inside the loopVal variable is less than the value in the variable called end_value. As long as it's true that loopVal is less than end_value, Java will keep looping over the code between the curly brackets. The final part between the round brackets of the for loop is this: loopVal++ What we're doing here is telling Java how to go from the starting value in loopVal to the next number in the sequence. We want to count from 0 to 10. The next number after 0 is 1. loopVal++ is a shorthand way of saying "add 1 to the value in the variable".

7

8 For-loop to print the values 0, 2, 4, 6,.. 98 for (int i=0; i<100; i+=2) { System.out.println(i); }

9 For-loop from 99 down to 0 99, 98, 97,... for (int i=99; i>=0; i--) { System.out.println(i); }

10 For-loop from 0 to 100 by 5's for (int i=0; i<=100; i+=5) { System.out.println(i); }

11 CODE THAT ADDS UP THE NUMBERS 1 TO 10. TRY IT OUT: 1+2+3+4+5+6+7+8+9+10 Output should be 55

12

13 Exercise Change you code so that the loop adds up the numbers 1 to a 100. The answer you should get is 5050.

14 WRITE A TIMES TABLE PROGRAM. THE PROGRAM SHOULD ASK A USER TO INPUT A NUMBER. THIS NUMBER IS THEN USED AS THE TIMES TABLE. SO IF THE USER ENTERS 10, THE 10 TIMES TABLE SHOULD BE DISPLAYED.

15 int loopVal; int end_value = 11; int addition = 0; int times_table = 0; Scanner kb = new Scanner(System.in); System.out.println("Which times table do you want?"); times_table = kb.nextInt(); for (loopVal = 1; loopVal < end_value; loopVal++) { addition = loopVal * times_table; System.out.println(loopVal + " times " + times_table + " = " + addition); }

16

17


Download ppt "FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)"

Similar presentations


Ads by Google