Loop Constructs
for Loop
Syntax:
for (initialization; condition; update) {
// statements
}
The initialization typically starts at 0. This structure is ideal when the number of iterations is known in advance.
while Loop
Syntax:
while (condition) {
// statements
}
Unlike for, while only includes a condition check. Initialization and update must be handled separately before or with in the loop body.
do-while Loop
Syntax:
do {
// statements
} while (condition);
This ensures the block executes at least once, even if the condition is false initially.
Increment and Decrement Operators
Consider ++variable (prefix) and variable++ (postfix):
++variable: Increments the value first, then returns the updated value. Used in expressions where the new value is needed immediately.
Example:
int num = 99;
int result = ++num; // result = 100, num = 100
variable++: Returns the current value first, then increments it.
Example:
int num = 99;
int result = num++; // result = 99, num = 100
Decrement (--) follows the same logic.
Wrapper Classes
Java wraps primitive types into objects via wrapper classes to enable object-oriented operations. These classes provide utility methods and support generic collections.
| Wrapper Class | Primitive Type |
|---|---|
| Integer | int |
| Long | long |
| Short | short |
| Byte | byte |
| Double | double |
| Float | float |
| Character | char |
| Boolean | boolean |
Wrapping allows primitives to be used in contexts requiring objects.
Autoboxing and Unboxing
Automatic conversion between primitives and their wrapper classes:
- Autoboxing:
int age = 25;
Integer wrappedAge = age; // Automatically converted to Integer
- Unboxing:
Integer wrappedAge = 30;
int age = wrappedAge; // Extracted back to int
Type Conversion
-
Implicit Casting: Smaller type values are automatically promoted to larger types during arithmetic operations.
-
Explicit Casting: Forces a conversion from larger to smaller types using parentehses. May lead to precision loss.
Example:
int largeValue = 200;
byte smallValue = (byte) largeValue; // Truncates to fit byte range
System.out.println(smallValue); // Output: 200 (if no overflow)
Problem Statement
Calculate the sum of two integers a and b, but input format varies.
Input Format
- First line: an integer
N, indicating the number of test cases. - Next
Nlines: each contains two integersaandbseparated by a space.
Output Format
- For each pair
(a, b), printa + bon a separate line.
Solution Using for Loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = scanner.nextInt();
for (int i = 0; i < count; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + b);
}
scanner.close();
}
}
Solution Using while Loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
while (n > 0) {
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + b);
n--;
}
scanner.close();
}
}