Skip to content

Commit 1834626

Browse files
authored
Merge pull request #6 from uvhareesh/develop
Conditional Statements Examples
2 parents 9f310ba + be77dd8 commit 1834626

File tree

6 files changed

+274
-0
lines changed

6 files changed

+274
-0
lines changed

src/Notes/Conditional Statements

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
Java Conditional Statements – Study Material
2+
Overview
3+
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.
4+
5+
Types of Conditional Statements in Java
6+
1. if Statement
7+
Syntax:
8+
9+
java
10+
CopyEdit
11+
if (condition) {
12+
// code to execute if condition is true
13+
}
14+
15+
16+
Usage: Executes a block only if the condition is true.
17+
18+
19+
2. if-else Statement
20+
Syntax:
21+
22+
java
23+
CopyEdit
24+
if (condition) {
25+
// true block
26+
} else {
27+
// false block
28+
}
29+
30+
31+
Usage: Provides an alternate block of code if the condition is false.
32+
33+
34+
3. if-else-if Ladder
35+
Syntax:
36+
37+
java
38+
CopyEdit
39+
if (condition1) {
40+
// block 1
41+
} else if (condition2) {
42+
// block 2
43+
} else {
44+
// default block
45+
}
46+
47+
48+
Usage: Checks multiple conditions sequentially.
49+
50+
51+
4. Nested if
52+
Usage: if statement inside another if block.
53+
54+
55+
Allows more complex decision structures.
56+
57+
58+
59+
Switch Statement
60+
Syntax:
61+
62+
java
63+
CopyEdit
64+
switch (expression) {
65+
case value1:
66+
// code block
67+
break;
68+
case value2:
69+
// code block
70+
break;
71+
default:
72+
// default block
73+
}
74+
75+
76+
Usage: Best used when evaluating one variable against many possible constant values.
77+
78+
79+
Limitations: Works only with byte, short, char, int, enum, and String (Java 7+).
80+
81+
82+
83+
Comparison and Logical Operators
84+
Used in condition checks:
85+
Comparison Operators: ==, !=, <, >, <=, >=
86+
87+
88+
Logical Operators: && (AND), || (OR), ! (NOT)
89+
90+
91+
92+
Practical Examples
93+
Age Validation: Check if a user is eligible to vote.
94+
95+
96+
Grade System: Use of if-else-if ladder to assign grades.
97+
98+
99+
Menu Selection: Switch case to select options like login, register, etc.
100+
101+
102+
103+
Key Points
104+
Conditional statements allow branching logic in programs.
105+
106+
107+
Use if when there's only one condition.
108+
109+
110+
Use switch for cleaner code when checking a single variable against multiple constants.
111+
112+
113+
Always test boundary conditions.
114+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package controlStatements;
2+
3+
public class IfCondition4 {
4+
5+
public static void main(String[] args) {
6+
//Smallest of numbers
7+
8+
int a = 10, b = 20, c = 30;
9+
10+
if (a <= b && a <= c) {
11+
System.out.println("Smallest number is: " + a);
12+
} else if (b <= a && b <= c) {
13+
System.out.println("Smallest number is: " + b);
14+
} else {
15+
System.out.println("Smallest number is: " + c);
16+
}
17+
18+
}
19+
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package controlStatements;
2+
3+
4+
public class IfCondition5 {
5+
6+
public static void main(String[] args) {
7+
//Two Largest Numbers
8+
9+
int a = 10, b = 20, c = 30;
10+
11+
if (a >= b && a >= c) {
12+
if (b >= c) {
13+
System.out.println("Two largest numbers: " + a + " and " + b);
14+
} else {
15+
System.out.println("Two largest numbers: " + a + " and " + c);
16+
}
17+
} else if (b >= a && b >= c) {
18+
if (a >= c) {
19+
System.out.println("Two largest numbers: " + b + " and " + a);
20+
} else {
21+
System.out.println("Two largest numbers: " + b + " and " + c);
22+
}
23+
} else {
24+
if (a >= b) {
25+
System.out.println("Two largest numbers: " + c + " and " + a);
26+
} else {
27+
System.out.println("Two largest numbers: " + c + " and " + b);
28+
}
29+
}
30+
}
31+
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package controlStatements;
2+
3+
public class SwitchCase {
4+
5+
public static void main(String[] args) {
6+
7+
int weekno=1;
8+
switch(weekno)
9+
{
10+
case 1: System.out.println("Sunday");
11+
break;
12+
case 2: System.out.println("Monday");
13+
break;
14+
case 3: System.out.println("Tuesday");
15+
break;
16+
case 4: System.out.println("Wednesday");
17+
break;
18+
case 5: System.out.println("Thursday");
19+
break;
20+
case 6: System.out.println("Friday");
21+
break;
22+
case 7: System.out.println("Saturday");
23+
default: System.out.println("Invalid week");
24+
}
25+
26+
27+
28+
}
29+
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package controlStatements;
2+
3+
public class SwitchCase1 {
4+
5+
public static void main(String[] args) {
6+
7+
String weekName="Sunday";
8+
9+
switch(weekName)
10+
{
11+
case "Sunday": System.out.println(1);
12+
break;
13+
case "Monday": System.out.println(2);
14+
break;
15+
case "Tuesday": System.out.println(3);
16+
break;
17+
case "Wednesday": System.out.println(4);
18+
break;
19+
case "Thursday": System.out.println(5);
20+
break;
21+
case "Friday": System.out.println(6);
22+
break;
23+
case "Saturday": System.out.println(7);
24+
default : System.out.println("invalid Week");
25+
}
26+
27+
}
28+
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package controlStatements;
2+
3+
public class TwoLargestNumbersWithTernary {
4+
5+
public static void main(String[] args) {
6+
7+
int a=10, b=20, c=30;
8+
9+
int largest = (a >=b && a>=c) ? a:(b >=a && b>=c) ? b:c;
10+
11+
int secondLargest = (largest == a) ? (b>=c?b:c) : (largest == b) ? (a>=c?a:c) : (a>=b?a:b);
12+
13+
System.out.println("Two LArgest Numbers: "+largest+ "and"+secondLargest);
14+
15+
}
16+
17+
}
18+
19+
/* Explanation:
20+
21+
Finding the largest:
22+
23+
int largest = (a >= b && a >= c) ? a : (b >= a && b >= c) ? b : c;
24+
25+
Checks if a is the largest.
26+
27+
If not, checks if b is the largest.
28+
29+
Otherwise, c must be the largest.
30+
31+
Finding the second largest:
32+
33+
int secondLargest = (largest == a) ? (b >= c ? b : c) :
34+
(largest == b) ? (a >= c ? a : c) :
35+
(a >= b ? a : b);
36+
37+
Based on who the largest is, compares the other two to find the second largest.
38+
39+
Printing the result:
40+
41+
42+
System.out.println("Two largest numbers: " + largest + " and " + secondLargest);
43+
44+
✅ Output for a = 10, b = 20, c = 30:
45+
46+
47+
Two largest numbers: 30 and 20
48+
49+
*/

0 commit comments

Comments
 (0)