0% found this document useful (0 votes)
3 views3 pages

Java Conditional Looping Notes-4

The document explains conditional and looping statements in Java, detailing types such as if, if-else, nested if, and switch for conditional statements, and while, do-while, and for loops for looping statements. It provides examples and syntax for each type, along with an integrated Java program demonstrating their usage. The program showcases decision-making and repetition through various statements, producing specific outputs based on conditions and loops.

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)
3 views3 pages

Java Conditional Looping Notes-4

The document explains conditional and looping statements in Java, detailing types such as if, if-else, nested if, and switch for conditional statements, and while, do-while, and for loops for looping statements. It provides examples and syntax for each type, along with an integrated Java program demonstrating their usage. The program showcases decision-making and repetition through various statements, producing specific outputs based on conditions and loops.

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 (Definition)


Conditional statements are used to make decisions in a program. They execute different blocks of
code based on conditions.

Types: if, if-else, nested if, switch

1. if Statement
Definition: Executes block when condition is true.
Syntax: if(condition){...}
Example:
int age = 18;
if(age >= 18){
[Link]("Eligible to vote");
}

2. if-else Statement
Definition: Executes one block if true, else another.
Syntax: if(cond){...} else {...}
Example:
int num = 5;
if(num % 2 == 0){
[Link]("Even");
}else{
[Link]("Odd");
}

3. Nested if
Definition: if inside another if.
Example:
int age = 20;
if(age >= 18){
if(age >= 21){
[Link]("Adult & Eligible for everything");
}
}

4. Switch Statement
Definition: Selects one case among many.
Example:
int day = 2;
switch(day){
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
default: [Link]("Invalid");
}
Looping Statements (Definition)
Loops are used to repeat a block of code multiple times until a condition is satisfied.

1. while Loop
Definition: Entry controlled loop.
Syntax: while(condition){...}
Example:
int i = 1;
while(i <= 3){
[Link](i);
i++;
}

2. do-while Loop
Definition: Exit controlled loop.
Example:
int i = 1;
do{
[Link](i);
i++;
}while(i <= 3);

3. for Loop
Definition: Loop with initialization, condition, update.
Example:
for(int i=1;i<=3;i++){
[Link](i);
}

Integrated Java Program (15 lines)


import [Link].*;
class Demo{
public static void main(String[] args){
int num=5;
if(num>0){
if(num%2==0){
[Link]("Positive Even");
}else{
[Link]("Positive Odd");
}
}

for(int i=1;i<=2;i++){
[Link]("For Loop:"+i);
}

int j=1;
while(j<=2){
[Link]("While Loop:"+j);
j++;
}

int k=1;
do{
[Link]("DoWhile:"+k);
k++;
}while(k<=2);

int day=1;
switch(day){
case 1:[Link]("Monday");break;
default:[Link]("Other");
}
}
}

Output:
Positive Odd
For Loop:1
For Loop:2
While Loop:1
While Loop:2
DoWhile:1
DoWhile:2
Monday

Code Explanation:
This program uses all conditional and looping statements. The if and nested if check number type.
The for, while, and do-while loops demonstrate repetition. The switch selects a case based on
value.

You might also like