Programming and Java Fundamentals

Conditional Statements

ReadingPreview

You are viewing a free preview lesson.

Lesson Overview

Program সবসময় একই instruction execute করে না। অনেক সময় data বা condition অনুযায়ী program-কে আলাদা decision নিতে হয়।

উদাহরণ:

  • User-এর বয়স 18 বা তার বেশি হলে enrollment allow করা
  • Mark 40 বা তার বেশি হলে passed দেখানো
  • Password সঠিক হলে login allow করা
  • Product stock না থাকলে order reject করা
  • Number positive, negative নাকি zero তা নির্ধারণ করা

Java-তে condition অনুযায়ী program flow control করার জন্য conditional statement ব্যবহার করা হয়।

এই lesson-এ আমরা শিখব:

  • Boolean condition
  • if
  • if-else
  • else-if
  • Multiple independent conditions
  • Nested conditions
  • Logical operators in conditions
  • Range validation
  • String এবং null checks
  • Common conditional mistakes
  • Practical decision-making programs

Learning Objectives

এই lesson শেষ করার পর আপনি পারবেন:

  • Boolean expression ব্যবহার করে decision নিতে
  • if, if-else এবং else-if লিখতে
  • Multiple condition combine করতে
  • Numeric range validate করতে
  • Nested condition বুঝতে
  • String safely compare করতে
  • Invalid input আগে reject করতে
  • Grade, eligibility এবং login logic তৈরি করতে

What Is Conditional Execution?

Conditional execution হলো condition-এর result অনুযায়ী কোনো code block execute বা skip করা।

int age = 20;

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

Condition:

age >= 18

Result:

true

তাই output:

Adult

যদি age হয় 15, condition false হবে এবং block execute হবে না।


Boolean Conditions

Conditional statement-এর condition অবশ্যই boolean result দিতে হবে।

Valid conditions:

age >= 18
score == 100
isActive
!accountBlocked
emailValid && passwordValid
"ADMIN".equals(role)

প্রতিটি condition-এর result হবে:

true

অথবা:

false

Numbers Are Not Boolean Values

Java-তে number সরাসরি condition হিসেবে ব্যবহার করা যায় না।

Invalid:

int value = 1;

if (value) {
    System.out.println("Valid");
}

Correct:

if (value == 1) {
    System.out.println("Value is one");
}

The if Statement

Syntax:

if (condition) {
    // Executes when condition is true
}

Example:

int mark = 75;

if (mark >= 40) {
    System.out.println("Passed");
}

Output:

Passed

যদি condition false হয়, block skip হবে।


Multiple Statements Inside if

int age = 20;

if (age >= 18) {
    System.out.println("Age requirement met");
    System.out.println("Registration allowed");
}

Condition true হলে block-এর সব statement top-to-bottom execute হবে।


Always Use Curly Braces

একটি statement হলে braces technically omit করা যায়:

if (age >= 18)
    System.out.println("Adult");

তবে সবসময় braces ব্যবহার করা safer।

Wrong and misleading:

if (age >= 18)
    System.out.println("Adult");
    System.out.println("Registration allowed");

এখানে শুধু প্রথম statement condition-এর অংশ।

Actual behaviour:

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

System.out.println("Registration allowed");

Correct:

if (age >= 18) {
    System.out.println("Adult");
    System.out.println("Registration allowed");
}

The if-else Statement

Condition true হলে একটি block এবং false হলে অন্য block execute করতে if-else ব্যবহার করা হয়।

Syntax:

if (condition) {
    // True branch
} else {
    // False branch
}

Example:

int mark = 35;

if (mark >= 40) {
    System.out.println("Passed");
} else {
    System.out.println("Failed");
}

Output:

Failed

একটি execution-এ শুধু একটি branch execute হবে।


Example: Even or Odd

int number = 17;

if (number % 2 == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}

Output:

Odd

The else-if Chain

Multiple mutually exclusive condition handle করতে else-if ব্যবহার করা হয়।

Syntax:

if (firstCondition) {
    // First branch
} else if (secondCondition) {
    // Second branch
} else if (thirdCondition) {
    // Third branch
} else {
    // Default branch
}

