Java loops: while, do-while, for

Java provides for while, do-whild, and for syntax for iterative control. You do not need to specify a block ({}) if the sentence to be repeated is a single line, but if the sentence to be repeated is several sentences, you must specify the block.

while

while executes the while block only while the condition specified by the conditional is true.

while(condition) {
	sentence;
}

If you use the following conditional, the while block will not execute.

int a = 10, b = 20;
while(a>b) {
	System.out.println("This sentence does not appear forever");
}
class While {
	public static void main(String args[]) {
		int n = 1;
		System.out.print("An integer between 1 and 10 = ");
		while(n<11) {
			System.out.print(n + " ");
			n++;
		}
	}
}
while Basic Example 1
class WhileNoBody {
	public static void main(String args[]) {
		int i = 10, j = 20;
		while( ++i < --j) {
			System.out.print("The middle value between 10 and 20 is " + i);
		}
	}
}
while Basic Example 2

do-while

do-while is a loop that evaluates the condition later. do-while defines the statement to be executed at least once through the do statement before evaluating the condition.

do {
	sentence
} while(condition);
class DoWhile {
	publid static void main(String args[]) {
		int n = 1;
		System.out.println("** multiplication table 3rd **");

		do {
			System.out.println(" " + 3 + "*" + n + " = " + (3*n));
			n++;
		} while(n<10);
	}
}
do-while basic example 1
class DoWhile {
	publid static void main(String args[]) {
		int n = 1;
		System.out.println("** multiplication table 3rd **");

		do {
			System.out.println(" " + 3 + "*" + n + " = " + (3*n));
		} while(++n<10);
	}
}
do-while basic example 2
class DoUseMenu {
	public static void main(String args[]) throws java.io.IOException {
		char choice;

		do {
			System.out.println("Object-oriented terminology");
			System.out.println("1. Object");
			System.out.println("2. Class");
			System.out.println("3. Message");
			System.out.println("4. Inheritance");
			System.out.print("Please enter your desired number : ");
			 choice = (char) System.in.read();
		} while( choice < '1' || choice > '4');

		System.out.println("\n");

		switch(choice) {
			case '1' :
				System.out.println("***** Object *****");
				System.out.println("Objects are logical units for managing information.");
				break;
			case '2' :
				System.out.println("***** Class *****");
				System.out.println("A class is a template that creates an object.");
				break;
			case '3' :
				System.out.println("***** Message *****");
				System.out.println("The message is to let the object do the work.");
				break;
			case '4' :
				System.out.println("***** Inheritance *****");
				System.out.println("Inheritance inherits all attributes and procedures from the superclass.");
				break;
		}
	}
}
do-while application example (using switch)

for

The for syntax executes the block while satisfying the condition, starting from the given initial value. First, the initial value is set and the condition is judged. If it is true, the block is executed and the condition is increased or decreased.

for(Initial value; Condition; increase or decrease) {
	 Repeating sentences
}
class For {
	public static void main(String args[]) {
		int n;
		System.out.print("An integer from 1 to 10 : ");
		for(n = 1; n < 11; n++) {
			System.out.print(n + " ");
		}
	}
}
for basic example 1
class Nestedfor {
	public static void main(String args[]) {
		int i, j;
		for(j = 1; i<10; i++) {
			for(j = 1; j<i; j++) {
				System.out.print(" * ");
			}
			System.out.println();
		}
	}
}
for basic example 2
class DoubleC {
	public static void main(String args[]) {
		int a, b;
		for(a=1, b=10; a<b; a++, b--) {
			System.out.print("a = " + a);
			System.out.println("b = " + b);
		}
	}
}
for basic example 3
class Forflag {
	public static void main(String args[]) {
		int i;
		boolean flag = false;
		i = 1;
		for( ; !flag; ) {
			System.out.println("The value of i is " + i);
			if(i == 10) flag = true;
			i++;
		}
	}
}
for basic example 4
class Alphabet {
	public static void main(String args[]) {
		int i;
		char c;
		for(i=0; i<26; i++) {
			System.out.print((char)('A' + i));
			System.out.print("(" + ('A' + i) + "), ");
			System.out.print((char)('a' + i));
			System.out.print("(" + ('a' + i) + ") \t");
			if(i%5 == 4) {
				System.out.println();
			}
		}
	}
}
for basic example 5
class MinMax {
	public static void main(String args[]) {
		int n = Integer.parseInt(args[0]);
		int m = Integer.parseInt(args[1]);
		int i;
		for(i=1; i<=m*n; i++) {
			if((i%n==0) && (i%m == 0)) {
				break;
			}
		}
		System.out.print("The least common multiple is " + i);

		for(i=ㅜ; i>=1; i--) {
			if((m%i==0) && (n%i == 0)) {
				break;
			}
		}
		if(i==1) {
			System.out.print("There is no maximum common divisor.");
		} else {
			System.out.print("The greatest common divisor is " + i);
		}

	}
}
for basic example 6

Share This Post

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

Latest Post