DMCA.com Protection Status Java Programming Lesson 4 | Conditional Statements Tutorial for Beginners - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 4 | Conditional Statements Tutorial for Beginners

LESSON-4: CONDITIONAL STATEMENTS

🚀 Let's move to Lesson-4: Conditional Statements
(Real-life logic in Java ✅)



📘 Lesson 4 — Conditional Statements (if, else, switch)

Conditional statements are used when your program must decide something based on conditions.


✅ 1️⃣ if Statement

Executes code only if condition is true.

if (condition) { // code }

Example

int age = 18; if(age >= 18){ System.out.println("You are an adult."); }

✅ 2️⃣ if...else Statement

If condition true → first block
Else → second block

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

✅ 3️⃣ else if Statement

Used for multiple conditions

int marks = 85; if(marks >= 90){ System.out.println("A+"); } else if(marks >= 80){ System.out.println("A"); } else { System.out.println("Try Again"); }

✅ 4️⃣ switch Statement

Better for fixed values (menu, grades, days)

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

📌 break = stops switching
📌 default = fallback option


✅ Full Practical Example

import java.util.Scanner; public class ConditionsExample { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter your age: "); int age = input.nextInt(); if(age >= 18){ System.out.println("Eligible for Voting"); } else { System.out.println("Not Eligible"); } System.out.println("Enter day number (1-7): "); int day = input.nextInt(); 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; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Invalid Day"); } } }

📝 Exercise for You ✅

Write a program:

📌 Input
A number from user

📌 Output
Check and print whether:

✅ Positive or Negative
✅ Even or Odd
✅ Number category using switch (1 → "One", 2 → "Two", otherwise → "Other Number")

You must use:

✔ if
✔ else-if
✔ switch

Reply with:
✅ Your Code
✅ Output Screenshot or text

I will grade it ✅🔥


🎯 Quick Quiz

1️⃣ Which keyword stops switch case?
A) stop
B) exit
C) break

2️⃣ Choose valid condition:
A) if x = 10
B) if(x == 10)
C) if(x => 10)

Reply: 1 → ?, 2 → ?


✅ Lesson-4 Completed!

When you're ready →
Lesson-5: Loops (for, while, do-while) 🚀

Send your exercise + quiz answers below 👇😊


▶ NEXT PART - Lesson-5: Loops

No comments

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.