Java conditionগুলো top-to-bottom check করে।

প্রথম true branch execute হওয়ার পর remaining branches skip হয়।


Example: Positive, Negative, or Zero

int number = -10;

if (number > 0) {
    System.out.println("Positive");
} else if (number < 0) {
    System.out.println("Negative");
} else {
    System.out.println("Zero");
}

Output:

Negative

Condition Order Matters

Wrong order:

int mark = 90;

if (mark >= 40) {
    System.out.println("Passed");
} else if (mark >= 80) {
    System.out.println("Excellent");
}

Output:

Passed

mark >= 40 প্রথমেই true হয়েছে। তাই second branch check হয়নি।

Correct:

if (mark >= 80) {
    System.out.println("Excellent");
} else if (mark >= 40) {
    System.out.println("Passed");
} else {
    System.out.println("Failed");
}

Specific বা higher condition আগে রাখা উচিত।


Grade Calculation

int mark = 85;
String grade;

if (mark < 0 || mark > 100) {
    grade = "Invalid";
} else if (mark >= 80) {
    grade = "A";
} else if (mark >= 70) {
    grade = "B";
} else if (mark >= 60) {
    grade = "C";
} else if (mark >= 50) {
    grade = "D";
} else if (mark >= 40) {
    grade = "E";
} else {
    grade = "F";
}

System.out.println("Grade: " + grade);

Output:

Grade: A

Why Range Checks Are Not Repeated

Consider:

if (mark >= 80) {
    grade = "A";
} else if (mark >= 70) {
    grade = "B";
}

Second condition only check হয় যখন:

mark < 80

তাই:

mark >= 70

effectively means:

70 <= mark < 80

Multiple Independent if Statements

Multiple if statement independently evaluate হয়।

int mark = 90;

if (mark >= 40) {
    System.out.println("Passed");
}

if (mark >= 80) {
    System.out.println("Distinction");
}

Output:

Passed
Distinction

দুটি condition true হওয়ায় দুটি block-ই execute হয়েছে।


Multiple if vs else-if

Multiple if

if (conditionA) {
}

if (conditionB) {
}
  • Conditions independently evaluate হয়
  • একাধিক block execute হতে পারে

else-if

if (conditionA) {
} else if (conditionB) {
}
  • First matching branch execute হয়
  • Remaining branches skip হয়

When to Use Multiple if

যখন conditions independent।

Example:

if (mark >= 40) {
    System.out.println("Passed");
}

if (mark >= 80) {
    System.out.println("Scholarship eligible");
}

একজন student একইসঙ্গে passed এবং scholarship eligible হতে পারে।


When to Use else-if

যখন results mutually exclusive।

Example:

Grade A
Grade B
Grade C
Grade D
Grade F

একটি mark-এর জন্য একটি grade।


Combining Conditions with &&

&& ব্যবহার করা হয় যখন সব condition true হতে হবে।

int age = 20;
boolean emailVerified = true;

if (age >= 18 && emailVerified) {
    System.out.println("Enrollment allowed");
}

Block execute হবে যদি:

  • Age অন্তত 18
  • Email verified

Combining Conditions with ||

|| ব্যবহার করা হয় যখন অন্তত একটি condition true হলেই হবে।

boolean isInstructor = false;
boolean isAdmin = true;

if (isInstructor || isAdmin) {
    System.out.println("Access allowed");
}

Output:

Access allowed

Using Logical NOT

! boolean value reverse করে।

boolean accountBlocked = false;

if (!accountBlocked) {
    System.out.println("Account access allowed");
}

Complex Conditions

int age = 20;
boolean emailVerified = true;
boolean accountBlocked = false;
boolean enrollmentOpen = true;

if (
        age >= 18
        && emailVerified
        && !accountBlocked
        && enrollmentOpen
) {
    System.out.println("Enrollment allowed");
}

Long condition multiple line-এ লেখা বেশি readable।


Extracting Conditions into Variables

Complex logic understandable করতে intermediate boolean variable ব্যবহার করা যায়।

boolean ageValid = age >= 18;
boolean accountAllowed = !accountBlocked;

boolean canEnroll =
        ageValid
        && emailVerified
        && accountAllowed
        && enrollmentOpen;

