So I'm in a computer science class, and the teacher teaches us nothing and I have no clue what I am suppose to be doing. This coding stuff is in eclipse. All the teacher gives us is a google docs that has little info. This is one of the docs.
The while loop
Another form of loop is called the while loop. This loop can have two different forms:
while (condition) {
//do stuff
// this code will be executed as long as condition is true
// if condition is initially false, this code will never be executed
}
The other form is called a do while loop:
do {
// do stuff
// this code will be executed at least once
} while (condition)
The difference between the loops is that in the second form (do...while), the code is executed at least once.
Try these out:
Make a new package and name it dowhileloops for the following code.
boolean loopflag = false;
while (loopflag) {
System.out.println("hey, do you even print?");
}
do {
System.out.println("how many times does this print?");
} while (loopflag);
int i = 0;
while (i < 10) {
System.out.println("i is " + i);
++i;
}
int j=1;
while ( j > 0) {
System.out.println("How do you stop this? " + j);
j=j+1;
}
Exercises:
Put your answers to the following questions in the dowhileloops package.
The while loop
Another form of loop is called the while loop. This loop can have two different forms:
while (condition) {
//do stuff
// this code will be executed as long as condition is true
// if condition is initially false, this code will never be executed
}
The other form is called a do while loop:
do {
// do stuff
// this code will be executed at least once
} while (condition)
The difference between the loops is that in the second form (do...while), the code is executed at least once.
Try these out:
Make a new package and name it dowhileloops for the following code.
boolean loopflag = false;
while (loopflag) {
System.out.println("hey, do you even print?");
}
do {
System.out.println("how many times does this print?");
} while (loopflag);
int i = 0;
while (i < 10) {
System.out.println("i is " + i);
++i;
}
int j=1;
while ( j > 0) {
System.out.println("How do you stop this? " + j);
j=j+1;
}
Exercises:
Put your answers to the following questions in the dowhileloops package.
- Create a class called DoLoop. In the main method, write a do loop that will print out the numbers from 0 to 10.
- Create a class called DoWhileLoop1. In the main method, write a do while loop that will take an integer from the keyboard and loop until the user guesses the correct number.
- Create a class called DoWhileLoop2. In the main method, write a do while loop that will take a string from the keyboard and loop until the user guesses the correct password.
- Create a class called DoWhileLoop3. In the main method, write a while loop that will print out the characters from A to Z.