Wednesday, January 11, 2012

The switch statement in JAVA

A switch statement provide you the option to test for a range of values for your variables. Switch statement helps simplifying multiple choices in a program

Syntax of Switch statement

switch(expression)

{

case value-1:Statement 1

break;

case value-2:Statement 2

break;

.........

.........

default: default Statement 3

break;

}

statement-x;

switch

The switch keyword is followed by a parenthesized integer expression. The switch statement executes the case corresponding to the value of the expression. Normally the code in a case clause ends with a break statement, which exits the switch statement and continues with the statement following the switch. If there is no corresponding case value, the default clause is executed. If no case matched and there is no default clause, execution continues after the end of the switch statement.

case

The case keyword is followed by an integer constant and a colon. This begins the statements that are executed when the switch expression has that case value.

default
If no case value matches the switch expression value, execution continues at the default clause.

break

The break statement causes execution to exit to the statement after the end of the switch. If there is no break, execution flows into the next case.

For more information visit http://www.programming-training.com/decision.html

Wednesday, January 4, 2012

If Statement in JAVA



If else statement is used to execute some code only if a specified condition is true.If condition is true then the statement will be execute 

Syntax of  If Statement in JAVA

if (condition)
  {
  code to be executed if condition is true
  }

Note : if is written in only lowercase letters. If we use uppercase letter like (IF) it will give error 


Example of if statement

public class IfStatement {

 public static void main(String[] args) {
  int a = 20, b = 30;
  if (a > b)
   System.out.println("a > b");
  if (a < b)
   System.out.println("b > a");
 }
}


Output of This Example

b > a