if (canEnroll) {
    System.out.println("Enrollment allowed");
}

Range Validation

Valid mark range:

0 to 100
boolean validMark =
        mark >= 0
        && mark <= 100;

Invalid mark:

boolean invalidMark =
        mark < 0
        || mark > 100;

Wrong Range Logic

Wrong:

boolean validMark =
        mark >= 0
        || mark <= 100;

ধরা যাক:

mark = 500

Evaluation:

500 >= 0 → true
500 <= 100 → false
true || false → true

Wrongly valid হয়ে গেছে।

Correct:

mark >= 0 && mark <= 100

Chained Comparison Is Invalid

Mathematical notation:

0 <= mark <= 100

Java-তে invalid:

if (0 <= mark <= 100) {
}

Correct:

if (mark >= 0 && mark <= 100) {
}

Nested if

একটি conditional block-এর মধ্যে আরেকটি conditional statement থাকলে সেটি nested if

int age = 20;
boolean emailVerified = true;

if (age >= 18) {
    if (emailVerified) {
        System.out.println("Enrollment allowed");
    }
}

Inner condition শুধু outer condition true হলে evaluate হয়।


Nested if-else

boolean loggedIn = true;
boolean isAdmin = false;

if (loggedIn) {
    if (isAdmin) {
        System.out.println("Admin dashboard");
    } else {
        System.out.println("User dashboard");
    }
} else {
    System.out.println("Please log in");
}

Nested condition useful যখন inner decision outer context-এর ওপর depend করে।


Avoid Unnecessary Deep Nesting

Hard to read:

if (userExists) {
    if (passwordCorrect) {
        if (!accountLocked) {
            if (emailVerified) {
                System.out.println("Login successful");
            }
        }
    }
}

Simpler:

boolean canLogin =
        userExists
        && passwordCorrect
        && !accountLocked
        && emailVerified;

if (canLogin) {
    System.out.println("Login successful");
}

Showing Specific Failure Reasons

সব condition combine করলে শুধু success বা failure জানা যায়।

Specific reason দেখাতে ordered else-if useful।

if (!userExists) {
    System.out.println("User not found");
} else if (!passwordCorrect) {
    System.out.println("Incorrect password");
} else if (accountLocked) {
    System.out.println("Account is locked");
} else if (!emailVerified) {
    System.out.println("Email verification required");
} else {
    System.out.println("Login successful");
}

Condition order এখানে business flow reflect করে।


Guard Conditions

Invalid input আগে handle করলে main logic সহজ হয়।

int mark = 120;

if (mark < 0 || mark > 100) {
    System.out.println("Invalid mark");
} else {
    System.out.println("Valid mark");
}

Method-এর মধ্যে early return ব্যবহার করা যায়:

static void displayGrade(int mark) {
    if (mark < 0 || mark > 100) {
        System.out.println("Invalid mark");
        return;
    }

    if (mark >= 80) {
        System.out.println("Grade: A");
    } else if (mark >= 70) {
        System.out.println("Grade: B");
    } else {
        System.out.println("Grade: C or below");
    }
}

Variable Scope in Conditional Blocks

Block-এর মধ্যে declare করা variable block-এর বাইরে accessible নয়।

if (mark >= 40) {
    String result = "Passed";

    System.out.println(result);
}

Invalid:

if (mark >= 40) {
    String result = "Passed";
}

System.out.println(result);

result শুধু if block-এর scope-এর মধ্যে আছে।


Assigning a Result in Both Branches

String result;

if (mark >= 40) {
    result = "Passed";
} else {
    result = "Failed";
}

System.out.println(result);

Valid, কারণ দুই possible branch-ই result initialize করে।


Possibly Uninitialized Variable

Wrong:

String result;

if (mark >= 40) {
    result = "Passed";
}

System.out.println(result);

Condition false হলে result initialize হবে না।

Compiler error হতে পারে:

variable result might not have been initialized

Ternary Operator for Simple Decisions

Simple if-else:

String result;

if (mark >= 40) {
    result = "Passed";
} else {
    result = "Failed";
}

Ternary form:

String result =
        mark >= 40
                ? "Passed"
                : "Failed";

