Paramclasses Academy - Blog

Tutorials Details

←
Post 6 of 40
→

What is Java Control Statements for Android: If-Else, Switch, Loops and Conditions ?

What is Java Control Statements for Android: If-Else, Switch, Loops and Conditions ?

November 2, 2026

1. What Are Control Statements in Java?

Control statements determine the order in which program instructions execute.

Normally, Java executes code from top to bottom. But sometimes, we need the program to make decisions or repeat certain instructions.

For example:

  • If a user enters the correct password, allow login.

  • If marks are above 40, display "Pass".

  • If a user clicks a button, perform a specific action.

  • Repeat an operation until a condition becomes false.

Java provides three main types of control statements:

  1. Decision-Making Statements: if, if-else, else-if, switch

  2. Looping Statements: for, while, do-while

  3. Jump Statements: break, continue

Let's understand each one with practical examples.

2. The if Statement

The if statement executes a block of code only when a given condition is true.

Syntax:

if (condition) {
    // Code executes if condition is true
}

Example:

public class Main {
    public static void main(String[] args) {

        int age = 20;

        if (age >= 18) {
            System.out.println("You are eligible to vote.");
        }
    }
}

Output:

You are eligible to vote.

Explanation:

  • int age = 20; stores the user's age.

  • age >= 18 checks whether the age is 18 or greater.

  • If the condition is true, Java executes the code inside the if block.

  • If the condition is false, the block is skipped.

3. The if-else Statement

The if-else statement is used when we want to execute one block if a condition is true and another block if it is false.

Syntax:

if (condition) {
    // Executes if condition is true
} else {
    // Executes if condition is false
}

Example: Check Pass or Fail

public class Main {
    public static void main(String[] args) {

        int marks = 35;

        if (marks >= 40) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }
    }
}

Output:

Fail

Explanation:

The condition marks >= 40 checks whether the student has scored at least 40 marks.

  • If marks are 40 or more, the program displays Pass.

  • Otherwise, it displays Fail.

This type of condition is useful in Android apps when checking login details, form values, or user eligibility.

4. The else-if Ladder

The else-if ladder is used when we need to check multiple conditions.

Example: Grade Calculator

public class Main {
    public static void main(String[] args) {

        int marks = 85;

        if (marks >= 90) {
            System.out.println("Grade A+");
        } else if (marks >= 80) {
            System.out.println("Grade A");
        } else if (marks >= 70) {
            System.out.println("Grade B");
        } else if (marks >= 40) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }
    }
}

Output:

Grade A

Explanation:

Java checks the conditions from top to bottom.

  • First, it checks whether marks are 90 or more.

  • If false, it checks whether marks are 80 or more.

  • The condition marks >= 80 is true, so it prints Grade A.

  • The remaining conditions are skipped.

Important: Always arrange conditions carefully. Java executes the first matching condition and skips the remaining branches.

5. Nested if Statement

A nested if means writing one if statement inside another if statement.

It is useful when a second condition should be checked only after the first condition is true.

Example: Login Validation

public class Main {
    public static void main(String[] args) {

        String username = "admin";
        String password = "12345";

        if (username.equals("admin")) {

            if (password.equals("12345")) {
                System.out.println("Login Successful");
            } else {
                System.out.println("Incorrect Password");
            }

        } else {
            System.out.println("Invalid Username");
        }
    }
}

Output:

Login Successful

Explanation:

  • The first if checks the username.

  • If the username is correct, the second if checks the password.

  • If both are correct, the login is successful.

  • Otherwise, the program displays the appropriate error message.

Note: Use .equals() to compare Java String values. The == operator generally checks whether two references point to the same object, not whether their text is equal.

6. The switch Statement

The switch statement is useful when we need to select one option from multiple fixed choices.

Syntax:

switch (variable) {

    case value1:
        // Code
        break;

    case value2:
        // Code
        break;

    default:
        // Default code
}

Example: Day Selection

