Spex3
    

Java Operators, Control Flow, and Comments


    

    

Java Operators, Control Flow, and Comments
1. Java Operators
Operators in Java perform operations on variables and values.

1.1 Arithmetic Operators
Used for mathematical calculations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (Remainder) a % b
✅ Example:


public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Sum: " + (a + b)); // 15
System.out.println("Difference: " + (a - b)); // 5
System.out.println("Product: " + (a * b)); // 50
System.out.println("Quotient: " + (a / b)); // 2
System.out.println("Remainder: " + (a % b)); // 0
}
}


1.2 Relational (Comparison) Operators
Used to compare values, returning true or false.

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
✅ Example:


public class RelationalExample {
public static void main(String[] args) {
int x = 10, y = 5;
System.out.println(x > y); // true
System.out.println(x < y); // false
System.out.println(x == y); // false
System.out.println(x != y); // true
}
}


1.3 Logical Operators
Used in boolean expressions to combine conditions.

Operator Description Example
&& Logical AND a > b && a > c
` `
! Logical NOT !(a > b)
✅ Example:


public class LogicalExample {
public static void main(String[] args) {
boolean a = true, b = false;
System.out.println(a && b); // false
System.out.println(a || b); // true
System.out.println(!a); // false
}
}


2. Control Flow Statements
Control flow statements help in decision-making and loops.

2.1 If-Else Statement
Executes a block of code based on a condition.

✅ Example:


public class IfElseExample {
public static void main(String[] args) {
int num = 10;

if (num > 0) {
System.out.println("Positive number");
} else {
System.out.println("Negative number");
}
}
}


2.2 Switch Statement
Used when multiple conditions are checked against a single variable.

✅ Example:


public class SwitchExample {
public static void main(String[] args) {
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");
}
}
}


💡 Output: Wednesday

2.3 Loops
Loops execute a block of code multiple times.

1. For Loop
✅ Example:


public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
}
}


💡 Output:


Count: 1
Count: 2
Count: 3
Count: 4
Count: 5


2. While Loop
Executes as long as a condition is true.

✅ Example:


public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Number: " + i);
i++;
}
}
}


3. Do-While Loop
Executes at least once, then checks the condition.

✅ Example:


public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Number: " + i);
i++;
} while (i <= 5);
}
}


3. Comments in Java
Comments help make the code more readable and are ignored by the compiler.

3.1 Single-Line Comment

// This is a single-line comment
int x = 5;


3.2 Multi-Line Comment


/*
This is a
multi-line comment
*/

int y = 10;

3.3 Documentation Comment (for JavaDocs)

/**
* This method prints a greeting message.
*/
public void greet() {
System.out.println("Hello!");
}