Complex logic-এর জন্য if-else বেশি readable।


Comparing Strings in Conditions

String content compare করতে == ব্যবহার করা উচিত নয়।

Wrong:

if (role == "ADMIN") {
    System.out.println("Access allowed");
}

Correct:

if ("ADMIN".equals(role)) {
    System.out.println("Access allowed");
}

Case-insensitive:

if ("ADMIN".equalsIgnoreCase(role)) {
    System.out.println("Access allowed");
}

Null-Safe String Validation

String courseName = null;

if (
        courseName != null
        && !courseName.isBlank()
) {
    System.out.println("Valid course name");
} else {
    System.out.println("Course name is required");
}

courseName null হলে first condition false হবে।

Short-circuit-এর কারণে:

courseName.isBlank()

call হবে না।


Wrong Null Check Order

Wrong:

if (
        !courseName.isBlank()
        && courseName != null
) {
}

courseName null হলে প্রথম method call-এই NullPointerException হবে।

Correct order:

courseName != null
&& !courseName.isBlank()

Checking Multiple String Values

String role = "INSTRUCTOR";

if (
        "ADMIN".equals(role)
        || "INSTRUCTOR".equals(role)
) {
    System.out.println("Access allowed");
}

Invalid approach:

if (role.equals("ADMIN" || "INSTRUCTOR")) {
}

|| boolean expressions combine করে, String values নয়।


Comparing char

Primitive char compare করতে == ব্যবহার করা যায়।

char grade = 'A';

if (grade == 'A') {
    System.out.println("Excellent");
}

Floating-Point Conditions

Floating-point value direct equality দিয়ে compare করা risky হতে পারে।

double total = 0.1 + 0.2;

if (total == 0.3) {
    System.out.println("Equal");
}

Condition false হতে পারে।

Approximate comparison:

double tolerance = 0.000001;

if (Math.abs(total - 0.3) < tolerance) {
    System.out.println("Approximately equal");
}

Practical Example: Age Classification

int age = 25;

if (age < 0 || age > 150) {
    System.out.println("Invalid age");
} else if (age < 13) {
    System.out.println("Child");
} else if (age < 18) {
    System.out.println("Teenager");
} else if (age < 60) {
    System.out.println("Adult");
} else {
    System.out.println("Senior");
}

Output:

Adult

Practical Example: Number Classification

int number = -8;

if (number > 0) {
    System.out.println("Positive");
} else if (number < 0) {
    System.out.println("Negative");
} else {
    System.out.println("Zero");
}

if (number % 2 == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}

Output:

Negative
Even

Sign এবং even/odd independent property, তাই separate conditional statements ব্যবহার করা হয়েছে।


Practical Example: Course Enrollment

public class Main {

    static final int MINIMUM_AGE = 18;

    public static void main(String[] args) {
        int learnerAge = 20;
        boolean emailVerified = true;
        boolean enrollmentOpen = true;
        boolean alreadyEnrolled = false;
        boolean accountBlocked = false;

        if (learnerAge < 0) {
            System.out.println("Invalid age");
        } else if (learnerAge < MINIMUM_AGE) {
            System.out.println(
                    "Minimum age requirement not met"
            );
        } else if (!emailVerified) {
            System.out.println(
                    "Email verification required"
            );
        } else if (!enrollmentOpen) {
            System.out.println(
                    "Enrollment is closed"
            );
        } else if (alreadyEnrolled) {
            System.out.println(
                    "Learner is already enrolled"
            );
        } else if (accountBlocked) {
            System.out.println(
                    "Blocked account cannot enroll"
            );
        } else {
            System.out.println(
                    "Enrollment allowed"
            );
        }
    }
}

Practical Example: Product Stock

int stockQuantity = 5;
int requestedQuantity = 8;

if (requestedQuantity <= 0) {
    System.out.println(
            "Quantity must be greater than zero"
    );
} else if (stockQuantity == 0) {
    System.out.println(
            "Product is out of stock"
    );
} else if (
        requestedQuantity > stockQuantity
) {
    System.out.println(
            "Not enough stock"
    );
} else {
    System.out.println(
            "Order can be placed"
    );
}

Output:

Not enough stock