public class Main {
    public static void main(String[] args) {

        int day = 3;

        switch (day) {

            case 1:
                System.out.println("Monday");
                break;

            case 2:
                System.out.println("Tuesday");
                break;

            case 3:
                System.out.println("Wednesday");
                break;

            case 4:
                System.out.println("Thursday");
                break;

            default:
                System.out.println("Invalid Day");
        }
    }
}

Output:

Wednesday

Explanation:

  • switch(day) checks the value stored in day.

  • case 3 matches the value 3.

  • Java prints Wednesday.

  • break exits the switch block.

  • default executes when no case matches.

When should you use if-else or switch?

  • Use if-else when checking ranges or complex conditions.

  • Use switch when comparing one expression against several fixed values.

7. Looping Statements in Java

Loops allow us to execute the same block of code multiple times.

For example, if we want to print numbers from 1 to 10, we do not need to write ten separate print statements. We can use a loop.

Java provides three commonly used loops:

  • for loop

  • while loop

  • do-while loop

8. The for Loop

The for loop is useful when we know how many times we want to repeat an operation.

Syntax:

for (initialization; condition; update) {
    // Code to repeat
}

Example: Print Numbers from 1 to 5

public class Main {
    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {
            System.out.println(i);
        }
    }
}

Output:

1
2
3
4
5

Explanation:

  • int i = 1 initializes the counter.

  • i <= 5 checks whether the loop should continue.

  • i++ increases the counter by 1 after each iteration.

  • When i becomes 6, the condition becomes false and the loop stops.

Here, one complete execution of the loop is called an iteration.

9. The while Loop

The while loop repeats a block of code as long as its condition remains true.

It is useful when the number of repetitions is not known in advance.

Syntax:

while (condition) {
    // Code to repeat
}

Example:

public class Main {
    public static void main(String[] args) {

        int i = 1;

        while (i <= 5) {
            System.out.println(i);
            i++;
        }
    }
}

Output:

1
2
3
4
5

Explanation:

  • The variable i starts at 1.

  • Java checks whether i <= 5.

  • If true, it prints the value.

  • i++ increases the value.

  • The loop stops when the condition becomes false.

Important: Always update the variable used in the condition when necessary. Otherwise, you may accidentally create an infinite loop.

10. The do-while Loop

The do-while loop executes its code at least once, even if the condition is false.

Syntax:

do {
    // Code executes first
} while (condition);

Example:

public class Main {
    public static void main(String[] args) {

        int i = 1;

        do {
            System.out.println(i);
            i++;
        } while (i <= 5);
    }
}

Output:

1
2
3
4
5

Explanation:

  • Java executes the do block first.

  • After executing the block, it checks the condition.

  • If the condition is true, the loop runs again.

  • If false, the loop stops.

Difference between while and do-while:

  • while checks the condition before executing the code.

  • do-while executes the code first and checks the condition afterward.

11. The break Statement

The break statement immediately terminates a loop or switch statement.

Example:

public class Main {
    public static void main(String[] args) {

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

            if (i == 5) {
                break;
            }

            System.out.println(i);
        }
    }
}

Output:

1
2
3
4

Explanation:

When i becomes 5, the condition i == 5 becomes true. The break statement stops the loop immediately.

12. The continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop.

Example: Skip Number 3

public class Main {
    public static void main(String[] args) {

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

            if (i == 3) {
                continue;
            }

            System.out.println(i);
        }
    }
}

Output:

1
2
4
5

Explanation:

When i becomes 3, the continue statement skips the print statement for that iteration. The loop then continues with the next value.

13. Using Control Statements in Android Development

Control statements are used in almost every Android application.

For example, imagine a login screen with two EditText fields and one Button.

When the user clicks the Login button, the app checks whether the fields are empty.

Android Java Example:

EditText username = findViewById(R.id.etUsername);
EditText password = findViewById(R.id.etPassword);
Button loginButton = findViewById(R.id.btnLogin);

