Java Control statements

Java Control statements

Java Control statements in a programming language are very useful as they allow a programmer to change the flow of program execution i.e. altering the normal program flow to jump directly on some statement(s) or to skip a statement(s). In Java control statements are divided into following 3 categories:
Selection Statements
Loop Statements
Jump Statements
Selection Statements:
Java supports two selection statements they are if and switch. These statements allow you to control the flow of your program’s execution based upon conditions known only during run time.
If Statement:
This is a control statement to execute a single statement or a block of code, when the given condition is true and if it is false then it skips if block and rest code of program is executed.
Syntax:
  if (conditional_expression) {
  <statements>;
  ...;
  ...;
}
Example: If n%2 evaluates to 0 then the "if" block is executed. Here it evaluates to 0 so if block is executed. Hence "This is even number" is printed on the screen.
int n = 10;
if (n%2 = = 0){
System.out.println ("This is even number");
}
If-else Statement:
The "if-else" statement is an extension of if statement that provides another option when 'if' statement evaluates to "false" i.e. else block is executed if "if" statement is false. 
Syntax:
if (conditional_expression) {
<statements>;
...;
...;
}
else {
<statements>;
 ....;
 ....;
}
Example: If n%2 doesn't evaluate to 0 then else block is executed. Here n%2 evaluates to 1 that is not equal to 0 so else block is executed. So "This is not even number" is printed on the screen.
int n = 11;
if (n%2 = = 0){
System.out.println ("This is even number");
}
else {
System.out.println ("This is not even number");
}
Switch Statement:
This is an easier implementation to the if-else statements. The keyword "switch" is followed by an expression that should evaluates to byte, short, char or int primitive data types, only. In a switch block there can be one or more labelled cases. The expression that creates labels for the case must be unique. The switch expression is matched with each case label. Only the matched case is executed, if no case matches then the default statement (if present) is executed.
Syntax:
  switch (control_expression) {
  case expression 1:
  <statement>;
  case expression 2:
  <statement>;
   ...
   ...
  case expression n:
  <statement>;
  default:
  <statement>;
  }//end switch
Example: Here expression "day" in switch statement evaluates to 5 which matches with a case labelled "5" so code in case 5 is executed that results to output "Friday" on the screen.
int day = 5;
switch (day) {
case 1:
  System.out.println ("Monday");
  break;
  case 2:
  System.out.println ("Tuesday");
  break;
  case 3:
  System.out.println ("Wednesday");
  break;
  case 4:
  System.out.println ("Thursday");
  break;
  case 5:
  System.out.println ("Friday");
  break;
  case 6:
  System.out.println ("Saturday");
  break;
  case 7:
  System.out.println ("Sunday");
  break;
  default:
  System.out.println ("Invalid entry");
  break;
}
Loop/ Iteration Statements:
Java’s repetition (iteration) statements are for, while, and do-while. These statements create what we commonly call loops. A loop repeatedly executes the same set of instructions until a termination condition is met.

While Statement:
while statement continually executes a block of statements while a particular condition is true. Entry controlled
Syntax:
while (conditions)
{
//Loop body
}
Example: Here expression i<=10 is the condition which is checked before entering into the loop statements. When i is greater than value 10 control comes out of loop and next statement is executed. So here i contain value "1" which is less than number "10" so control goes inside of the loop and prints current value of i and increments value of i. Now again control comes back to the loop and condition is checked. This procedure continues until i become greater than value "10". So this loop prints values 1 to 10 on the screen.
int i = 1;
//print 1 to 10
while (i <= 10) {
System.out.println ("Num" + i);
i++;
}
Do-while Statement:
It will enter the loop without checking the condition first and checks the condition after the execution of the statements. That is it will execute the statement once and then it will evaluate the result according to the condition. Exit controlled
Syntax:
do
{
//Loop body
} while (condition);
Example: Here first do block of code is executed and current value "1" is printed then the condition i<=10 is checked. Here "1" is less than number "10" so the control comes back to do block. This process continues till value of i becomes greater than 10.
int i = 1;
do {
System.out.println ("Num: " + i);
i++;
} while (i <= 10);
For Statement:
The concept of Iteration has made our life much easier. Repetition of similar tasks is what Iteration is and that too without making any errors. Until now we have learnt how to use selection statements to perform repetition.
Syntax:
for (initialization; test condition; increment)
{
//Loop body
}
initialization: The loop is started with the value specified.
condition: It evaluates to either 'true' or 'false'. If it is false then the loop is terminated.
increment or decrement: After each iteration, value increments or decrements.
Example: Here num is initialized to value "1", condition is checked whether num<=10. If it is so then control goes into the loop and current value of num is printed. Now num is incremented and checked again whether num<=10.If it is so then again it enters into the loop. This process continues till num>10. It prints values 1 to10 on the screen.
for (int num = 1; num <= 10; num++) {
System.out.println ("Num: " + num);
}
Jump Statements:
Java supports three jump statements: break, continue, and return. These statements transfer control to another part of your program.
break.
continue.
return.
Break Statement:
Sometimes we use Jumping Statements in Java. Using for, while and do-while loops is not always the right idea to use because they are cumbersome to read. . Break statement skips the following code lets the execution jump to point after, where the program execution has jumped from to switch-case region.
Syntax:
for (initialization; test condition; increment)
{
if (condition)
{
//statements
break;
}
}
Example:
for (int i = 0; i &lt; 10, i++ ) {
if (i == 3) {
break;
}
System.out.println (“inside for loop”);
}// program jumps here after break
Continue Statement:
Continue statement is just similar to the break statement in the way that a break statement is used to pass program control immediately after the end of a loop and the continue statement is used to force program control back to the top of a loop.
Syntax:
for (initialization; test condition; increment)
{
if (condition)
{
//statements
continue;
}
}
Example:
for (int i = 0; i &lt;10; i++ ){
if (1==1 )
continue;
System.out.println (i);
}

Return Statement:
It is a special branching statement that transfers the control to the caller of the method. This statement is used to return a value to the caller method and terminates execution of method. This has two forms: one that returns a value and the other that cannot return. The returned value type must match the return type of method.
Syntax:
return;
return values;
return; //This returns nothing. So this can be used when method is declared with void return type.
return expression; //It returns the value evaluated from the expression.
Example:
class Return
{
public static void main (String args [])
{
boolean t = true;
System.out.println ("Before the return.");
if (t) return; // return to caller
System.out.println ("This won't execute.");
}
}

0 comments:

Post a Comment