Practical Example: Discount Rules

int orderTotal = 6000;
boolean premiumCustomer = true;

int discountPercentage;

if (
        premiumCustomer
        && orderTotal >= 5000
) {
    discountPercentage = 20;
} else if (premiumCustomer) {
    discountPercentage = 10;
} else if (orderTotal >= 5000) {
    discountPercentage = 5;
} else {
    discountPercentage = 0;
}

System.out.println(
        "Discount: "
        + discountPercentage
        + "%"
);

Combined and most specific condition আগে রাখা হয়েছে।


Practical Example: Largest of Three Numbers

int firstNumber = 10;
int secondNumber = 25;
int thirdNumber = 20;

if (
        firstNumber >= secondNumber
        && firstNumber >= thirdNumber
) {
    System.out.println(
            firstNumber + " is largest"
    );
} else if (
        secondNumber >= firstNumber
        && secondNumber >= thirdNumber
) {
    System.out.println(
            secondNumber + " is largest"
    );
} else {
    System.out.println(
            thirdNumber + " is largest"
    );
}

Output:

25 is largest

Interactive Grade Program

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner =
                new Scanner(System.in);

        System.out.print(
                "Enter your mark: "
        );

        int mark =
                Integer.parseInt(
                        scanner
                                .nextLine()
                                .strip()
                );

        if (mark < 0 || mark > 100) {
            System.out.println(
                    "Invalid mark"
            );
        } else if (mark >= 80) {
            System.out.println(
                    "Grade: A"
            );
        } else if (mark >= 70) {
            System.out.println(
                    "Grade: B"
            );
        } else if (mark >= 60) {
            System.out.println(
                    "Grade: C"
            );
        } else if (mark >= 50) {
            System.out.println(
                    "Grade: D"
            );
        } else if (mark >= 40) {
            System.out.println(
                    "Grade: E"
            );
        } else {
            System.out.println(
                    "Grade: F"
            );
        }

        scanner.close();
    }
}

Complete Student Result Program

import java.util.Scanner;

public class Main {

    static final int MINIMUM_MARK = 0;
    static final int MAXIMUM_MARK = 100;
    static final int PASSING_MARK = 40;

    public static void main(String[] args) {
        Scanner scanner =
                new Scanner(System.in);

        System.out.print(
                "Enter student name: "
        );

        String studentName =
                scanner
                        .nextLine()
                        .strip();

        System.out.print(
                "Enter mathematics mark: "
        );

        int mathematicsMark =
                Integer.parseInt(
                        scanner
                                .nextLine()
                                .strip()
                );

        System.out.print(
                "Enter English mark: "
        );

        int englishMark =
                Integer.parseInt(
                        scanner
                                .nextLine()
                                .strip()
                );

        System.out.print(
                "Enter science mark: "
        );

        int scienceMark =
                Integer.parseInt(
                        scanner
                                .nextLine()
                                .strip()
                );

        boolean mathematicsValid =
                mathematicsMark >= MINIMUM_MARK
                && mathematicsMark <= MAXIMUM_MARK;

        boolean englishValid =
                englishMark >= MINIMUM_MARK
                && englishMark <= MAXIMUM_MARK;

        boolean scienceValid =
                scienceMark >= MINIMUM_MARK
                && scienceMark <= MAXIMUM_MARK;

        boolean allMarksValid =
                mathematicsValid
                && englishValid
                && scienceValid;

        if (!allMarksValid) {
            System.out.println(
                    "One or more marks are invalid"
            );

            scanner.close();
            return;
        }

        int totalMarks =
                mathematicsMark
                + englishMark
                + scienceMark;

        double averageMark =
                totalMarks / 3.0;

        boolean passedAllSubjects =
                mathematicsMark >= PASSING_MARK
                && englishMark >= PASSING_MARK
                && scienceMark >= PASSING_MARK;

        String result =
                passedAllSubjects
                        ? "Passed"
                        : "Failed";

        String grade;

        if (!passedAllSubjects) {
            grade = "F";
        } else if (averageMark >= 80) {
            grade = "A";
        } else if (averageMark >= 70) {
            grade = "B";
        } else if (averageMark >= 60) {
            grade = "C";
        } else if (averageMark >= 50) {
            grade = "D";
        } else {
            grade = "E";
        }

        System.out.println();
        System.out.println(
                "Student: " + studentName
        );

        System.out.println(
                "Total: " + totalMarks
        );

        System.out.println(
                "Average: "
                + "%.2f".formatted(
                        averageMark
                )
        );

        System.out.println(
                "Grade: " + grade
        );

        System.out.println(
                "Result: " + result
        );

        scanner.close();
    }
}

