From be77dd82d63f4fe3600091d0b84944ebc867f8de Mon Sep 17 00:00:00 2001 From: uvhareesh Date: Sat, 17 May 2025 17:59:27 +0530 Subject: [PATCH] Conditional Statements Examples --- src/Notes/Conditional Statements | 114 ++++++++++++++++++ src/controlStatements/IfCondition4.java | 20 +++ src/controlStatements/IfCondition5.java | 32 +++++ src/controlStatements/SwitchCase.java | 30 +++++ src/controlStatements/SwitchCase1.java | 29 +++++ .../TwoLargestNumbersWithTernary.java | 49 ++++++++ 6 files changed, 274 insertions(+) create mode 100644 src/Notes/Conditional Statements create mode 100644 src/controlStatements/IfCondition4.java create mode 100644 src/controlStatements/IfCondition5.java create mode 100644 src/controlStatements/SwitchCase.java create mode 100644 src/controlStatements/SwitchCase1.java create mode 100644 src/controlStatements/TwoLargestNumbersWithTernary.java diff --git a/src/Notes/Conditional Statements b/src/Notes/Conditional Statements new file mode 100644 index 0000000..42faf1b --- /dev/null +++ b/src/Notes/Conditional Statements @@ -0,0 +1,114 @@ +Java Conditional Statements – Study Material +Overview +This session covers Java conditional statements, a key concept used to control the flow of execution in Java programs based on certain conditions. These are crucial when automating test cases using Selenium. + +Types of Conditional Statements in Java +1. if Statement +Syntax: + + java +CopyEdit +if (condition) { + // code to execute if condition is true +} + + +Usage: Executes a block only if the condition is true. + + +2. if-else Statement +Syntax: + + java +CopyEdit +if (condition) { + // true block +} else { + // false block +} + + +Usage: Provides an alternate block of code if the condition is false. + + +3. if-else-if Ladder +Syntax: + + java +CopyEdit +if (condition1) { + // block 1 +} else if (condition2) { + // block 2 +} else { + // default block +} + + +Usage: Checks multiple conditions sequentially. + + +4. Nested if +Usage: if statement inside another if block. + + +Allows more complex decision structures. + + + +Switch Statement +Syntax: + + java +CopyEdit +switch (expression) { + case value1: + // code block + break; + case value2: + // code block + break; + default: + // default block +} + + +Usage: Best used when evaluating one variable against many possible constant values. + + +Limitations: Works only with byte, short, char, int, enum, and String (Java 7+). + + + +Comparison and Logical Operators +Used in condition checks: +Comparison Operators: ==, !=, <, >, <=, >= + + +Logical Operators: && (AND), || (OR), ! (NOT) + + + +Practical Examples +Age Validation: Check if a user is eligible to vote. + + +Grade System: Use of if-else-if ladder to assign grades. + + +Menu Selection: Switch case to select options like login, register, etc. + + + +Key Points +Conditional statements allow branching logic in programs. + + +Use if when there's only one condition. + + +Use switch for cleaner code when checking a single variable against multiple constants. + + +Always test boundary conditions. + diff --git a/src/controlStatements/IfCondition4.java b/src/controlStatements/IfCondition4.java new file mode 100644 index 0000000..8f7f5e9 --- /dev/null +++ b/src/controlStatements/IfCondition4.java @@ -0,0 +1,20 @@ +package controlStatements; + +public class IfCondition4 { + + public static void main(String[] args) { + //Smallest of numbers + + int a = 10, b = 20, c = 30; + + if (a <= b && a <= c) { + System.out.println("Smallest number is: " + a); + } else if (b <= a && b <= c) { + System.out.println("Smallest number is: " + b); + } else { + System.out.println("Smallest number is: " + c); + } + + } + +} diff --git a/src/controlStatements/IfCondition5.java b/src/controlStatements/IfCondition5.java new file mode 100644 index 0000000..1883523 --- /dev/null +++ b/src/controlStatements/IfCondition5.java @@ -0,0 +1,32 @@ +package controlStatements; + + +public class IfCondition5 { + + public static void main(String[] args) { + //Two Largest Numbers + + int a = 10, b = 20, c = 30; + + if (a >= b && a >= c) { + if (b >= c) { + System.out.println("Two largest numbers: " + a + " and " + b); + } else { + System.out.println("Two largest numbers: " + a + " and " + c); + } + } else if (b >= a && b >= c) { + if (a >= c) { + System.out.println("Two largest numbers: " + b + " and " + a); + } else { + System.out.println("Two largest numbers: " + b + " and " + c); + } + } else { + if (a >= b) { + System.out.println("Two largest numbers: " + c + " and " + a); + } else { + System.out.println("Two largest numbers: " + c + " and " + b); + } + } + } + +} diff --git a/src/controlStatements/SwitchCase.java b/src/controlStatements/SwitchCase.java new file mode 100644 index 0000000..ef61b82 --- /dev/null +++ b/src/controlStatements/SwitchCase.java @@ -0,0 +1,30 @@ +package controlStatements; + +public class SwitchCase { + + public static void main(String[] args) { + + int weekno=1; + switch(weekno) + { + case 1: System.out.println("Sunday"); + break; + case 2: System.out.println("Monday"); + break; + case 3: System.out.println("Tuesday"); + break; + case 4: System.out.println("Wednesday"); + break; + case 5: System.out.println("Thursday"); + break; + case 6: System.out.println("Friday"); + break; + case 7: System.out.println("Saturday"); + default: System.out.println("Invalid week"); + } + + + + } + +} diff --git a/src/controlStatements/SwitchCase1.java b/src/controlStatements/SwitchCase1.java new file mode 100644 index 0000000..7c13bf7 --- /dev/null +++ b/src/controlStatements/SwitchCase1.java @@ -0,0 +1,29 @@ +package controlStatements; + +public class SwitchCase1 { + + public static void main(String[] args) { + + String weekName="Sunday"; + + switch(weekName) + { + case "Sunday": System.out.println(1); + break; + case "Monday": System.out.println(2); + break; + case "Tuesday": System.out.println(3); + break; + case "Wednesday": System.out.println(4); + break; + case "Thursday": System.out.println(5); + break; + case "Friday": System.out.println(6); + break; + case "Saturday": System.out.println(7); + default : System.out.println("invalid Week"); + } + + } + +} diff --git a/src/controlStatements/TwoLargestNumbersWithTernary.java b/src/controlStatements/TwoLargestNumbersWithTernary.java new file mode 100644 index 0000000..fdbb220 --- /dev/null +++ b/src/controlStatements/TwoLargestNumbersWithTernary.java @@ -0,0 +1,49 @@ +package controlStatements; + +public class TwoLargestNumbersWithTernary { + + public static void main(String[] args) { + + int a=10, b=20, c=30; + + int largest = (a >=b && a>=c) ? a:(b >=a && b>=c) ? b:c; + + int secondLargest = (largest == a) ? (b>=c?b:c) : (largest == b) ? (a>=c?a:c) : (a>=b?a:b); + + System.out.println("Two LArgest Numbers: "+largest+ "and"+secondLargest); + + } + +} + +/* Explanation: + + Finding the largest: + + int largest = (a >= b && a >= c) ? a : (b >= a && b >= c) ? b : c; + + Checks if a is the largest. + + If not, checks if b is the largest. + + Otherwise, c must be the largest. + + Finding the second largest: + + int secondLargest = (largest == a) ? (b >= c ? b : c) : + (largest == b) ? (a >= c ? a : c) : + (a >= b ? a : b); + + Based on who the largest is, compares the other two to find the second largest. + + Printing the result: + + + System.out.println("Two largest numbers: " + largest + " and " + secondLargest); + + ✅ Output for a = 10, b = 20, c = 30: + + + Two largest numbers: 30 and 20 + +*/ \ No newline at end of file