Table of Contents

    Statements

    Programming Fundamentals

    Statements

    Learn what statements are in programming, how they work, different types of statements, and how statements control the execution of a program.

    What is a Statement in Programming?

    A statement is a complete instruction that tells the computer to perform a specific action. In simple words, a statement is one command written in a program.

    A program is made up of many statements. Each statement performs one small task such as declaring a variable, assigning a value, calculating a result, displaying output, checking a condition, or repeating a block of code.

    A statement is an instruction that the computer executes while running a program.

    In Java, most simple statements end with a semicolon ;. The semicolon tells Java that the instruction is complete.

    Easy Real-Life Example

    Statements as Instructions

    Imagine your teacher gives instructions: open your notebook, write your name, solve the first question, and submit your work. Each instruction is a separate statement. Similarly, in programming, each statement tells the computer what to do.

    Just like humans follow instructions one by one, a computer follows programming statements one by one.

    Why are Statements Important?

    Statements are important because they are the basic building blocks of a program. Without statements, a program cannot perform any action.

    Importance of Statements

    • Statements tell the computer what action to perform.
    • They help store and update data.
    • They perform calculations and operations.
    • They display output to the user.
    • They help make decisions using conditions.
    • They help repeat tasks using loops.
    • They control the flow of program execution.
    • They make programs meaningful and functional.

    Simple Statement Example

    The following Java statement displays a message on the screen.

    System.out.println("Hello, World!");

    This is a statement because it gives one complete instruction to the computer: display the text Hello, World!.

    How Statements Execute

    Normally, statements are executed from top to bottom in the order they appear in the program.

    System.out.println("Step 1");
    System.out.println("Step 2");
    System.out.println("Step 3");

    Output

    Step 1
    Step 2
    Step 3

    The computer first executes the first statement, then the second statement, and then the third statement.

    Types of Statements in Programming

    Programming statements can be divided into different types based on what they do.

    1

    Declaration Statement

    Used to declare a variable.

    A declaration statement creates a variable and tells the program what type of data it can store.

    2

    Assignment Statement

    Used to assign a value to a variable.

    An assignment statement stores a value inside a variable using the assignment operator =.

    3

    Expression Statement

    Used to perform an operation.

    An expression statement performs calculations, updates values, calls methods, or changes program state.

    4

    Output Statement

    Used to display result.

    An output statement shows information on the screen.

    5

    Control Statement

    Used to control program flow.

    Control statements decide which statements run, how many times they run, and in what order they run.

    1. Declaration Statements

    A declaration statement is used to declare a variable. It tells the program the variable name and data type.

    int age;
    double price;
    String name;
    boolean isPassed;

    In the above example, variables are declared but not assigned values yet.

    Declaration Statement Breakdown

    Statement Data Type Variable Name
    int age; int age
    double price; double price
    String name; String name

    2. Assignment Statements

    An assignment statement assigns a value to a variable.

    age = 20;
    price = 99.50;
    name = "Ravi";
    isPassed = true;

    The assignment operator = stores the value on the right side into the variable on the left side.

    Declaration and Assignment Together

    int age = 20;
    double price = 99.50;
    String name = "Ravi";
    boolean isPassed = true;

    Here, the variable is declared and assigned a value in the same statement.

    3. Expression Statements

    An expression statement performs an operation and may produce a value or change a value.

    int number1 = 10;
    int number2 = 20;
    int sum = number1 + number2;

    In this example, number1 + number2 is an expression, and int sum = number1 + number2; is a complete statement.

    More Expression Statement Examples

    total = price * quantity;
    count++;
    marks += 5;
    System.out.println("Result: " + total);

    These statements perform actions such as multiplication, increment, addition assignment, and method calling.

    4. Output Statements

    An output statement displays information to the user.

    System.out.println("Welcome to Java");
    System.out.println("Total: " + total);

    In Java, System.out.println() prints output and moves to the next line.

    print vs println

    print() println()
    Prints output on the same line. Prints output and moves to the next line.
    System.out.print("Hello"); System.out.println("Hello");

    5. Control Statements

    Control statements control the flow of execution in a program. They decide which statements should run and how many times.

    Control statements are mainly divided into three types:

    Main Types of Control Statements

    • Decision-making statements: if, if-else, switch
    • Looping statements: for, while, do-while
    • Jump statements: break, continue, return

    Decision-Making Statements

    Decision-making statements allow a program to choose a path based on a condition.

    if Statement

    int marks = 80;
    
    if (marks >= 35) {
        System.out.println("Pass");
    }

    The statement inside the block runs only if the condition is true.

    if-else Statement

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

    If the condition is true, the first block runs. Otherwise, the else block runs.

    switch Statement

    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;
        default:
            System.out.println("Invalid day");
    }

    A switch statement selects one block from multiple options.

    Looping Statements

    Looping statements repeat a block of code multiple times.

    for Loop Statement

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

    A for loop is commonly used when the number of repetitions is known.

    while Loop Statement

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

    A while loop repeats while the condition remains true.

    do-while Loop Statement

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

    A do-while loop runs the block at least once before checking the condition.

    Jump Statements

    Jump statements change the normal flow of execution.

    break Statement

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

    The break statement stops the loop immediately.

    continue Statement

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

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

    return Statement

    static int add(int a, int b) {
        return a + b;
    }

    The return statement sends a value back from a method and can also end method execution.

    Statement Blocks

    A statement block is a group of statements written inside curly braces { }.

    if (marks >= 35) {
        System.out.println("Pass");
        System.out.println("Congratulations!");
    }

    The two output statements are grouped inside one block. If the condition is true, both statements execute.

    Statement vs Expression

    Beginners often confuse expressions and statements. They are related, but they are not the same.

    Expression Statement
    Produces a value. Performs a complete action.
    number1 + number2 sum = number1 + number2;
    Can be part of a statement. Usually forms a full instruction.

    Complete Example: Statements in a Java Program

    The following program uses declaration, assignment, expression, output, and control statements.

    public class Main {
        public static void main(String[] args) {
            int marks = 80;
            String result;
    
            if (marks >= 35) {
                result = "Pass";
            } else {
                result = "Fail";
            }
    
            System.out.println("Result: " + result);
        }
    }

    Output

    Result: Pass

    Statement Breakdown

    Statement Type Purpose
    int marks = 80; Declaration with assignment Creates a variable and stores marks.
    String result; Declaration statement Creates a variable to store result.
    if (marks >= 35) Control statement Checks whether marks are enough to pass.
    result = "Pass"; Assignment statement Stores Pass in result.
    System.out.println("Result: " + result); Output statement Displays final result.

    Statements and Program Flow

    Statements create the flow of a program. The flow may be sequential, conditional, or repetitive.

    1

    Sequential Flow

    Statements run one after another.

    int a = 10;
    int b = 20;
    int sum = a + b;
    System.out.println(sum);
    2

    Conditional Flow

    Statements run based on a condition.

    if (age >= 18) {
        System.out.println("Eligible");
    }
    3

    Repetitive Flow

    Statements run multiple times using loops.

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

    Rules for Writing Statements

    Students should follow some basic rules while writing statements.

    Recommended Rules

    • Write one clear instruction per statement.
    • End Java statements with a semicolon ;.
    • Use meaningful variable names.
    • Write statements inside the correct method or block.
    • Use proper indentation for blocks.
    • Use braces correctly for control statements.
    • Do not write incomplete statements.
    • Check statement order carefully.

    How Statements Help Debugging

    When a program gives wrong output, students can debug it by checking statements one by one.

    Debugging Questions

    • Is the variable declared before use?
    • Is the value assigned correctly?
    • Is the formula written correctly?
    • Is the condition true or false as expected?
    • Is the loop updating the variable properly?
    • Is the output statement printing the correct variable?
    • Is any semicolon missing?
    • Are braces placed correctly?

    Common Beginner Mistakes

    Mistakes

    • Forgetting semicolon after a statement.
    • Using a variable before declaring it.
    • Confusing assignment = with comparison ==.
    • Writing incomplete statements.
    • Putting statements outside the method body.
    • Forgetting braces in control statements.
    • Writing wrong condition in if statements.
    • Creating loops that never stop.

    Better Habits

    • Check every statement carefully.
    • Use semicolons properly.
    • Declare variables before using them.
    • Use = for assignment and == for comparison.
    • Keep statements inside proper blocks.
    • Use indentation for readability.
    • Dry run statements step by step.
    • Test output after writing code.

    Prerequisites Before Learning Statements

    To understand statements properly, students should know some basic programming concepts.

    Basic Prerequisites

    • Basic understanding of what programming is.
    • Understanding of basic program structure.
    • Knowledge of variables and data types.
    • Basic understanding of operators.
    • Understanding of expressions.
    • Basic idea of input, process, and output.
    • Ability to read simple Java code.

    Practice Activity: Identify Statements

    This activity helps students identify different types of statements in a program.

    Task

    Read the following Java program and identify declaration statements, assignment statements, control statements, and output statements.
    public class Main {
        public static void main(String[] args) {
            int number = 8;
    
            if (number % 2 == 0) {
                System.out.println("Even");
            } else {
                System.out.println("Odd");
            }
        }
    }

    Sample Answer

    Code Statement Type
    int number = 8; Declaration with assignment statement.
    if (number % 2 == 0) Control statement.
    System.out.println("Even"); Output statement.
    System.out.println("Odd"); Output statement.

    Mini Quiz

    1

    What is a statement?

    A statement is a complete instruction that tells the computer to perform an action.

    2

    Which symbol usually ends a Java statement?

    A semicolon ; usually ends a Java statement.

    3

    What is an assignment statement?

    An assignment statement stores a value in a variable using the assignment operator =.

    4

    What is a control statement?

    A control statement controls the flow of execution in a program.

    5

    Give one example of an output statement in Java.

    System.out.println("Hello"); is an output statement.

    Interview Questions on Statements

    1

    Define statement in programming.

    A statement is a complete instruction that performs an action during program execution.

    2

    What are the common types of statements?

    Common types include declaration statements, assignment statements, expression statements, output statements, and control statements.

    3

    What is the difference between expression and statement?

    An expression produces a value, while a statement performs a complete action.

    4

    Why are control statements important?

    Control statements are important because they allow programs to make decisions, repeat tasks, and change execution flow.

    5

    Why do Java statements use semicolons?

    Semicolons mark the end of most Java statements and help the compiler understand where an instruction finishes.

    Quick Summary

    Concept Meaning
    Statement A complete instruction executed by the program.
    Declaration Statement Declares a variable.
    Assignment Statement Assigns a value to a variable.
    Expression Statement Performs an operation or method call.
    Output Statement Displays information to the user.
    Control Statement Controls the flow of execution.
    Statement Block A group of statements inside curly braces.

    Final Takeaway

    Statements are the basic instructions of a program. Every meaningful program is built using statements that declare variables, assign values, perform calculations, display output, make decisions, and repeat tasks. Once students understand statements clearly, they can read and write programs with much better confidence.