Catch us on

Thursday, March 31, 2016

how to make decisions

Hello friends you should be aware of decision making statements in java i.e. steps to be taken or statements to be executed by the java compiler if any unwanted condition arises. Before reading further you should have good knowledge of operators in java.
Ø  if-statement: if statement is given with a Boolean expression in the pair of parentheses and the statement hgiven between the pair of curly braces is executed only if the condition is true.
Syntax: if(condition) {
  // Statements to be executed
}
Example: int x=10;
                  If(x==10) //checking if x is equal to 10
                        {
                      System.out.println(“the value of x is”+x);                 /*this statement will be executed only if the condition x=10 is true*/
                         }
*NOTE: The condition given inside parenthesis is false then the whole statement given inside ifblock is skipped and the next statement is executed.
Ø  if-else statement: The if block of this statement is executed as same as if-statement but if the condition of if block is false then else statement if executed.
Syntax: if(condition) {
  // Statements to be executed
}
else {
//Statements to be executed if the condition if-block is false.
}
Example: int x=10;
                  If(x==9) //checking if x is equal to 10
                        {
                      System.out.println(“If statement is executed”);                 /*this statement will be executed only if the condition x=9 is true*/
                         }
                 else {
                         System.out.println(“Else statement is executed”);
                         }
Ø  Switch statement: in switch statement a variable is passed to a switch block and that variable is tested for each case( present in the switch block). That statement is executed whose case matches with the variable.
Syntax: switch(variable1) {
              case ‘a’: //statement one
                               break;
              case ‘b’: //statement two
                               break;
      default:         //this statement will be executed if none of the case variable matches with the variable1.
  }
NOTE: the default case does not have any break statement because it is the last statement which is executed by the switch block.
Example: int x=10;
 switch(x) {
                 case 5: //statement one
                               break;
                case 10: //statement one –this statement will run because x=10
                               break;
                default://default statement
                }
Ø  Nested if-else: in nested if else as the name suggests one if statement has many if-else statement inside its block.
Ø  else-if ladder: in this statement an else block has so many if-else statement inside it.
*NOTE: ANY IF STATEMENT MAY OR MAY NOT HAVE AN ELSE STATEMENT BUT EVERY ELSE STATEMENT HAS A IF STATEMENT.

THANK YOU FOR READING THIS ARTICLE.
PLEASE SHARE IT ON FACEBOOK,TWITTER AND OTHER SOCIAL NETWORKS AND RECOMMEND YOUR FRIENDS TOO.
Kishan Agarwal



Please Share


No comments:

Post a Comment