A. Introduction
do loops are essentially same at while loops except that they will run at least once. All three types of loops can be converted into each other... Which one to choose?
B. Watch(slides)
C. Try this:
Write a do loop that reads integers and computes their sum. Stop when reading a
zero or the same value twice in a row. For example, if the input is 1 2 3 4 4, then
the sum is 14 and the loop stops.
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("how many numbers you gon input?");
int y = in.nextInt();
int sum = 0;
int previous_in = 0;
int i = 0;
do{
int x = in.nextInt();
sum += x;
if(x == 0 || x == previous_in){
break;
}
previous_in = x;
i++;
}while (i < y);
System.out.println(sum);
}
}