Control Statements:
Selection Statements:
There are two selection statements in Java: if and switch:
a. if
Syntax : if (condition) statementsIfTrue
e.g. if (weight == 0) Sytem.out.println(“Error: Weight can’t be zero”);
if_else
Syntax: if (condition) statementsIfTrue; else statementsIfFalse;
e.g. if (password == mjs)
{ System.out.println(“Welcome”);
Login(); }
else { System.out.println(“Password not correct”); exitSystem(); }
if_else_if
Syntax: if (condition1) statement1 else if(condition2) statement2 else statement3
e.g. if (gender == female)
if (age > 25)
Sytem.out.println(“Woman”);
else
System.out.println(“Girl”);
else if (gender == male)
System.out.println(“Man”);
else
System.out.println(“Boy”);
b. switch
The expression must be of type byte, short, int, or char; each of the values specified in the case statements must be of a type compatible with the expression. Each case value must be a unique literal (that is, it must be a constant, not a variable). Duplicate case values are not allowed.
Syntax:
switch(expression)
{
case value1: statements; break;
case value2: statements; break;
case valuen: statements; break;
default:
default statements; }
Example:
switch(choice) {
case 'o': open(); break;
case 'c': close(); break;
case 'e': exit(); break;
default:
system.out.println(“not valid choice”);
}
Switch Example:
switch(intM)
{
case 1: //If M is 1
case 2: //If M is 2
case 3:
case 4:
System.out.println(“M is less than 5”);
break;
case 5:
System.out.println(“M is 5”);
}
Iteration Statements:
Jump Statements:
Java supports three jump statements:
1. break; break statements has three basic uses:
a. In switch statement to terminate case.
b. Used to exit from loops
e.g. for(int m=1; m<=10; m++) {
if(m == 5) break; /*break loop when m is 5*/ System.out.println(m); }
c. Used as civilized form of goto.
By using this form of break, you can break out of one or more blocks of code. These blocks need not be part of a loop or a switch. They can be any block. You can use a labeled break statement to exit from a set of nested locks. But you cannot use break to transfer control to a block of code that does not enclose the break statement.
Syntax : break label;
e.g. // Using break as a civilized form of goto.
class Break {
public static void main(String args[])
{
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
2. continue; continue statement has two uses:
a. In loops: In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression. For all three loops, any intermediate code is bypassed.
e.g. for(int m = 1; m<=5; m++)
{ if(m==3) continue; /*Skip printing of 3*/ System.out.println(m); }
b. As wment,ith the break state continue may specify a label to describe which enclosing loop to continue. Syntax: continue label;
3. Return
The return statement is used to explicitly return from a method. And also used to return any value or result to caller.
Post a Comment
Post a Comment
Please do not enter any spam link in the comment box