Switch, Break, Continue & Comment Statements in Java

Java Switch Statement

Java Switch Statement is used to check conditions based on a single integer and enumerated value. It is used to execute a single statement from multiple expressions. Byte, Character, Short, and Integer are some data types that the switch statement works with. It is fall-through and is similar to the if-else-if ladder statement.

Here is an example of the switch statement:

package com.javafolder.corejava.switchStatement;

import java.util.Scanner;

public class Questions {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Choose a food from this list: 1. Cake 2. Pizza 3. Burger");

		int option = sc.nextInt();

		switch (option) {  // <-----

		case 1:
			System.out.println("Cake");
			break;

		case 2:
			System.out.println("Pizza");
			break;

		case 3:
			System.out.println("Burger");
			break;

		default:
			System.out.println(" Sorry we don't have any other options");

		}

	}
}

 Java Break Statement

Java break statement is used to break the loop in java. A loop ends immediately after a break statement is encountered. After the loop gets terminated, the program will continue from the next part. It is also used to break the switch statement and inner loops.

Here is an example of the break statement:

                case 1:
			System.out.println("First Break");
			break;  // <-----

		case 2:
			System.out.println("Second Break");
			break;

		case 3:
			System.out.println("Third Break");
			break;

 

Java Continue Statement

Java Continue Statement can be used in both for and while loop to continue the loop. It is used to continue the flow of the program and is used to immediately jump to the next iteration of the loop. It can be used with an inner loop as well.

Here is an example of the continue statement:

public class ContinueDemo {

	public static void main(String args[]) {

		int[] values = { 5, 10, 15, 20, 30, 40, 60, 90, 100 };

		for (int a : values) {
			if (a == 30) {
				continue; // <-----
			}
			System.out.print(a + "\n");
		}
	}
}

Java Comments Statement

Java Comments Statements are those statements which provide the information about a method, variable, or class. It is also used to hide code and is not executed by the interpreter or the computer.

The types of Java comments are:

  1. Single Line Comment

Single line comment explains only a single line.

// <-- This is a single line comment.
  1. Multi-Line Comment

Multi-Line Comment explains multiple lines in a code. ‘Ctrl + Shift+/’ keys the shortcut key to do multiline comment.

/* 
This  
is a 
multi line  
comment. 
*/
  1. Documentation Comment

Documentation comment is used to explain an API by using the Javadoc tool.

/** documentation */ <-- this is a documentation line comment.

Java Programs

There are various programs in Java which are listed below:

  • Fibonacci Series
  • Prime Number
  • Palindrome Number
  • Factorial Number
  • Armstrong Number

Java Sorting Programs

  • Bubble Sort
  • Selection Sort
  • Insertion Sort

Java Searching Programs

  • Linear Search
  • Binary Search

Leave a Reply

Your email address will not be published. Required fields are marked *