0% found this document useful (0 votes)
16 views

Java Control Statements

The document discusses Java control flow statements. It describes three types of control flow statements in Java: decision making/selection statements, loop/iterative statements, and transfer/jumping statements. Decision making statements include if and switch statements, which evaluate boolean expressions to control program flow. Loop statements like do-while, while, for, and for-each loops allow repeating blocks of code. Transfer statements like break, continue, return, and try-catch-finally change the normal flow execution. The document provides examples and explanations of if and if-else statements in Java.

Uploaded by

Projects 2022
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java Control Statements

The document discusses Java control flow statements. It describes three types of control flow statements in Java: decision making/selection statements, loop/iterative statements, and transfer/jumping statements. Decision making statements include if and switch statements, which evaluate boolean expressions to control program flow. Loop statements like do-while, while, for, and for-each loops allow repeating blocks of code. Transfer statements like break, continue, return, and try-catch-finally change the normal flow execution. The document provides examples and explanations of if and if-else statements in Java.

Uploaded by

Projects 2022
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

KVR 9949939869

Java Control Statements

 Flow control describes the order in which all the statements will be executed at
runtime.
 Java compiler executes the code from top to bottom.
 The statements in the code are executed according to the order in which they appear.
 However, Java provides statements that can be used to control the flow of Java code.
Such statements are called control flow statements.
 Control flow statements, change, or break the flow of execution by implementing
decision making, looping, and branching your program to execute particular blocks of
code based on the conditions.

Java provides three types of control flow statements:

Control flow statements

Decision Making /Selection Loop / Iterative Transfer / Jumping


statements statements Statements

do while loop break


If Statements
continue
while loop
if…else Statement
return
for loop
Nested if statement
for-each loop try-catch-finally

Ladder if statement
assert

switch Statements

1
KVR 9949939869

Decision Making in Java:


 As the name suggests, decision-making statements decide which statement to execute
and when.
 Decision-making statements evaluate the Boolean expression and control the program
flow depending upon the result of the condition provided.
 There are two types of decision-making statements in Java, i.e., If statement and
switch statement.

1) If Statement:

 In Java, the "if" statement is used to evaluate a condition.


 The control of the program is diverted depending upon the specific condition.
 The condition of the If statement gives a Boolean value, either true or false.

In Java, there are four types of if-statements:


1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement

1. if statement in Java:

 It is used to decide whether a certain statement or block of statements will be


executed or not i.e. if a certain condition is true then a block of statement is executed
otherwise not.

Syntax of If Statement:

if(condition) {
    block of code // is executed if the condition is true
  }

2
KVR 9949939869

Working of if Statement:

Condition True: Condition False:

int age = 20; int age = 15;


if(Condition) { if(Condition){
//Code execute if Condition is True //code
} }
//code after if // Code execute if Condition is False

Flow Diagram:

Condition Condition False

Condition True

Code inside if body

Code inside if body

1. The argument / condition to the if statement should be Boolean by mistake if we are


providing any other value type we will get compile time error.

3
KVR 9949939869

Program:

public class Test {


    public static void main(String[] args) {
        int a = 0;
        if (a) {
            System.out.println("Code inside If executing...!");
        }
        System.out.println("Code after If executing...!");
    }
}
Output:

PS D:\JavaWork> javac Test.java

Test.java:5: error: incompatible types: int cannot be converted to boolean

