CONTROL STATEMENTS

 


CONTROL STATEMENT 

A control statement determains whether the nest set of tasks have to be Executed or not

These are divided into three categories: 

  1. Selection Statements
  2. Iterative Statements
  3. Jump Statements

SELECTION STATEMENT 

Selection statements are classified into two types: 

  • if
  • Switch

 IF

  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"); 

                  } 

         } 

}

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");

                 }

         }

}

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");

                 }

         }

}

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


Pass

EVEN / ODD NUMBER

It is a program of odd and even numbers check it the number is divisible by 2 or not 

package mypackage;

 

public class Ifelsecondition {

 

         public static void main(String[] args) {


                 int number =50;

 

                 if (number % 2 == 0) {

                          System.out.println("Even");

                 } else {

                          System.out.println("Odd");

                 }

         }

OUTPUT


Odd

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");
        }
    }

}

OUTPUT


LEAP YEAR


3. IF-ELSE-IF LADDER STATEMENT

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

SYNTAX

 if (conditon) {

}else if (condition 2) {

}else if (condition 3) {

}else{

}


EXAMPLE:

package statements;


public class IfElseIf {

        public static void main (String[]args] {

                int marks = 90;
            
            if (marks >= 80 && marks <100 ) {
            system.out.println("A Grade");

           } else if (marks >= 60 && marks <80 ) {
            system.out.println("B Grade");

            { else if (marks >= 40 && marks <60 ) {
            system.out.println("C Grade");

            } else if (marks >= 35 && marks <40 ) {
            system.out.println("D Grade");

        }else{
            system.Out.Println("fail);
        }

      }

}

OUTPUT


A Grade

4. NESTED IF STATEMENT

    The nested if statement represent the if block within another if block

SYNTAX

 if (condition) {

if (condition) {

}

}


EXAMPLE:

package operators;


public class Arithmaticperators {

        public static void main (String[]args] {

                int age= 50;
                int weight = 80;

                if ( age > 40 ) {
                if (weight > 50) {

                System.out.printIn();"Eligible to donate blood");
            }

        }

}

OUTPUT


Eligible to donate blood

 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 value:

break;

case value:

break;

case value:

break;

default:

}


EXAMPLE:

package statements;


public class Switch {

        public static void main (String[]args] {

                int age = 20;
                
                switch (age) {
                case 10:
                    system.out.println("10");
                break;

                case 20:
                    system.out.println("20");
                break;    
           
                case 30:
                    system.out.println("30");
                break;   
             
                case 40:
                    system.out.println("40");
                break;     
           
                case 50:
                    system.out.println("50");
                break;
                
                default:
                System.out.printIn("invalid");

        }

}

OUTPUT


20

ITERATIVE STATEMENTS

Iterative statements are divided into four types they are

  1. While
  2. do-while
  3. for
  4. for-each

WHILE


 The java while is used to iterate a part pf the program several times if the no.of.iteration is not fixed it is recommended to use while loop

SYNTAX

while (condition){

}


EXAMPLE:

package controlstatements;


public class wholeLoop {

        public static void main (String[]args] {

                int = 1i; 
                while (i <=10) {
                    System.Out.Println(i); 
                    i++;
            }

        }

}

OUTPUT


1
2
3
4
5
6
7
8
9
10   

INFINITIVE WHILE LOOP

If you pass true in the while loop it will be infinitive do while loop.

 SYNTAX

while (true){


 DO-WHILE


DO WHILE loop is used to iterate a part of the program several times it is executed at least once because condition is checked after loop body.

SYNTAX

do{

}while (condition);

EXAMPLE

package controlstatements;


public class DoWhile {

        public static void main (String[]args] {

                int = 1i; 
                do{
                    System.Out.Println(i); 
                    i++;
                }while(i <=10);

        }

}

OUTPUT


1
2
3
4
5
6
7
8
9
10   

INFINITIVE WHILE LOOP

If you pass true in the while loop it will be infinitive do while loop.

 SYNTAX

do{
}while (true);

FOR

We can initialize the variable, check condition and increment/decrement value. It consists of four parts:

  1. Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition.
  2. Condition: It is the second condition which is executed each time to test the condition of the loop. It continues execution until the condition is false. It must return boolean value either true or false. It is an optional condition.
  3. Statement: The statement of the loop is executed each time until the second condition is false.
  4. Increment/Decrement: It increments or decrements the variable value. It is an optional condition.
SYNTAX

for(initialization; condition; inc/dec) {



EXAMPLE: Increment

package controlstatements;


public class ForLoop{

        public static void main (String[]args] {

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

System.out.println(i);


}


        }

}

OUTPUT


1
2
3
4
5
6
7
8
9
10   

EXAMPLE: Decrement

package controlstatement;


public class forLoop{

        public static void main (String[]args] {

for (int i = 10; i >= 1; i--) {

System.out.println(i);


}


        }

}

OUTPUT


10
9
8
7
6
5
4
3
2
1  

PRINT EVEN NUMBER 1 TO 100

package controlstatements;


public class EvenNumbers{

        public static void main (String[]args] {

       int num = 100;

for (int i = 2; i <= num; i=i+2) {

System.out.println(i);


}


        }

}

OUTPUT


2
4
6
8
10
.
.
.
.
98
100   

PRINT EVEN NUMBER 1 TO 100

package controlstatements;


public class EvenNumbers{

        public static void main (String[]args] {

       int num = 100;

for (int i = 1; i <= num; i=i+2) {

System.out.println(i);


}


        }

}

OUTPUT


1
3
5
7
9
.
.
.
.
97
99

Java Nested For Loop: 

If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.

EXAMPLE

package conditionalstatements;


public class NestedForLoop {

        public static void main (String[]args] {

. 

for (int i = 1; i <= 3; i++) {

            
    for (int j = 1; j <= 3; j++) {


System.out.println(i + " " + j);


}


        }

}

OUTPUT

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

PYRAMID EXAMPLE

package conditionalstatements;


public class Pyramid {

        public static void main (String[]args] {

. 

for (int i = 1; i <= 5; i++) {

            
    for (int j = 1; j <= i; j++) {


System.out.println(" * ");


}

            System.out.Println();

        }

}

OUTPUT

*
**
***
****
*****


FOR-EACH


The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.

       It works on elements basis does not index. It returns element one by one in the defined variable.

SYNTAX

while(type variableName : arrayName) { 



EXAMPLE

package conditionalstatements;


public class ForEach{

        public static void main (String[]args] {

      String[] cars = {"Volvo", "BMW", "Ford", "Honda"};

            for (String i : cars) {

              System.out.println(i);

            } 

. 

        }

}

OUTPUT

Volvo
BMW
Ford
Honda

TABLES 

package conditionalstatements;


public class Tables{

        public static void main (String[]args] {




            } 

. 

        }

}

OUTPUT

Volvo
BMW
Ford
Honda

JUMP STATEMENTS


BREAK


CONTINUE



RETURN