Homeinterviewinterview program javaAll 8 program for loop in java

All 8 program for loop in java

Write a program for loop in java (While loop)

package com.onurdesk.loop.program;

public class WhileLoopExample {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {

		int i = 1;
		System.out.println("While Loop Demo");
		while (i <= 10) {
			System.out.println(i);
			i++;
		}
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

Write a program for loop in java(do While)

package com.onurdesk.loop.program;

public class DoWhileLoopExample {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {

		int i = 1;
		do {
			System.out.println("Do While Loop Example");

		} while (i < 1);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}
}

Write a program for loop in java(for Loop)

package com.onurdesk.loop.program;

public class ForLoopExample {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {

		System.out.println("For Loop Example: ");

		int[] intArray = { 1, 2, 3, 4, 5 };
		for (int i = 0; i < intArray.length; i++)
			System.out.println(intArray[i]);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

Write a program for loop in java(For Each)

package com.onurdesk.loop.program;

public class ForEachLoopExample {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {

		System.out.println("For Each Loop Example: ");

		int[] intArray = { 1, 2, 3, 4, 5 };

		for (int i : intArray)
			System.out.println(i);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

Write a program to print message without SYSOUT.

package com.onurdesk.loop.program;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class PrintMsgWithOutSysOut {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */

	public static void main(String[] args) throws IOException {

		System.out.write("Java Programs App \n".getBytes());
		System.out.format("%s", "Java Programs App \n");
		PrintStream printStream = new PrintStream(new FileOutputStream(FileDescriptor.out));
		printStream.print("Java Programs App \n");
		System.err.print("Error message");
		System.out.println("Thanks for visiting https://onurdesk.com");


	}

}

Write a program for loop in java for Enum

package com.onurdesk.loop.program;

enum Colour {
	Black, White, Red, Green, Yellow, Grey
}

public class EnumExample {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */

	public static void main(String[] args) {

		Colour colour;

		colour = Colour.Black;

		System.out.println("Selected " + colour + " Colour");

		colour = Colour.Yellow;

		System.out.println("Selected " + colour + " Colour");
		System.out.println("Thanks for visiting https://onurdesk.com");

	}
}

Write a program for loop in java (Break Statement)

package com.onurdesk.loop.program;

public class BreakStatement {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {

		for (int i = 1; i < 10; i++) {
			System.out.println("i=" + i);
			if (i == 4) {
				System.out.println("Break statement executes and loop breaks when i value is equal to 4");
				break;
			}
		}
		System.out.println("Thanks for visiting https://onurdesk.com");

	}
}

Write a program for loop in java(Switch Case Statement)

package com.onurdesk.loop.program;

import java.util.Scanner;

public class SwitchCaseExample {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		System.out.println("Enter one digit from 0 to 6: ");
		int num = input.nextInt();

		switch (num) {
		case 0:
			System.out.println("Your Lucky Day is Sunday");
			break;
		case 1:
			System.out.println("Your Lucky Day is Monday");
			break;
		case 2:
			System.out.println("Your Lucky Day is Tuesday");
			break;
		case 3:
			System.out.println("Your Lucky Day is Wednesday");
			break;
		case 4:
			System.out.println("Your Lucky Day is Thursday");
			break;
		case 5:
			System.out.println("Your Lucky Day is Friday");
			break;
		case 6:
			System.out.println("Your Lucky Day is Saturday");
			break;
		default:
			System.out.println("Please select the right option");
			break;
               }
			System.out.println("Thanks for visiting https://onurdesk.com");
	}
}

Admin
Onurdesk is a technical platform based primarily on Spring, Java, and Nodejs ecosystem app creation. We write tutorials and manuals concentrating extensively on realistic and real-life usage cases.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

Recent Comments