Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(JAVA) 1. Code snippet example of a sential controlled loop 2. T/F. Both while l

ID: 3890269 • Letter: #

Question

(JAVA)

1. Code snippet example of a sential controlled loop

2. T/F. Both while loops and for loops are pretest loops

3. What is wrong with the following loop that is supposed to print the even numbers from 2 to 10?

4. what is equivalent to the following snippet?

5. What is the difference between conditions that control loops and conditions that control if statements?

6. What is wrong with the following code?

4. what is equivalent to the following snippet?

  int i = 5;  
  while (i > 0)   
  {  
  ...  
  i--;  
  }  

5. What is the difference between conditions that control loops and conditions that control if statements?

6. What is wrong with the following code?

  Scanner kybd = new Scanner(System.in);  
  System.out.println("Enter an int: ");  
  int i = kybd.nextInt();  
  int j = 1;  
  while (i > 0)   
  {  
  System.out.println(j++);  
  }  

Explanation / Answer

Answer:

1) Any loop where the exit condition is based on user input is an example.

Scanner sc=new

Scanner(System.in) ;

int x=sc.nextInt();

while (x!=0) x=sc.nextInt();

2) TRUE , They both pretest. Only do-while loop posttest.

3) Programmatically nothing wrong. As per software Engineer practice, it is better to write 2<=10. But software engineering practice is very controversial, and only stupid people will emphasize to much for little things which don't make a difference.

4) int i;

for (i=5;i>0;i--)

{

...

}

5) With if statement, the condition can be true or false, and can have optional else statement to handle the false result. With loop, there must be some like of update inside the loop block ensuring that the condition will result in false at some point.

6) Variable i is the only decision factor of the loop condition. However, there is no update statement for variable i inside the loop block. Thus, the code will be stuck in an infinite loop.