Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
Switch Statement In Java
Previous Home Next

Similar to the if-else branch, the switch statement is specifically designed to conditionally switch among multiple alternatives. The syntax for the switch statement follows:


 switch (input) {
Java Tutorialsase label1:
Statement1;
break;
Java Tutorialsase label2:
Statement2;
break;

default:
DefaultStatement;
}

The switch branch evaluates and compares input to all of the case label and branches the program’s execution to the matching case statement list. If no case label matches input, the program branches to the DefaultStatement, if one has been defined (DefaultStatement is optional).

In switch a statement list is simply a series, or list, of statements. Unlike the if-else branch, which directs program flow to a simple or compound statement, the switch statement directs the flow to list of statements. When the program execution moves into a case statement list, it continues from there in a sequential manner.

Note that the case labels must be integers. You cannot test strings. For example, the following is an error

Example


String input = "anyvalue";
switch (input) // wrong, show error
{
Java Tutorialsase "p": //wrong, will show error
. . .
break;
. . .
}

Using break, continue for breaking the Control Flow

In Java we have goto as a reserved keyword, but the designers of java decided not to include it in the language. In general, goto statements are considered poor style of programming. But some programmers argue that goto is useful for an occasional jump out of a loop.

The Java designers gave it a nod and added a new statement to support this programming style, the labeled break. Let us first look at the unlabeled break statement. The break statement that you use to exit a switch can also be used to break out of a loop.

Example


Java Tutorialslass DemoUnlabeledBreak 
{
  public static void main (String args[]) 
    {
      int i = 0;
     do 
       {
        System.out.println(“demobreak”);
        i++;
        if (i > 50)
        break;
       }
    while (true);
    }
}

In above example, an infinite do-while loop is created by setting the loop condition to true. But here we incorporate the break statement to exit the loop when i is incremented past 50.

Java also offers a labeled break statement that lets you break out of multiple nested loops. Occasionally something weird happens inside a deeply nested loop. In that case, you may want to break completely out of all the nested loops. It is inconvenient to program that simply by adding extra conditions to the various loop tests.


Java Tutorialslass DemoLabelBreak 
{
  public static void main (String args[]) 
    {
      int i = Integer.parseInt(args[0]);
      labelbreak:    
     while(true) 
       {
       for(i=0;i<51;i++)        
         {
          System.out.println(“demobreak”);
          i++;
          if (i > 50)
          break labelbreak;
          }
       }
     System.out.println("Undesired value of  i");   
    }
}

In above program if the value of i is greater than 50 is inputted by user than control will come out of the loop and will pass to the very next statement after the loop.

Java Continue Statement

Another useful statement that works similarly to the break statement is the continue statement. Unlike break, the continue statement is only useful when working with loops and has no relation to the switch branch. The continue statement works like the break statement in that it jumps out of the current iteration of a loop.

The difference with continue is that program execution is restored to the test condition of the loop while break jumps completely out of a loop. Use break when you want to jump out and terminate a loop, and use continue when you want to jump immediately to the next iteration of the loop.


Java Tutorialslass Java Tutorialsontinued
{
public static void main(String args[])
{
int a=0;
for(int i=0;i<10;i++)
{
if(i>7)
Java Tutorialsontinue;
a=i;
System.out.println("The value of a is:"+a);
}
}
}

The output of the program will print values 0 to 7, after the value of i become greater than 7 next statement after continue will not be executed and its control will pass to the header of the loop i.e in case of for loop the control will pass to incrementing slot till the loop terminates.

Previous Home Next