Java if,if/else.Ternary and switch statements - Neuroon Networks

Breaking

Tuesday, February 27, 2018

Java if,if/else.Ternary and switch statements

        Hello guys! I think you guys followed the previous tutorials. Today we are going to learn about some interesting part in the Java. It is if else statement , Ternary Statement and the switch statement. For that we need the knowledge of the relation operations as well as the boolean operators. so first of all lets learn about the boolean operators.


  1. and operator
    • The and operator is represented in Java by &&.
    • It returns the boolean value true when expressions on both sides of && operator is true.
    • For examples just look at the code given below with the output.                                                                                                                                                            

  2.  or operator.||
    • The or operator is represented in Java by ||.
    •  It returns a Boolean value of true when at least one expression on either side of || is true.
    •  For examples just look at the code given below with the output. 


       
  3.  not operator
    • The not operator is represented in Java by !
    • It will return the opposite of the expression immediately after it. It will return false if the expression is true, and true if the expression is false.
    •  For examples just look at the code given below with the output. 


               just like numerical operators (BODMAS) , Boolean operators follow rules that specify the order in which they are evaluated. The precedence of each Boolean operator is as follows:
      • First - ! operator.
      • Second - && operator.
      •  Third - || operator.
Look at the example given below.


If Statement

               Just look at the statement given below.

    if(a>b){
         System.out.println("a");
   }

      In the example, a>b is the boolean expression that get checked.So in here if the statement true it means if a is greater than b the code will print a. if not code will do nothing. Thats what the if statement does.



If Else Statement
          Just look at the statement given below.

if (a < b) {

            System.out.println("a");
} else {

           System.out.println("b");
}

   

       In here its same like the if statement if the conditions in side the if statements returns the value true it will execute the code inside the if scope and if the condition in side the if statement returns false it will execute the code inside the else scope.

If Elseif Statement

         If the Boolean expression after the if statement evaluates to true it will run the code block that directly follows.Otherwise, if the Boolean expression after the else if statement evaluates to true, the code block that directly follow will run.Finally, if all previous Boolean expressions evaluate to false, the code within the else block will run.


if (a > b) {

            System.out.println("a");
 } else if (b>c) {

            System.out.println("c");

 }    else {

            System.out.println("b");

 }  

Given below is a Simple example from if/if else/if elseif statements.




Ternary Statements.

    if/else statements are some what long when you want just return the value using the Boolean expression. Fortunately, Java provides a shortcut that allows you to write the if else statement in  simple line code. Ternary statement consist with three parts. the are
  • A boolean expression.
  • A single statement that gets executed if the Boolean expression is true.
  • A single statement that gets executed if the Boolean expression is false.
given below is a Example of the ternary statement.


      Look at the output. The boolean expression a>5 is true so it returns he value 'W' and it assigned to the variable b. thats why the output gives the W. This is not only of char this can be done for the all the primitive data types and non primitive data types like string. Try them by yourselves.

Switch Statements.


      A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.


        In the above example we assign the a in to the in variable with value 3. In first case it checks whether a=1 if a =1 it will print a=1 but here its not. So in here the vale of a is 3 and in the case 3 it checks whether the value of a is 3 thats why the out put is 3.
       Default case is execute when the value is not equals to the any compare value in the cases. break key word is use to exit from the switch cases.

See you guys in Next tutorial.

No comments:

Post a Comment