if (a) {

Program:

public class Test {


    public static void main(String[] args) {
        int a = 10;
        if (a=20) {
            System.out.println("Code inside If executing...!");
        }
        System.out.println("Code after If executing...!");
    }
}
Output:

PS D:\JavaWork> javac Test.java

Test.java:4: error: incompatible types: int cannot be converted to boolean

if (a=20) {

4
KVR 9949939869

Program:

public class Test {


    public static void main(String[] args) {
        int a = 10;
        if (a==20) {
            System.out.println("Code inside If executing...!");
        }
        System.out.println("Code after If executing...!");
    }
}
Output:

PS D:\JavaWork> javac Test.java

PS D:\JavaWork> java Test

Code after If executing...!

Program:

public class Test {


    public static void main(String[] args) {
        boolean a = false;
        if (a=true) {
            System.out.println("Code inside If executing...!");
        }
        System.out.println("Code after If executing...!");
    }
}
Output:

PS D:\JavaWork> javac Test.java

PS D:\JavaWork> java Test

Code inside If executing...!

Code after If executing...!

5
KVR 9949939869

Program:

public class Test {


    public static void main(String[] args) {
        boolean a = false;
        if (a==true) {
            System.out.println("Code inside If executing...!");
        }
        System.out.println("Code after If executing...!");
    }
}
Output:

PS D:\JavaWork> javac Test.java

PS D:\JavaWork> java Test

Code after If executing...!

Program:

import java.util.Scanner;

public class IfExample {


    public void check() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Voter age...!");
        int age = sc.nextInt();
        if (age >= 18) {
            System.out.println("Eligible to Vote...!");
        }
        System.out.println("Code after if body...!!");  
    }
    public static void main(String args[]) {
        IfExample ex = new IfExample();
        ex.check();  
    }
}

Output:
PS D:\JavaWork> java IfExample
Enter Voter age...!
21

6
KVR 9949939869

Eligible to Vote...!
Code after if body...!!
PS D:\JavaWork> java IfExample
Enter Voter age...!
12
Code after if body...!!

if-else Statement:
 The Java if-else statement also tests the condition.
 It executes the if block if condition is true otherwise else block is
executed.

Syntax:

if(condition) {
    //Statement(s); //This block will execute only if the condition is true
 }
 else {
    Statement(s); //This block will execute if the condition is false
 }
 // Statement(s); // after if-else block will execute

Working of if Statement:

Condition True: Condition False:

int age = 20; int age = 15;


if(Condition) { if(Condition){
//Code execute if Condition is True //code
}else{ }else{
// Code // Code execute if Condition is False
} }
//code after if-else block //code after if-else block

7
KVR 9949939869

Flow Diagram:

Condition

If block code Else block code

After If-Else
block code

Program:
import java.util.Scanner;

public class IfExample {


    public void check() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Voter age...!");
        int age = sc.nextInt();
        if (age >= 18) {
            System.out.println("Eligible to Vote...!");
        }
        else{
            System.out.println("Not Eligible to Vote...!");
        }
        System.out.println("Code after if-elsebody...!!");  
    }
    public static void main(String args[]) {
        IfExample ex = new IfExample();
        ex.check();  
    }
}

8
KVR 9949939869

Output:
PS D:\JavaWork> javac IfExample.java

PS D:\JavaWork> java IfExample

Enter Voter age...!

17

Not Eligible to Vote...!

Code after if-elsebody...!!

PS D:\JavaWork> java IfExample

Enter Voter age...!

25

Both else part and curly braces are optional. Without curly braces we can take only one
statement under if, but it should not be declarative statement.

Semicolon(;) is a valid java statement which is call empty statement and it won't produce any
output.

Program:

public class Test{


    public static void main(String args[]){
    if(true);//valid
    }
}

Program:

public class Test{


    public static void main(String args[]){
        if (true)
            System.out.println("Without curly braces");//valid
    }
}

9
KVR 9949939869

Program:

public class Test{


    public static void main(String args[]){
    if(true)
        int a = 5;//invalid
    }
}
Output:

PS D:\JavaWork> javac Test.java

Test.java:4: error: variable declaration not allowed here

int a = 5;

Program:

public class Test{


    public static void main(String args[]){
        if (true) {
            int a = 5;//valid
        }
    }
}

Program:

public class Test{


    public static void main(String args[]){
        if (true)
            System.out.println("Without curly braces");
        System.out.println("This is after if body");//valid
    }
}
Program:

public class Test{


    public static void main(String args[]){
        if (true)
            System.out.println("Without curly braces");
        System.out.println("This is after if body");//valid
    }

10
KVR 9949939869

Program: Find Even or Odd

import java.util.Scanner;

public class Test{

    public void find(int num) {


        if (num % 2 == 0){
            System.out.println(num+" is a Even number");
        } else {
            System.out.println(num+" is a Odd number");  
        }
    }
    public static void main(String args[]){
        Test t = new Test();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number to find even or odd: ");
        int number = sc.nextInt();
        t.find(number);
    }
}
Output:

PS D:\JavaWork> javac Test.java


PS D:\JavaWork> java Test
Enter number to find even or odd:
7
7 is a Odd number
PS D:\JavaWork> java Test
Enter number to find even or odd:
8
8 is an Even number

11
KVR 9949939869

Program:

import java.util.Scanner;

public class Test{

    public void find(int num) {


        if (num > 0){
            System.out.println(num+" is a +ve number");
        } else {
            System.out.println(num+" is a -ve number");  
        }
    }
    public static void main(String args[]){
        Test t = new Test();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number to find +ve or -ve: ");
        int number = sc.nextInt();
        t.find(number);
    }
}
Output:
PS D:\JavaWork> javac Test.java
PS D:\JavaWork> java Test
Enter number to find +ve or -ve:
-9
-9 is a -ve number
PS D:\JavaWork> java Test
Enter number to find +ve or -ve:
7
7 is a +ve number

12
KVR 9949939869

Program: To find year is leap year or not

import java.util.Scanner;

public class Test{

