Skip to main content

Switch, break, continue, return statements

In previous post we discussed about control statements up to for loop. In this post we will come to know the remaining control statements like switch, break, continue, return statements with respective examples. If you don't have any idea about control statements in java, better follow this link to know about the Control Statement.

Switch statement

        When there are several options and we have to choose only one option from the available ones, we can use switch statement. Depending on the selected option, a particular task can be performed. A task represents one or more statements.
Syntax:
                  switch(variable){

                          case value-1 : statement-1;

                          case value-2 : statement-2;

                          case value-3 : statement-3;

                          .

                          .

                          case value-n : statement-n;

                          default : default_statement;

                 }
        Here, depending on the value of the variable , a particular task(statement) will be executed. If the variable value is equal to value-1, statement-1 will be executed. If the variable value is value2, statement2 will be executed, and so on. If variable value is not equal to any of the case, then none of the statements will be executed.
        In this case default cause is executed and hence the default_statement are executed.
Ex: write a program to come out of switch block, after executing a task?
Note: remember we can not use all the data types along with the switch statement. We can use char, int, byte, short only. For example, the following switch statement is invalid.
         String str = “Nalla”;
     Switch(str) //invalid, string can not be used with switch.
        Switch statements are mostly used in menu driven programs. A Menu driven program is a program where a menu is displayed and the user and the user can select an item from the list of items available in the menu. Depending on the choice of the user, a particular task is done.

break statement

break statement can be used on 3 ways.
1. break statement is used inside a loop to come out of it.
2. break statement is used inside the switch block to come out of the switch block.
3. break statement is can be used in nested blocks to go to the end of a block. Nested block represents a block written with in another block.
        We have already observed first two uses of break statement. Now let us see the third use, where break statement is used inside nested blocks.
         break label; // here break represents the name of the block
        the meaning of preceding statement is go to the end of the block, whose name is given by the label.
Ex: write a program to use break statement to go end of the block?
        In the preceding program, bl1,bl2,bl3 are names of the blocks starting with a { and ending with a }. First block3 will be displayed as the control of the execution enters the block. Then it executes
                     if(x) break bl2;
Why goto statements are not available in java?
A: goto statement lead to confusion for a programmer. Especially, in a large program, if several goto statements are used, the programmer would be perplexed while understanding the flow from where to where the control is jumping.

Continue statement

        Continue is used inside a loop to repeat the next iteration of the loop. when the continue is executed subsequent in the loop are not executed and control of execution goes back to the next repetition of the loop.
Syntax:
           continue;
        To understand the use of continue, let us write a program using a for loop to display the numbers in descending order from 10 to 1.
Ex: write a program using for loop to display the numbers in descending order?
        In this program, I value starts at 10. As long as I value is greater or equals to 1, it is decremented by 1. So, we get numbers from 10 to 1 in descending order. Now, lets introduce continue in this program.
        Here, we are redirecting the flow of execution back to the next iteration of the loop when i>5. So, when I value changes from 10 to 6 continue statement will executed and hence the subsequent statement.
            System.out.println(i + " ");
        Will not be executed. So the value of I from 10 to 6 will not be displayed. Continue statement can be used along with a label, like break statement as,
               Continue label; //here label represents name of the loop.

Return statement

        Return statement is used to return some value from a method. Return statement can be used in a method to come out of it to the calling method. For example, we are calling a method by the name method() from the main() method. If return is used inside method(), then the flow of execution comes out of it and goes back to main(). While going back to method(), we can return some value to main() method. For this purpose return should be used as follows,
return 1; //return 1 to calling method
return x; //return x value
return (x+y); //calculate x+y and return that value
return -6; //return -6
Ex: write a program to return some value from a method?
        If we use return statement inside the main() method, then the entire program will be terminated and we come out to the system prompt. Let us try to understand it is better with the help of example,
Ex: write a program to demonstrate that the return statement in main() method terminate the application?
What is the difference return and System.exit(0)?
A: return statement is used inside the method to come out of it, system.exit(0) is used in any method to come out of the program.
What is the difference between System.exit(0) and System.exit(1)?
A: System.exit(0) terminates the program normally. Where as System.exit(1) terminates the program because of some error encountered in the program.  

Related posts:

Comments

Popular posts from this blog

JNDI configuration for Tomcat 9 with Oracle

In this article, I am going to place the required source code to get data from the table by using the JNDI configuration. Below are the environment details that I have configured currently. Windows - 7 Oracle - 10g Tomcat - 9 JDK - 8 Eclipse Oxygen Ojdbc6 jar required First, we need to create the Dynamic Web Project. If you don't know how to do <Click Here>. I have faced a lot of issues before getting the output like 405, No driver class to load, etc. I am using JSP & Servlets in the current explanation. Before started writing the application logic, we need to do the below configuration in the installed tomcat directory. Place OJDBC6.jar in the Tomcat LIB directory. Add this Resource under <GlobalNamingResources> in Server.xml file which is present under the conf directory in Tomcat. < Resource name = "jdbc/myoracle" global= "jdbc/myoracle" auth = "Container" type= "javax.sql.DataSource" driverClass...

Prime, Fibonacci and Factorial number with example in java

Prime number, Fibonacci series and Factorial number programs are most commonly asked questions in interview. Read this article to know what is and how to write programs for prime number, fibonacci series and factorial number. Prime Number: prime number is natural number greater than 1 that has no positive divisor other than 1 and itself. A natural number greater than 1 is not a prime number, is called Composite number . For example, 7 is a prime number. Because it can divide with 1 and 7 only. Where as 8 is composite number. Since it has the divisor 2 and 4 in addition to the 1 and 8. The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false. package com . javatbrains . practice ; public class PrimeNumber { public boolean isPrimeNumber ( int number ) { if ( number <= 1 ) return false ; // There's only one ...

JVM, JRE and JDK in Java

JVM, JRE and JDK are the most basic common concepts to know in java. These are the basic features to understand how Java architecture works? JVM stands for Java Virtual Machine, which doesn't have any physical directories created in java installation. JRE stands for Java Runtime Environment, which creates the directory under Java installation path and also present in JDK. JDK stands for Java Development Kit, which creates the directory in Java installation path and also it has it's own JRE. Since we have already learn that Java is platform independent means if we have implemented any of the java class in one environment, it will be executed in any other environment and provides the same output. But, JVM, JRE and JDK all are platform dependent . So that, for windows, linux, unix, mac, solaris..etc has it's own JVM, JRE and JDK. One will be not compatible with other environments. While installing the Java, we might come to know a bit about JRE and JDK. But, JVM is the other...