The Best Technical Blog Ever

twitterrss

Decision making statements

1. The if Statement:

If statement in java enables your Java programs to make decisions about what code to execute based on the condition. The code inside the if statement is executed only if a condition is true. It only takes a boolean expression( TRUE / FALSE) as its condition.


Syntax:
if (Boolean Expression) {
   	// Code block to get executed if the Boolean expression 
   	is true
}

Here, statements within the block will be executed only if the boolean-expression is true. If not, the first statement after the end of the if block will be executed. If there is only one statement in the block then the surrounding braces may be omitted.


Example:
if


2. The if...else Statement:

It has one if condition part and one else part. The statements within the if block will be executed only if the boolean-expression is true. If its false, then the else part code will be executed.


Syntax:
if(Boolean_expression){
    //Executes if the Boolean expression is true
}else{
    //Executes if the Boolean expression is false
}

Example:
ifelse


You can also use the return value of a method by passing a method as condition in an if statement.


ifelse


3. The if...else if... Statement:

It has multiple if conditions and an else part. It is used to test various conditions using single if...else if statement. If the if condition is true then the enclosed code will be executed. But if the if condition is false then it will check for the next else if condition. If the next if condition is true then the enclosed code will be executed otherwise the else part of the code will be executed.


Syntax:
if(Boolean_expression 1){
    //Executes if the Boolean expression 1 is true
}else if(Boolean_expression 2){
    //Executes if the Boolean expression 2 is true
}else {
    //Executes when the above conditions are false.
}

Example:
ifelseif


4. Nested if...else Statement:

In nested if else statement, one if or else if statement contains another if or else if statement.


Syntax:
if(Boolean_expression 1){
       //Executes if the Boolean expression 1 is true
      if(Boolean_expression 2){
   	     //Executes if the Boolean expression 2 is true
      }
}else{
      //Executes when the above conditions are false.
}

Example:
nestedif


5. The switch Statement:

  • The switch statement allows execution of different statements depending on the value of an expression. Here, the variable being switched on is checked for each and every case.
  • Switch statement can have any number of case statements. Each case is followed by the value to be compared to and a colon. The variable passed in a switch statement can only be a byte, short, int, or char.
  • When the value of the variable being switched on is equal to a case, the statements following that case will execute. When a break statement is reached, the switch terminates, and the flow control jumps to the next line following the switch statement. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
  • default case is optional, which must appear at the end of the switch. It is executed when none of the cases is true. No break is needed in the default case.

Syntax:
switch(expression){
    case value1 :
    	//Statements
    case value2 :
       //Statements
          .
          .
    default :
       //Statements
}

Example:
switch

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status