0% found this document useful (0 votes)
185 views7 pages

Csc111-Tutorial07 1

This document contains tutorials and exercises on repetitive statements like while, do-while, and for loops in Java. It includes examples of analyzing loop conditions, converting loops, prompting users for input, and printing patterns. Solutions are provided for exercises involving finding maximum numbers, summing inputs, and printing numbers between two inputs in descending order.

Uploaded by

Osama Maher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views7 pages

Csc111-Tutorial07 1

This document contains tutorials and exercises on repetitive statements like while, do-while, and for loops in Java. It includes examples of analyzing loop conditions, converting loops, prompting users for input, and printing patterns. Solutions are provided for exercises involving finding maximum numbers, summing inputs, and printing numbers between two inputs in descending order.

Uploaded by

Osama Maher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

 

College of Computer and Information Sciences 
CSC111 – Computer Programming I  
 

Tutorial 07 
Repetitive Statements: while | do­while | for 

Exercise 1: 
A. Analyze the following code. Is count < 100 always true, always false, or sometimes true or 
sometimes false at Point A, Point B, and Point C? 
int count = 0; 
while (count < 100) { 
  // Point A 
  System.out.println("Welcome to Java!"); 
  count++; 
  //Point B 

// Point C 
B. How many times are the following loop bodies repeated? What is the output of each loop? 
1. int i = 1; 
while (i<10) 
    if (i % 2 == 0) 
    System.out.println(i); 
2. int i = 1; 
while (i<10) 
    if (i % 2 == 0) 
    System.out.println(i++); 
3. int i = 1; 
while (i<10) 
    if (i++ % 2 == 0) 
    System.out.println(i); 

C. Suppose the input is 2 3 5 4 0. What is the output of the following code? Explain what it does. 
import java.util.Scanner; 
public class Test { 
  public static void main(String[] args){ 
    Scanner input = new Scanner(System.in); 
    int number, max; 
    number = input.nextInt(); 
    max = number; 
    while (number != 0) { 
      number = input.nextInt(); 
      if (number > max) 
        max = number; 
    } 
    System.out.println("max is " + max); 
    System.out.println("number is " + number); 
  } 

 
 

D. Convert the following while loop into a do­while loop. 
Scanner input = new Scanner(System.in); 
int sum = 0; 
System.out.println("Enter an integer (input ends if it is 0)"); 
int number = input.nextInt(); 
while (number != 0) { 
  sum += number; 
  System.out.println("Enter an integer (input ends if it is 0)"); 
  number = input.nextInt(); 

E. Suppose the input is 2 3 4 5 0. What is the output of the following code? 
import java.util.Scanner; 
public class Test { 
  public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    int number, sum = 0, count; 
    for (count = 0; count < 5; count++) { 
      number = input.nextInt(); 
      sum += number; 
    } 
    System.out.println("sum is " + sum); 
    System.out.println("count is " + count); 
  } 

F. How many times is the println statement executed in the following code? 
for (int i = 0; i < 10; i++) 
  for (int j = 0; j < i; j++) 
    System.out.println(i * j); 

Exercise 2: 
Show the output of the following programs? 
A. public class Test { 
  public static void main(String[] args) { 
    for (int i = 1; i < 5; i++) { 
      int j = 0; 
      while (j < i) { 
        System.out.print(j + " "); 
        j++; 
      } 
    } 
  } 

B. public class Test { 
  public static void main(String[] args) { 
    int i = 0; 
    while (i < 5) { 
      for (int j = i; j > 1; j­­) 


 

        System.out.print(j + " "); 
      System.out.print("****"); 
      i++; 
    } 
  } 

C. public class Test { 
  public static void main(String[] args) { 
    int i = 5; 
    while (i >= 1) { 
      int num = 1; 
      for (int j = 1; j <= i; j++) { 
        System.out.print(num + "xxx"); 
        num *= 2; 
      } 
      System.out.println(); 
      i­­; 
    } 
  } 

D. public class Test { 
  public static void main(String[] args) { 
    int i = 1; 
    do { 
      int num = 1; 
      for (int j = 1; j <= i; j++) { 
        System.out.print(num + "G"); 
        num += 2; 
      } 
      System.out.println(); 
      i++; 
    } while (i <= 5); 
  } 

Exercise 3: 
Write a program using for loop that prompts the user to enter two integers x and y. The program 
prints numbers between x and y (excluding x and y) that are either divisible by x or divide y in 
reverse (from largest to smallest).  

Here are two sample runs: 
Enter two integers: 10 50 ↵ 
40 30 25 20  
 
Enter two integers: 5 1 ↵
 


 

Exercise 4 
Solve exercise 2 using while loop and without using logical operators ​ ||​ &&​
 and ​ . (Note: there is no 
||​
relation between while and ​ &&​
, ​ . This is just to train you on different equivalent ways of writing 
loops and conditional statements) 

Exercise 5 
Write a program that reads a character then displays the following pattern using the input character 
(assuming input character is ‘A’ and height is 6): 
     A  
    A A  
   A A A  
  A A A A  
 A A A A A  
A A A A A A  

Height of pattern and character are input by user. 
input.next().charAt(0);​
(Hint: assuming name of your Scanner object is input, use ​  to read 
a character from user.) 
 
 
   


 

Tutorial 07 Solutions 

Exercise 1: 
count < 100​
A. Point A: ​  is always ​
true 
count < 100​
Point B: ​ true ​
 is sometimes ​ false ​
and sometimes ​ false?​
(when is it ​ ) 
count < 100​
Point C: ​  is always ​
false 

B. (1) will repeat forever (infinite number of iterations) 
(2) will repeat forever (infinite number of iterations) 
(3) will repeat 9 times 
C.  
max is 5 
number is 0 
This program finds maximum number among input numbers. 

D. import java.util.Scanner; 
public class WhileToDoWhile { 
  public static void main(String[] args){ 
    Scanner input = new Scanner(System.in); 
    int number, sum = 0; 
    do { 
      System.out.print("Enter an integer (input ends if it is 
0)"); 
      number = input.nextInt(); 
      sum += number; 
    } while (number != 0); 
  } 

E.  
sum is 14 
count is 5 

F. 45 times 

Exercise 2: 
A.  
0 0 1 0 1 2 0 1 2 3 
B.  
**** 
**** 
2 **** 
3 2 **** 
4 3 2 **** 


 

  
C.  
1xxx2xxx4xxx8xxx16xxx 
1xxx2xxx4xxx8xxx 
1xxx2xxx4xxx 
1xxx2xxx 
1xxx 
D.  
0 0 1 0 1 2 0 1 2 3 

Exercise 3: 
import java.util.Scanner; 
public class Reverse { 
  public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter two integers: "); 
    int x = input.nextInt(); 
    int y = input.nextInt(); 
    for (int i = y ­1; i > x; i­­) 
      if (i % x == 0 || y % i == 0) 
        System.out.println(i + " ");   
  } 

Exercise 4: 
import java.util.Scanner; 
public class Reverse2 { 
  public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter two integers: "); 
    int x = input.nextInt(); 
    int y = input.nextInt(); 
    int i = y ­ 1; 
    while (i > x) { 
      if (i % x == 0) 
        System.out.println(i + " ");   
      else if (y % i == 0) 
        System.out.println(i + " ");   
      i­­; 
    } 
  } 

Exercise 5: 
import java.util.Scanner; 
public class Triangle { 
  public static void main(String[] args) { 


 

    Scanner input = new Scanner(System.in); 
    System.out.print("Enter character: "); 
    char c = input.next().charAt(0); 
    System.out.print("Enter height: "); 
    int height = input.nextInt(); 
    for (int i = height; i <= height; i++) { 
      for (int j = height ­ i; j >= 1; j­­) 
        System.out.print(" "); 
      for (int j = i; j >= 1; j­­) 
        System.out.print(c + " "); 
      System.out.println(); 
    } 
  } 

 

You might also like