    public void find(int year) {


        if (((year % 4 == 0) && (year % 100 != 0)) ||(year % 400 == 0)){
            System.out.println(year+" is a Leap year");
        } else {
            System.out.println(year+" is not a Leap year");  
        }
    }
    public static void main(String args[]){
        Test t = new Test();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a year to check Leap or Not: ");
        int year = sc.nextInt();
        t.find(year);
    }
}
Output:

PS D:\JavaWork> javac Test.java


PS D:\JavaWork> java Test
Enter a year to check Leap or Not:
1988
1988 is a Leap year
PS D:\JavaWork> java Test
Enter a year to check Leap or Not:
1900
1900 is not a Leap year
PS D:\JavaWork> java Test
Enter a year to check Leap or Not:
2000

13
KVR 9949939869

2000 is a Leap year

If-else-if ladder Statement:

 The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

if(condition1) {
    statement(s); /*This block will execute if condtion1 is true*/
 }
 else if(condition2) {
    statement(s); /* This block will execute if condition1 is not met and
condition2 is true */
 }
 else if(condition3) {
    statement(s);/* This block will execute if condition1 and condition2 is
not met and condition2 is true */
 }
 .
 .
 else {
    statement(s);// This block will execute if all of the above condition is
false.
 }

Flow Diagram:

14
KVR 9949939869

if...else...if ladder works

Program:

import java.util.Scanner;
public class Test{
    public void findGrade(int marks) {

        if (marks > 90){


            System.out.println("Excellent");
        }
        else if (marks > 80){
            System.out.println("Very Good");
        }

15
KVR 9949939869

        else if (marks > 70){


            System.out.println("Good");
        }
        else if (marks > 60){
            System.out.println("Average");
        }
        else if (marks > 50){
            System.out.println("poor");
        }
        // default statement
        else {
            System.out.println("Fail");
        }        
    }
    public static void main(String args[]){
        Test t = new Test();
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your marks to find grade: ");
        int marks = sc.nextInt();
        t.findGrade(marks);
    }
}

Output:

PS D:\JavaWork> javac Test.java


PS D:\JavaWork> java Test
Enter your marks to find grade: 99
Excellent
PS D:\JavaWork> java Test
Enter your marks to find grade: 88
Very Good
PS D:\JavaWork> java Test
Enter your marks to find grade: 75
Good
PS D:\JavaWork> java Test
Enter your marks to find grade: 64
Average
PS D:\JavaWork> java Test
Enter your marks to find grade: 56

16
KVR 9949939869

poor
PS D:\JavaWork> java Test
Enter your marks to find grade: 49
Fail

Program:
import java.util.Scanner;

public class Test{

    public void findSign(int num) {

        // checks if number is greater than 0


        if (num > 0) {
            System.out.println("The number is positive.");
        }
         
        // checks if number is less than 0
        else if (num < 0) {
            System.out.println("The number is negative.");
        }
             
        // if both condition is false
        else {
            System.out.println("The number is 0.");
        }
    }
    public static void main(String args[]){

17
KVR 9949939869

        Test t = new Test();


        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number to find sign: ");
        int number = sc.nextInt();
        t.findSign(number);
    }
}

Output:
PS D:\JavaWork> javac Test.java
PS D:\JavaWork> java Test
Enter number to find sign: -6
The number is negative.
PS D:\JavaWork> java Test
Enter number to find sign: 9
The number is positive.

Nested if statement

 The nested if statement represents the if block within another if block. Here, the inner
if block condition executes only when outer if block condition is true.

Syntax:

if(condition1) {
 }     StatementFirst(s);
    if(condition2) {
       StatementSecond(s);
    }

Program:

import java.util.Scanner;

public class Test{

    public void donate(int age, int weight) {


        // checks if number is greater than 0

18
KVR 9949939869

        if(age>=18){    
            if(weight>50){  
                System.out.println("You are eligible to donate blood");  
            }    
        }    
    }
    public static void main(String args[]){
        Test t = new Test();
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your age: ");
        int age = sc.nextInt();
        System.out.print("Enter your weight: ");
        int weight = sc.nextInt();
        t.donate(age, weight);
    }
}
Output:

PS D:\JavaWork> javac Test.java

PS D:\JavaWork> java Test

Enter your age: 29

Enter your weight: 55

You are eligible to donate blood

Program:

import java.util.Scanner;

public class Test{

    public void donate(int age, int weight) {    


    //applying condition on age and weight    
    if(age>=18){      
        if(weight>50){    
            System.out.println("You are eligible to donate blood");    
        } else{  
            System.out.println("You are not eligible to donate blood");    
        }  
    }else{  
      System.out.println("Age must be greater than 18");  
    }  
    }
    public static void main(String args[]){
        Test t = new Test();

19
KVR 9949939869

        Scanner sc = new Scanner(System.in);


        System.out.print("Enter your age: ");
        int age = sc.nextInt();
        System.out.print("Enter your weight: ");
        int weight = sc.nextInt();
        t.donate(age, weight);
    }
}
Output:

PS D:\JavaWork> javac Test.java

PS D:\JavaWork> java Test

Enter your age: 46

Enter your weight: 45

You are not eligible to donate blood

20

You might also like