CONTROL STATEMENTS

 

IF STATEMENT 

  The java if statement is used to test the condition. it checks boolean condition true or false. 

    There are various types of if statement in java 

  1. if statement 
  2. if-else statement 
  3. if else-if ladder
  4. nested if statement

1. IF STATEMENT


The java if statement tests the condition. if executes the if block it condition is true 


SYNTAX:


if(condition) {


}


EXAMPLE:


package mypackage;

 

public class Ifcondition {

 

         public static void main(String[] args) {


                 int ab;


                 a=20;

                 b=29;

 

                 if (a>18) {


                 System.out.println("Age is greater then 18"); 


            }


        }


}


OUTPUT:


Age is greater then 18



2. IF-ELSE STATEMENT


The java if-else statement also tests the condition. it executes the if block if condition is true other wise else block is executed 


SYNTAX:


if(Condition) { 


}else { 

EXAMPLE:

package myPackage

 

public class IfelseCondittion 

 

         public static void main(String[] args) { 

                  

                  String actual="ABCD"

                  String expected="ABCD123"

                  

                  boolean status=actual.equals(expected); 

                  

                  if(status) { 

                          System.out.println("THIS IS SUCCESS"); 

                  } else { 

                          System.out.println("THIS IS FAILURE"); 

                  } 

         } 

}


CONSOLE OUTPUT: 

THIS IS FAILURE  


IF BOTH THE CONDITIONS ARE EQUAL

package mypackage;

 

public class Ifelsecondition {

 

         public static void main(String[] args) {


                 int a , b;

                 a=5;

                 b=5;

 

                 if (a == b) {

                          System.out.println("pass");

                 } else {

                          System.out.println("fail");

                 }

         }

}

CONSOLE OUTPUT: 

PASS


IF ANY ONE CONDITION IS TRUE

package mypackage;

 

public class Ifelsecondition {

 

         public static void main(String[] args) {

                 int TeluguEnglish;

                 Telugu=50;

                 English=59;

 

                 if (Telugu > 35 || English > 35) {

                          System.out.println("pass");

                 } else {

                          System.out.println("fail");

                 }

         }

}

CONSOLE OUTPUT: 

PASS

IF BOTH THE CONDITIONS ARE TRUE

package mypackage;

 

public class Ifelsecondition {

 

         public static void main(String[] args) {


                 int TeluguEnglish;

                 Telugu=50;

                 English=29;

 

                 if (Telugu > 35 && English > 35) {

                          System.out.println("pass");

                 } else {

                          System.out.println("fail");

                 }

         }


OUTPUT: 

fail


USING TERNARY OPERATOR 

    We can also use ternary operator ( ?, : ) to perform the task of if - else condition it is a short hand way to check the condition if the condition is true the result of ? is returned but it the condition is false the result of : is returned.

package mypackage;

 

public class Ifelsecondition {

 

    public static void main(String[] args) {

        

        int number=13;


        String output=(number%2==0)?"Even Number":"Odd Number";    

        System.out.println(output);  

}

}


OUTPUT

Odd Number

LEAP YEAR 

    A year is a leap if it is divisible by 4 and 400. but not by 100,

package condition;

public class leapyear {

    public static void main (String [] args ) {

        int year = 2020;

        if ((year %4 == 0)) {
               System.out.printIn("LEAP YEAR");
        } else {
                System.out.printIn("COMMON YEAR");
        }
    }
}

3. IF-ELSE-IF LADDER STATEMENT

    The if else if ladder statement executes one condition from multiple statements

SYNTAX

if (condition) {

} else if (condition 2) {


4. NESTED IF STATEMENT

SWITCH STATEMENT

  • Java switch statement executes one statement from multiple conditions. It is like if-else-ladder statement.
  • The switch statement works with byte, short, int, long, enum types, string and some wrapper types.
  • Since java 7 you can use strings in the switch statement 

POINTS 

  1. There are one or N number of case values for a switch expression
  2. The case value must be of switch expression type only. 
  3. the values must be unique. 
  4. Each case statement can have a break statement which is optional. 
  5. The case value can have a default label which is optional 

syntax:

switch (expression) {

case value1:

    //statement

break; 

case value2: 

    //statement

break; 

case value3: 

    //statement

break; 

default: 

    //statement

}

FOR LOOP


TABLES 

package hellojava;

public class Myclass {

    public static void main(String[] args) {

        int a = 10;

        for (int i = 0; i < 10; i++) {

            System.out.println(a +" x " + (i+1) + " = " + (a*(i+1)));

        }

    }

}

OUTPUT:

10 x 1 = 10

10 x 2 = 20

10 x 3 = 30

10 x 4 = 40

10 x 5 = 50

10 x 6 = 60

10 x 7 = 70

10 x 8 = 80

10 x 9 = 90

10 x 10 = 100