Condition that cause a block of code to repeat.
Problem: Print welcome to java 100 times
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
…
…
…
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
Problem: Print welcome to java 100 times using loop
int count = 0;
while (count < 100) {
System.out.println("Welcome to Java");
count++;
}
while (loop-continuation-condition) {
// loop-body;
Statement(s);
}
int count = 0;
while (count < 100) {
System.out.println("Welcome to Java!");
count++;
}
int count = 0; //Initialize count
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
//Demo on Intellij
int i = 1;
.......
(i < 6) {
System.out.println(i);
......
}
int i = 1;
while
(i < 6) {
System.out.println(i);
i++;
}
The data != 0 within the while (data != 0) { ... }
is the sentinel-controlled-condition.
Don’t use floating-point values for equality checking in a loop control. Since floating-point values are approximations for some values, using them could result in imprecise counter values and inaccurate results. Consider the following code for computing 1 + 0.9 + 0.8 + ... + 0.1:
double item = 1; double sum = 0;
while (item != 0) { // No guarantee item will be 0
sum += item;
item -= 0.1;
}
System.out.println(sum);
do {
// Loop body;
Statement(s);
} while (loop-continuation-condition);
int count = 0;
do
{
count++;
System.out.println (count);
} while (count < 5);
int i = 1;
{
System.out.println(i);
i++;
}
(i < 6);
int i = 1;
do
{
System.out.println(i);
i++;
}
while
(i < 6);
for (initial-action; loop-continuation-condition; action-after-each-iteration) {
// loop body;
Statement(s);
}
for (int i = 0, i<100; i++) {
// Do something
}
int i;
for (i = 0; i < 100; i++) {
System.out.println("Welcome to Java!");
}
(int i = 0; i < 10; ) {
System.out.println("IS 147");
}
for(int i = 0; i < 10; i++) {
System.out.println("IS 147" );
}
for (int i = 1; i < 100; System.out.println(i++));
for (int i = 0, j = 0; (i + j < 10); i++, j++) {
// Do something
}
//using while loop
int count = 0; //Initialize count
while (count < 5) {
System.out.println("Welcome to Java!");
count++;
}
//using do while loop
int count1 = 0;
do
{
count1++;
System.out.println ("Welcome to Java!");
} while (count1 < 5);
// Using For loop
int i;
for (i = 0; i < 5; i++) {
System.out.println("Welcome to Java!");
}
#(a)
for(;;){
//Do something
}
# equivalent
#(b)
while(true){
//Do something
}
for (int i=0; i<10; i++); //logical error
{
System.out.println("i is " + i);
}
int i=0;
while (i < 10); //Logical Error
{
System.out.println("i is " + i);
i++;
}
int i=0;
do {
System.out.println("i is " + i);
i++;
} while (i<10); // This is correct
while(loop-condition){
loop body
}
for(loop-condition){
loop body
}
do{
loop body
}while(condition);
while(loop-condition){
loop body
}
for(loop-condition){
loop body
}
do{
loop body
}while(condition);
public static void main(String[] args) {
// Display the table heading
System.out.println(" Multiplication Table");
// Display the number title
System.out.print(" ");
for (int j = 1; j <= 9; j++)
System.out.print(" " + j);
System.out.println("\n-----------------------------------------");
// Print table body
for (int i = 1; i <= 9; i++) {
System.out.print(i + " | ");
for (int j = 1; j <= 9; j++) {
// Display the product and align properly
System.out.printf("%4d", i * j);
}
System.out.println();
}
}
public static void main(String[] args) {
// Initialize sum
float sum = 0;
// Add 0.01, 0.02, ..., 0.99, 1 to sum
for (float i = 0.01f; i <= 1.0f; i = i + 0.01f)
sum += i;
// Display result
System.out.println("The sum is " + sum);
}
public static void main(String[] args) {
int count = 0;
while (count < 100) {
System.out.println("count:" + count);
count = count * 1; // OOPS, does not change count
}
}
break;
continue;
public static void main(String[] args) {
int i=0;
while(i<6){
if(i==3) break;
i++;
}
System.out.println("Loop stopped at " +i);
}
// Stop the loop if i is 5;
for (int i = 0; i < 10; i++) {
if (i == 5) {
......
}
System.out.println(i);
}
for (int i = 0; i < 10; i++) {
if (i == 5) {
break
;
}
System.out.println(i);
}
public static void main(String[] args) {
int i=0;
while(i<5){
i++;
if(i==3) continue;
System.out.println("pass " +i);
}
}
// In the loop, when the value is "4", jump directly to the next value.
for (int i = 0; i < 10; i++) {
if (i == 4) {
.......
}
System.out.println(i);
}
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
System.out.println(i);
}
- HW 1
- Exam I
- Group
- Group Project Task