loginButton.setOnClickListener(v -> {

    String user = username.getText().toString().trim();
    String pass = password.getText().toString().trim();

    if (user.isEmpty() || pass.isEmpty()) {

        Toast.makeText(
            this,
            "Please enter username and password",
            Toast.LENGTH_SHORT
        ).show();

    } else {

        Toast.makeText(
            this,
            "Form submitted successfully",
            Toast.LENGTH_SHORT
        ).show();
    }
});

Explanation:

  • setOnClickListener() runs the code when the user clicks the button.

  • getText().toString() reads the text entered by the user.

  • trim() removes extra spaces from the beginning and end.

  • isEmpty() checks whether the field is empty.

  • The if condition checks whether either field is empty.

  • If a field is empty, the app displays a warning.

  • Otherwise, it displays a success message.

This example demonstrates how Java control statements help Android apps respond to user input.

Note: This is a UI validation example, not a complete authentication system. Real login systems should verify credentials securely on a backend.

14. Common Mistakes Beginners Should Avoid

1. Using = instead of ==

int number = 10;

if (number == 10) {
    System.out.println("Correct");
}

Use = for assignment and == for comparing primitive values.

2. Forgetting break in switch

Without break, execution may continue into the following cases. This is called fall-through.

3. Creating an infinite loop

int i = 1;

while (i <= 5) {
    System.out.println(i);
    i++;
}

Make sure the loop condition can eventually become false.

4. Writing incorrect conditions

Always check whether you need >, <, >=, <=, ==, or !=.

5. Comparing Strings using ==

Use .equals() when comparing String content.

15. Practice Programs

Try writing these programs yourself in Android Studio or any Java compiler.

  1. Write a program to check whether a number is positive, negative, or zero.

  2. Write a program to check whether a number is even or odd.

  3. Write a program to find the largest of three numbers.

  4. Write a program to display grades using an else-if ladder.

  5. Write a program to print numbers from 1 to 20 using a for loop.

  6. Write a program to print the multiplication table of a number.

  7. Write a program to calculate the sum of numbers from 1 to 10.

  8. Write a program to print only even numbers from 1 to 20.

  9. Write a program to create a simple calculator using switch.

  10. Write a program to check login credentials using if-else.

Try solving these programs without looking at the examples first. This will help you understand how conditions and loops work.

16. Conclusion

Java control statements help developers control how a program executes. With if-else and switch, we can make decisions. With for, while, and do-while, we can repeat operations. With break and continue, we can control loop execution.

These concepts are important for Android development because apps constantly make decisions based on user input, button clicks, and application conditions.

Practice each example and try changing the values to understand how the output changes.

17. Frequently Asked Questions (FAQs)

Q1. What are control statements in Java?

Control statements control the execution flow of a Java program. They include decision-making, looping, and jump statements.

Q2. What is the difference between if and if-else?

The if statement executes code only when a condition is true. The if-else statement provides an alternative block when the condition is false.

Q3. What is the difference between switch and if-else?

switch is useful for selecting between fixed values, while if-else is better for ranges and complex conditions.

Q4. What is the difference between for and while loops?

A for loop is commonly used when the number of iterations is known. A while loop is useful when repetition depends on a condition.

Q5. What is the difference between while and do-while?

A while loop checks its condition before execution. A do-while loop executes at least once before checking the condition.

Q6. What is the use of break in Java?

The break statement immediately terminates the current loop or switch statement.

Q7. What is the use of continue in Java?

The continue statement skips the current loop iteration and moves to the next one.

Q8. Are control statements important for Android development?

Yes. They are used for form validation, login checks, button actions, menu selection, and many other application operations.

18. SEO Details

SEO Title: Java Control Statements for Android: If-Else, Switch and Loops

Meta Description: Learn Java control statements for Android development, including if-else, switch, for loop, while loop, do-while, break and continue with practical examples.

Focus Keyword: Java Control Statements for Android

Related Keywords:

  • Java if else statement

  • Java switch statement

  • Java loops tutorial

  • For loop in Java

  • While loop in Java

  • Do while loop in Java

  • Java programming for Android

  • Android Studio Java tutorial

Suggested URL Slug: java-control-statements-android-if-else-switch-loops

←
Post 6 of 40
→