# Day 2: Conditionals, Loops, and Methods

Day 1 was all about understanding what happens *under the hood* when Java code runs. Day 2 shifted gears into actually controlling *how* that code behaves — decision-making, repetition, and organizing code into reusable blocks. Here's everything I covered.

## Conditional Statements (if-else)

Conditionals let a program make decisions and execute different code paths based on whether something is true or false.

**Basic if-else:**

```java

int age = 20;



if (age >= 18) {

    System.out.println("You are an adult.");

} else {

    System.out.println("You are a minor.");

}

```

**else if for multiple conditions:**

```java

int marks = 75;



if (marks >= 90) {

    System.out.println("Grade: A");

} else if (marks >= 75) {

    System.out.println("Grade: B");

} else if (marks >= 50) {

    System.out.println("Grade: C");

} else {

    System.out.println("Grade: F");

}

```

**Nested if:** an if-else block inside another if-else — useful when a decision depends on multiple layered conditions, though too much nesting quickly hurts readability.

**Switch statement:** a cleaner alternative to long if-else chains when checking one variable against multiple fixed values.

```java

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");

}

```

One thing I made sure to note: don't forget `break` — without it, execution "falls through" into the next case, which is a classic beginner bug.

## Loops

Loops let you repeat a block of code without writing it out multiple times.

**for loop** — best when you know exactly how many times you want to repeat something:

```java

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

    System.out.println("Count: " + i);

}

```

**while loop** — best when the number of repetitions isn't known upfront, and depends on a condition:

```java

int i = 1;

while (i <= 5) {

    System.out.println("Count: " + i);

    i++;

}

```

**do-while loop** — same as `while`, but guarantees the code runs *at least once* before the condition is checked:

```java

int i = 1;

do {

    System.out.println("Count: " + i);

    i++;

} while (i <= 5);

```

**Key difference I noted:** in a `while` loop, the condition is checked *before* the block runs. In `do-while`, the block runs first, and the condition is checked *after* — so it always executes at least one time, even if the condition is false from the start.

I also briefly touched on `break` (exits the loop entirely) and `continue` (skips the current iteration and moves to the next).

## Methods (Functions in Java)

Methods let you package a block of code under a name, so you can reuse it instead of rewriting the same logic repeatedly.

**Basic structure:**

```java

returnType methodName(parameters) {

    // code

    return value; // if returnType isn't void

}

```

Example:

```java

public class Calculator {

    static int add(int a, int b) {

        return a + b;

    }



    public static void main(String[] args) {

        int result = add(5, 10);

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

    }

}

```

### User-Defined vs Built-in Methods

*   **Built-in methods** are already provided by Java's standard library — things like `System.out.println()`, `Math.sqrt()`, or `.length()` on a String. You don't write these; you just call them.
    
*   **User-defined methods** are the ones *you* write yourself, like `add()` above, to encapsulate your own logic.
    

Understanding this distinction made a lot of the "magic" method calls from Day 1 (like `println`) make more sense — they're just methods someone else already wrote for us.

### Method (Function) Overloading

This was the most interesting concept today. **Method overloading** means you can have multiple methods with the *same name*, as long as their **parameter list differs** (either in number of parameters or their types).

```java

static int add(int a, int b) {

    return a + b;

}



static double add(double a, double b) {

    return a + b;

}



static int add(int a, int b, int c) {

    return a + b + c;

}

```

Java figures out which version to call based on the arguments you pass — this is resolved at **compile time**, which is why it's also called **compile-time polymorphism**.

Important distinction I made sure to note: overloading is **not** about changing the return type alone. Two methods with the same name, same parameters, but different return types will NOT compile — the parameter list has to be different.

### Scope

Scope determines *where* in the code a variable is accessible.

*   **Local scope**: variables declared inside a method exist only within that method. Once the method finishes executing, they're gone.
    
*   **Instance/class scope**: variables declared at the class level (outside any method) are accessible across multiple methods within that class.
    

```java

public class ScopeExample {

    static int classLevelVar = 100; // accessible throughout the class



    static void demo() {

        int localVar = 10; // only accessible inside demo()

        System.out.println(localVar + classLevelVar);

    }

}

```

Trying to access `localVar` outside `demo()` would throw a compilation error — a good reminder that variables have a defined "lifetime" tied to where they're declared.

## Wrapping Up Day 2

Today felt like the point where Java started feeling less like theory and more like actual programming — being able to make decisions (if-else), repeat actions (loops), and organize logic into reusable blocks (methods) are the real building blocks of any program.

**Tomorrow (Day 3):** planning to move into arrays and possibly start touching on the basics of Object-Oriented Programming (classes and objects) — since that's where Java really starts to shine.

If you spotted something I got wrong, or have a cleaner way to explain overloading or scope, let me know in the comments. Always learning in public here. 👋

* * *

*This is Day 2 of my #100DaysOfJava challenge. Catch up on* [*Day 1*](#) *if you missed it.*

create the cover image for me and I hope you remember the dimensions
