Java Statements

 In Java, There are basically two types of Statements- 

1. Conditional Statements.

2. Control flow (Looping or Iterations).


Conditional Statements- 

Conditional statement are also called block of statement. it returns two results true or false


if else - 

Ex- 

import java.util.Scanner;

public class Test

 {

public static void main(string args[]) 

  {

int a;

Scanner s1 = new Scanner(System.in);  

System.out.println("Enter the age");

a = s1.nextInt();

if(a>18)

     {// if true 

      System.out.println("you are eligible to vote");  

     }

else

     {//if false

System.out.println("you are not eligible to vote");

     }

  }

 }


else if -

' else if ' is used for multiple conditions.

Ex:-

import java.util.Scanner;

public class Test

 {

public static void main(string args[]) 

  {

int a;

Scanner s1 = new Scanner(System.in);  

System.out.println("Enter the marks");

a = s1.nextInt();

if(a>60)

{

System.out.println("First Division");

}

else if(a>50 && a<60) 

{

System.out.println("Second Division"); 

}

else if(a>40 && a<50) 

{

System.out.println("Third Division"); 

}

else 

{

System.out.println("Failed");

}

note:- In else if (&& operator) is used.

It is true when both conditions are true. 

and 

|| operator - it is true when only one condition is true .


switch case-   

It is basically used to overcome the 'else if' complexity problem

[So many conditions]

switch- It is communicated with the case block.

case- case is a part of a program. 

Ex- 

public class Test

 {

public static void main(string args[]) 

  {

int a;

Scanner s1 = new Scanner(System.in);  

System.out.println("Enter the choice 1,2,3 or 4");

a = s1.nextInt(); 

switch(a) 

{

case 1: 

             System.out.println("Addition");

            break;

case 2: 

           System.out.println("Subtraction"); 

           break;

case 3: 

               System.out.println("Multiplication");

          break;

case 4:

              System.out.println("Division");

             break;

default: 

         System.out.println("Invalid choice");

}

}

}


2. Control Flow Statements- 

Control flow is also called looping statements or iteration.

There are basically two types of Control flow statements-

1. Clockwise 

2. Anti-Clockwise 

Clockwise :-

'while' and 'do while' loop comes under this category, because they works in clockwise direction.

while-

while loop is also called 'entry loop' .

while loop is multi line loop.

Ex- 

int i=10; //initialisation

while(condition)

{

//Statements//

Increment/Decrement(i++ or i--)

}

do while- 

int i=10; //initialisation

do

{

//Statements//

Increment/Decrement(i++ or i--)

while(condition);


Anti- Clockwise- 

'for' loop comes under category, because it works in anti clockwise direction. 

Ex- 

for(initialisation ; condition ; increment/decrement)

{

for-

for loop is called single line loop.

(;) - the sign is called as statement separator.

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

{

//Statements//

}

 

      






Comments