CONDITIONAL & LOOPING STATEMENTS IN
JAVA
Conditional Statements: Used to make decisions based on conditions.
Looping Statements: Used to repeat a block of code multiple times.
IF Statement
Definition: Executes block only when condition is true.
Syntax: if(condition){ statements }
Use Case: Checking conditions like positive number, eligibility etc.
Example Program:
import [Link];
public class IfExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number:");
int n = [Link]();
if(n > 0){
[Link]("Number is Positive");
}
if(n % 2 == 0){
[Link]("Number is Even");
}
if(n > 50){
[Link]("Number greater than 50");
}
[Link]("End of program");
}
}
Output: Input: 60 → Positive, Even, Greater than 50
IF-ELSE Statement
Definition: Executes one block if true otherwise another.
Syntax: if(condition){ } else { }
Use Case: Used in decision making like pass/fail or positive/negative.
Example Program:
import [Link];
public class IfElseExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number:");
int n = [Link]();
if(n >= 0){
[Link]("Positive Number");
} else {
[Link]("Negative Number");
}
if(n % 2 == 0){
[Link]("Even Number");
} else {
[Link]("Odd Number");
}
[Link]("Program finished");
}
}
Output: Input: -3 → Negative, Odd
NESTED IF Statement
Definition: An if inside another if.
Syntax: if(condition){ if(condition){ } }
Use Case: Used when multiple conditions depend on each other.
Example Program:
import [Link];
public class NestedIfExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter age:");
int age = [Link]();
if(age >= 18){
if(age >= 60){
[Link]("Senior Citizen");
} else if(age >= 30){
[Link]("Adult");
} else {
[Link]("Young Adult");
}
} else {
[Link]("Minor");
}
[Link]("End");
}
}
Output: Input: 35 → Adult
SWITCH Statement
Definition: Selects one block from multiple cases.
Syntax: switch(expression){ case value: break; }
Use Case: Used in menu-driven programs.
Example Program:
import [Link];
public class SwitchExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter choice:");
int ch = [Link]();
switch(ch){
case 1:
[Link]("Addition");
break;
case 2:
[Link]("Subtraction");
break;
case 3:
[Link]("Multiplication");
break;
default:
[Link]("Invalid Choice");
}
[Link]("End of program");
}
}
Output: Input: 2 → Subtraction
FOR LOOP
Definition: Repeats code with initialization, condition and update.
Syntax: for(init; condition; update)
Use Case: Used when number of iterations is known.
Example Program:
public class ForLoopExample {
public static void main(String[] args) {
int sum = 0;
for(int i=1; i<=10; i++){
[Link]("Number: " + i);
sum = sum + i;
}
[Link]("Sum = " + sum);
[Link]("Loop finished");
}
}
Output: Prints 1–10 and Sum = 55
WHILE LOOP
Definition: Executes while condition is true.
Syntax: while(condition){ }
Use Case: Used when iterations are not fixed.
Example Program:
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
int sum = 0;
while(i <= 10){
[Link](i);
sum = sum + i;
i++;
}
[Link]("Sum = " + sum);
[Link]("Loop ended");
}
}
Output: Prints 1–10 and Sum = 55
DO-WHILE LOOP
Definition: Executes at least once before checking condition.
Syntax: do{ }while(condition);
Use Case: Used when loop must run at least once.
Example Program:
public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
int sum = 0;
do{
[Link](i);
sum = sum + i;
i++;
}while(i <= 10);
[Link]("Sum = " + sum);
[Link]("Loop finished");
}
}
Output: Prints 1–10 and Sum = 55