[Go to site: main page, start]

0% found this document useful (0 votes)
4 views7 pages

Java Conditional Looping Improved

The document provides an overview of conditional and looping statements in Java, including IF, IF-ELSE, Nested IF, Switch, FOR, WHILE, and DO-WHILE statements. Each statement is defined with syntax, use cases, and example programs demonstrating their functionality. The examples illustrate how to implement these statements to make decisions and repeat code execution based on conditions.

Uploaded by

crisejesus54
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Conditional Looping Improved

The document provides an overview of conditional and looping statements in Java, including IF, IF-ELSE, Nested IF, Switch, FOR, WHILE, and DO-WHILE statements. Each statement is defined with syntax, use cases, and example programs demonstrating their functionality. The examples illustrate how to implement these statements to make decisions and repeat code execution based on conditions.

Uploaded by

crisejesus54
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CONDITIONAL & LOOPING STATEMENTS IN

JAVA
Conditional Statements: Used for decision making 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 multiple independent conditions.

Example Program:
import [Link];

public class IfExample {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter number:");
int n = [Link]();

if(n > 0){


[Link]("Positive");
}

if(n % 2 == 0){
[Link]("Even");
}

if(n > 100){


[Link]("Greater than 100");
}

[Link]("End");
}
}

Output:
Enter number: 120
Positive
Even
Greater than 100
End
IF-ELSE Statement
Definition: Executes one block if true otherwise another.

Syntax:
if(condition){
statements;
}else{
statements;
}

Use Case: Binary decisions like pass/fail.

Example Program:
import [Link];

public class IfElseExample {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter number:");
int n = [Link]();

if(n >= 0){


[Link]("Positive");
} else {
[Link]("Negative");
}

if(n % 2 == 0){
[Link]("Even");
} else {
[Link]("Odd");
}

[Link]("End");
}
}

Output:
Enter number: -5
Negative
Odd
End

Nested IF Statement
Definition: An if inside another if.

Syntax:
if(condition){
if(condition){ }
}

Use Case: Multi-level decision making.

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]("Done");
}
}

Output:
Enter age: 35
Adult
Done

Switch Statement
Definition: Executes one case among many.

Syntax:
switch(expression){
case value: break;
}

Use Case: 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]("Add");
break;
case 2:
[Link]("Subtract");
break;
case 3:
[Link]("Multiply");
break;
default:
[Link]("Invalid");
}

[Link]("End");
}
}

Output:
Enter choice: 2
Subtract
End
FOR LOOP
Definition: Loop with initialization, condition and update.

Syntax:
for(init; condition; update){
statements;
}

Use Case: Fixed iterations.

Example Program:
public class ForExample {
public static void main(String[] args) {

int sum = 0;

for(int i=1;i<=5;i++){
[Link](i);
sum = sum + i;
}

[Link]("Sum="+sum);
[Link]("End");

}
}

Output:
1
2
3
4
5
Sum=15
End

WHILE LOOP
Definition: Executes while condition is true.

Syntax:
while(condition){
statements;
}

Use Case: Unknown iterations.

Example Program:
public class WhileExample {
public static void main(String[] args) {

int i=1;
int sum=0;

while(i<=5){
[Link](i);
sum=sum+i;
i++;
}

[Link]("Sum="+sum);
[Link]("End");

}
}

Output:
1
2
3
4
5
Sum=15
End

DO-WHILE LOOP
Definition: Executes at least once.

Syntax:
do{
statements;
}while(condition);

Use Case: At least one execution required.

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<=5);

[Link]("Sum="+sum);
[Link]("End");
}
}

Output:
1
2
3
4
5
Sum=15
End

You might also like