Learning Java with Aditya Tandon Sir: Conditional Statements

Day 6-7 of Learning Java : Today I started my journey into Java programming which is taught by Aditya Tandon Sir. Here are the key concepts I learned today: Conditional Statements In Java:- Java uses conditional statements to decide which block of code should execute based on condition is true or false. Types:- • If statement • If else statement • Else If ladder • Switch statement • The Ternary Operator ∆ If statement:-The most basic control flow statement. It executes a block of code only if the given condition is true. Example:- if (age >= 18) { System.out.println("You are eligible to vote."); } ∆ If else statement:-It executes a block of code, when if condition is false, the else block runs. Example:- if (score >= 50) { System.out.println("Pass"); } else { System.out.println("Fail"); } ∆ Else If ladder:-When you have multiple conditions to check, you use a ladder. Java tests them from top to bottom and executes the first one that is true. Example:- int score = 85; if (score >= 90) { System.out.println("Grade: A"); } else if (score >= 80) { System.out.println("Grade: B"); } else { System.out.println("Grade: F (Fail)"); } ∆ The switch Statement:-Instead of a long chain of else if statements, a switch is comparing a single variable against a list of constant values. Example:- 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"); } ∆ The Ternary Operator (? :):-This is a shorthand of the if-else statement. It is the only operator in Java that takes three operands. Syntax: variable = (condition) ? value_if_true : value_if_false; This is just the beginning. Excited to continue this journey Special thanks to Rohit Negi bhaiya & Aditya Tandon Sir. #Day6 #Java #Coding #Learning #Consistency

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories