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:
Related posts:
Comments
Post a Comment