0% found this document useful (0 votes)
53 views

Javalab - 5 and Flow Control: Loops

The document contains 4 Java programs that demonstrate the use of loops and flow control statements. Program 1 uses for, while, and do-while loops to calculate the sum of numbers from 1 to 10, 1 to 5, and 1 to 6 respectively. Program 2 uses a for loop with incremented counter variables to calculate a sum. Program 3 uses a continue statement inside a nested for loop to terminate the outer loop under a certain condition. Program 4 uses a continue statement to skip an iteration of a for loop when the counter equals 2.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
53 views

Javalab - 5 and Flow Control: Loops

The document contains 4 Java programs that demonstrate the use of loops and flow control statements. Program 1 uses for, while, and do-while loops to calculate the sum of numbers from 1 to 10, 1 to 5, and 1 to 6 respectively. Program 2 uses a for loop with incremented counter variables to calculate a sum. Program 3 uses a continue statement inside a nested for loop to terminate the outer loop under a certain condition. Program 4 uses a continue statement to skip an iteration of a for loop when the counter equals 2.
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 2

[] J A V A

JavaLab_5

LAB

LOOPS and Flow Control

%Lab5_1:
public class loop {
public static void main(String[] args) {
int sum1 = 0 ;

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


{
sum1 = sum1 + x;
}
System.out.println(" The sum of 1+2+ ...+10 is " + sum1);
int A = 5, sum2 = 0 ;

while (A >= 1)
{
sum2 = sum2 + A;
A--;
}
System.out.println(" The sum of 1+2+ ...+5 is " + sum2);
int B = 1, sum3 = 0 ;

do {
sum3 += B;
B++ ;
} while (B <= 6) ;
System.out.println(" The sum of 1+2+ ...+6 is " + sum3);
}

%Lab5_2:
public class sum{
public static void main(String args[]){
int i, j;
float sum=0.0f ;
for(i=1, j=2 ; i<=4 && j<=8 ; i++, j+=2 )
sum+=(float) I / j ;
System.out.println("sum=" + sum );
}

------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl

[] J A V A

LAB

%Lab5_3:
public class continueBreak{
public static void main(String args[]){

loop1:

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


{ System.out.println( );

break;

if(i>=10)

for(int j=1;j<100;;j++)
{
System.out.println(*);
if ( j == I )

continue loop1;
}
}
System.out.println(Termination by break);
}

%Lab5_4:
public class sum{
public static void main(String args[]){
int i, sum=0;
for(i=1 ; i<=4 ; i++)
{
if(i==2)

continue;

sum += I ;
}
System.out.println("sum=" + sum );
}

Try this
Write a program that add, subtract, multiply, and divided two numbers, and
display the result.
Write a program to perform the following sum
2

X 3 4 5 28

Write a program to find the sum of the Even numbers between 6 and 88 ?
------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl

You might also like