Presentation is loading. Please wait.

Presentation is loading. Please wait.

Looping Examples I.

Similar presentations


Presentation on theme: "Looping Examples I."— Presentation transcript:

1 Looping Examples I

2 Print out the Numbers from 1 to 10
public void printNums() { for(int count = 1; count <= 10; count++) System.out.println(count); }

3 Print out the Numbers from 1 to 10
public void printNumsWhile() { int count = 1; while(count <= 10) System.out.println(count); count++; }

4 Your Turn Change the code to print from 1 to 25 5 to 10 17 to 135

5 Print every Second Number from 1 to 10
public void printSkipNums() { for(int count = 1; count <= 10; count += 2) System.out.println(count); }

6 Print every Second Number from 1 to 10
public void printSkipNumsWhile() { int count = 1; while(count <= 10) System.out.println(count); count += 2; }

7 Your Turn Change the code to print 1,5,9 5,10,15,25,30,35
10,9,8,7,6,5,4,3,2,1 Also need to change the condition! 50,40,30,20,10,0

8 Make a new method public void printRange(int start, int end, int change) -You may assume change will be positive -You need an if/else if change can be negative

9 Find the Sum of values in an array
public int sum(int[] nums) { int sum = 0; int index = 0; while(index < nums.length) sum += nums[index]; index++; } return sum; }//You make the for loop of this method

10 Your Turn Write a method to find the average value in an array
public int average(int[] nums) Write a method to add up only the even numbers in an array public int evenSum(int[] nums) Write a method to count how many values in an array are positive public int posCount(int[] nums)


Download ppt "Looping Examples I."

Similar presentations


Ads by Google