0% found this document useful (0 votes)
19 views5 pages

Java Control Flow Statements Guide

The document contains a list of Java programming examples demonstrating various control flow statements including IF...Else, Switch Case, While Loop, Do While Loop, For Loop, Enhanced For Loop, Break Statement, and Continue Statement. Each example is encapsulated in a separate class within the 'Lec_1' package, showcasing how to implement these constructs in Java. The examples cover basic functionalities such as checking number signs, iterating through numbers, and controlling loop execution.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Java Control Flow Statements Guide

The document contains a list of Java programming examples demonstrating various control flow statements including IF...Else, Switch Case, While Loop, Do While Loop, For Loop, Enhanced For Loop, Break Statement, and Continue Statement. Each example is encapsulated in a separate class within the 'Lec_1' package, showcasing how to implement these constructs in Java. The examples cover basic functionalities such as checking number signs, iterating through numbers, and controlling loop execution.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program List 2

1) IF…Else:

package Lec_1;

public class if_else


{

public static void main(String[] args)


{
int number = 5;

if (number > 0)
{
[Link](number + " is positive.");
}

else if (number < 0)


{
[Link](number + " is negative.");
}

else
{
[Link]("The number is zero.");
}

2) Switch Case:

package Lec_1;

public class switch_case {

public static void main(String[] args) {


int day=13; // Input: day of the week (1 =
Monday, 2 = Tuesday, etc.)
switch (day)
{
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid day");

3) While Loop:

package Lec_1;

public class while_loop {

public static void main(String[] args) {


int even = 2;
int odd = 1;

[Link]("Even Numbers from 1 to 20:");

while (even <= 20)


{
[Link](even + " ");
even += 2;
// even = even + 2
}

[Link]();

[Link]("Odd Numbers from 1 to 20:");

while (odd <= 20)


{
[Link](odd + " ");
odd += 2; // Increment by 2 to get the next
odd number
}
}

4) Do While Loop:

package Lec_1;

public class do_while {

public static void main(String[] args) {


int i = 1;

do
{
[Link](i);
i++;

} while(i <= 5);


}
}

5) For Loop:

package Lec_1;

public class for_loop {

public static void main(String[] args) {


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

6) For Each loop (Enhanced For Loop):

package Lec_1;

public class for_each_loop {

public static void main(String[] args) {

int[] numbers = {10, 20, 30, 40, 50};

for (int storage_1 : numbers)


{
[Link](storage_1);
}
}
}

7) Break Statement:

package Lec_1;

public class break_stat {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++)


{
if (i == 7)
{
break;
}
[Link]("i = " + i);
}

[Link]("Loop exited.");
}

}
8) Continue Statement:

package Lec_1;

public class conti_stat {

public static void main(String[] args) {

for(int i = 1; i < 10 ; i++)


{
if(i == 6)
{
[Link]("Continue
statement");
continue;
}
[Link]("i = " + i);
}
}

You might also like