DOC PREVIEW
UGA CSCI 1301 - LoopExercises

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCI 1301 While, Do-while and For Loop Exercises 1) Given the following loop: int i=19; while (i<29) { i=i+3; } a) What will the value of the variable i be after the loop terminates? b) How many will the body of the loop iterate? c) Rewrite the previous loop as the for loop. 2) Given the following loop: int x=10; while (x >=5 && x<=8) { if (x % 2 == 0) { x=x + 3; } else { x=x -2; } } a) What will the value of the variable x be after the loop terminates? b) How many times will the body of the loop iterate? c) If the initial value of x is 7 instead of 10, how many times will the body of the loop iterate? 3) Given the following loop: int w=1; int count = 0; do { w++; System.out.println("w="+w); } while (w >=5 && w<= 8); a) What will the value of the variable w be after the loop terminates? b) How many times will the body of the loop iterate?4) Given the following loop: int w=0; while (w % 10 != 2) { w= (w + 4) % 2; System.out.println("w="+w); } How many times will the body of the loop iterate? 5) Can you rewrite the following while loop as a do-while loop to produce the same output? int x=0; int sum=0; System.out.println(″Please enter an integer between 0 and 10 :″); x=keyboard.nextInt(); while (x > 0 && x < 10) { sum =+x; System.out.println(″Please enter an integer between 0 and 10 :″); x=keyboard.nextInt(); } 6) Can you rewrite the following do-while loop as a while loop to produce the same output? y = (int) Math.random()*100 + 1; do { System.out.println(″y=″+y); y=2*y + 1; } while (y < 100); 7) What will be produced by the following code fragment? for (int num=10; num <= 100; num++) System.out.println(num); 8) What output will be produced by the following code fragment? int num=1 for (num=10; num <= 100; num++); System.out.println(num); 9) Indicate the output of the following code fragment: for (int num=100; num >= 10; num--){ System.out.println(num); }10) Indicate the output of the following code fragment? int increment=3; for (int num=0; num <= 200; num +=2 + increment*3){ System.out.println(num); } How many times does the previous loop iterate? 11) Indicate the output of the following code fragment? for (int num=1; num <= 42; num +=4){ if (num % 3 == 0) System.out.println(num); } How many times does the previous loop iterate? 12) Indicate the output of the following code fragment? for (int i=30; i > 10; i++) { System.out.println(″i=″+i); } How many times does the previous loop iterate? 12) Given the following code fragment? for (int i=1; i < 15; i+=1) { if (i % 3 == 1) { System.out.println(″i=″+i); continue; } } a) What is the output of the code fragment? b) How many times does the previous for loop iterate? c) Rewrite an equivalent for loop that do not the continue statement.13) Rewrite the following code fragment loop so it does not use any continue and break statements: int i = 100; while (true) { i-=2; if (i % 6 == 0) { System.out.println(i); continue; } if (i % 4 == 0) { System.out.println(i); continue; } if ( i % 7 == 0 && i % 5 == 0) { break; }


View Full Document

UGA CSCI 1301 - LoopExercises

Documents in this Course
Load more
Download LoopExercises
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LoopExercises and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LoopExercises 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?