Common Mistakes

Extra Semicolon After if

Wrong:

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

The semicolon ends the if statement।

Correct:

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

Assignment Instead of Comparison

Wrong:

boolean active = false;

if (active = true) {
    System.out.println("Active");
}

এখানে true assign হয়েছে।

Preferred:

if (active) {
    System.out.println("Active");
}

Comparing Boolean with true

Verbose:

if (isAvailable == true) {
}

Preferred:

if (isAvailable) {
}

For false:

if (!isAvailable) {
}

Wrong Grade Order

Wrong:

if (mark >= 40) {
    System.out.println("Passed");
} else if (mark >= 80) {
    System.out.println("Grade A");
}

Correct:

if (mark >= 80) {
    System.out.println("Grade A");
} else if (mark >= 40) {
    System.out.println("Passed");
}

Independent if for Exclusive Grades

Wrong:

if (mark >= 80) {
    System.out.println("A");
}

if (mark >= 70) {
    System.out.println("B");
}

if (mark >= 60) {
    System.out.println("C");
}

Mark 85 হলে output:

A
B
C

Correct:

if (mark >= 80) {
    System.out.println("A");
} else if (mark >= 70) {
    System.out.println("B");
} else if (mark >= 60) {
    System.out.println("C");
}

Wrong String Comparison

Wrong:

if (role == "ADMIN") {
}

Correct:

if ("ADMIN".equals(role)) {
}

Wrong Null Check Order

Wrong:

if (
        !name.isBlank()
        && name != null
) {
}

Correct:

if (
        name != null
        && !name.isBlank()
) {
}

Impossible Condition

if (
        age < 18
        && age >= 18
) {
}

একটি value একই সময়ে দুই condition satisfy করতে পারে না।

এই block কখনো execute হবে না।


Always-True OR Logic

Wrong:

if (
        !"ADMIN".equals(role)
        || !"INSTRUCTOR".equals(role)
) {
}

কোনো role একইসঙ্গে দুটো value না হওয়ায় expression প্রায় সবসময় true।

Correct “neither role” check:

if (
        !"ADMIN".equals(role)
        && !"INSTRUCTOR".equals(role)
) {
}

Testing Conditional Logic

Conditional logic test করার সময় শুধু normal value নয়, boundary value ব্যবহার করা উচিত।

Age requirement:

age >= 18

Test:

17
18
19

Mark range:

mark >= 0 && mark <= 100

Test:

-1
0
1
99
100
101

Grade boundaries:

39
40
49
50
59
60
69
70
79
80
100

Boundary tests off-by-one errors identify করতে সাহায্য করে।


Practice Exercises

Exercise 1: Even or Odd

একটি integer নিন এবং determine করুন সেটি even নাকি odd।


Exercise 2: Positive, Negative, or Zero

একটি integer-এর জন্য print করুন:

Positive
Negative
Zero

Exercise 3: Valid Mark

Mark 0 থেকে 100-এর মধ্যে আছে কি না check করুন।


Exercise 4: Grade Calculator

Rules:

80–100 → A
70–79  → B
60–69  → C
50–59  → D
40–49  → E
0–39   → F

Invalid mark handle করুন।


Exercise 5: Login Decision

Variables:

boolean userExists = true;
boolean passwordCorrect = false;
boolean accountLocked = false;
boolean emailVerified = true;

Specific message দেখান:

  • User not found
  • Incorrect password
  • Account locked
  • Verify email
  • Login successful

Exercise 6: Enrollment Eligibility

Rules:

  • Minimum age 18
  • Email verified
  • Enrollment open
  • Account blocked নয়
  • Already enrolled নয়

Specific failure reason এবং success message দেখান।


