Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions src/Notes/Conditional Statements
Original file line number Diff line number Diff line change
@@ -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.

20 changes: 20 additions & 0 deletions src/controlStatements/IfCondition4.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

}
32 changes: 32 additions & 0 deletions src/controlStatements/IfCondition5.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

}
30 changes: 30 additions & 0 deletions src/controlStatements/SwitchCase.java
Original file line number Diff line number Diff line change
@@ -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");
}



}

}
29 changes: 29 additions & 0 deletions src/controlStatements/SwitchCase1.java
Original file line number Diff line number Diff line change
@@ -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");
}

}

}
49 changes: 49 additions & 0 deletions src/controlStatements/TwoLargestNumbersWithTernary.java
Original file line number Diff line number Diff line change
@@ -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

*/