Exercise 7: Largest of Three Numbers

তিনটি integer-এর মধ্যে largest value নির্ধারণ করুন।


Exercise 8: Discount Rules

Rules:

  • Premium এবং total 5000 বা বেশি → 20%
  • শুধু premium → 10%
  • শুধু total 5000 বা বেশি → 5%
  • Otherwise → 0%

Exercise 9: Safe String Validation

একটি name valid হবে যদি:

  • null না হয়
  • Blank না হয়

Exercise 10: Interactive Student Result

Scanner ব্যবহার করে:

  • Student name
  • Three subject marks

নিন এবং calculate করুন:

  • Total
  • Average
  • Grade
  • Passed বা failed

Knowledge Check

Question 1

Conditional statement কী?

Question 2

if condition-এর result কোন type হতে হবে?

Question 3

if block কখন execute হয়?

Question 4

else কখন execute হয়?

Question 5

else-if chain-এ কয়টি branch execute হয়?

Question 6

Multiple independent if-এর একাধিক block execute হতে পারে কি?

Question 7

Condition order কেন গুরুত্বপূর্ণ?

Question 8

Valid range 0–100-এর expression কী?

Question 9

Invalid range-এর expression কী?

Question 10

&&, || এবং ! কী করে?

Question 11

Nested if কী?

Question 12

String content compare করতে কী ব্যবহার করা উচিত?

Question 13

name != null && !name.isBlank() safe কেন?

Question 14

Conditional block-এর local variable বাইরে accessible কি?

Question 15

Boundary testing কেন প্রয়োজন?


Knowledge Check Answers

Answer 1

Condition অনুযায়ী program-এর execution path control করা statement হলো conditional statement।

Answer 2

boolean

Answer 3

Condition true হলে।

Answer 4

Associated if এবং previous else-if conditions false হলে।

Answer 5

সর্বোচ্চ একটি matching branch execute হয়।

Answer 6

হ্যাঁ।

Answer 7

প্রথম matching branch-এর পর remaining branches skip হয়।

Answer 8

mark >= 0 && mark <= 100

Answer 9

mark < 0 || mark > 100

Answer 10

  • && সব conditions true require করে
  • || অন্তত একটি true require করে
  • ! boolean value reverse করে

Answer 11

এক conditional block-এর মধ্যে আরেকটি conditional statement।

Answer 12

equals()

অথবা case-insensitive comparison-এর জন্য:

equalsIgnoreCase()

Answer 13

name null হলে first condition false হয় এবং short-circuit-এর কারণে isBlank() call হয় না।

Answer 14

না।

Answer 15

Condition change হওয়ার boundary-এর আশেপাশে error আছে কি না check করতে।


Lesson Summary

এই lesson-এ আমরা শিখেছি:

  • Conditional statement program flow control করে
  • Condition অবশ্যই boolean হতে হয়
  • if true branch execute করে
  • else false branch handle করে
  • else-if multiple exclusive branch তৈরি করে
  • First matching branch-এর পর remaining branches skip হয়
  • Condition order result পরিবর্তন করতে পারে
  • Multiple independent if-এর একাধিক block execute হতে পারে
  • Mutually exclusive result-এর জন্য else-if appropriate
  • &&, || এবং ! complex condition তৈরি করে
  • Numeric range lower এবং upper boundary দিয়ে validate করা হয়
  • Nested if context-dependent decision handle করে
  • Deep nesting logical operator দিয়ে simplify করা যায়
  • Guard condition invalid case আগে handle করে
  • Block variable বাইরে accessible নয়
  • String compare করতে .equals() ব্যবহার করা উচিত
  • Null check method call-এর আগে রাখতে হয়
  • Extra semicolon conditional logic ভেঙে দিতে পারে
  • Boundary testing conditional bugs identify করতে সাহায্য করে

Next Lesson

পরবর্তী lesson-এ আমরা switch expressions এবং statements শিখব।

আমরা জানব:

  • Traditional switch
  • case
  • default
  • break
  • Fall-through
  • Multiple case labels
  • Arrow-style switch
  • switch expression
  • yield
  • String এবং enum values নিয়ে switch
  • if-else এবং switch নির্বাচন