[go: up one dir, main page]

0% found this document useful (0 votes)
11 views10 pages

LOOPINGEXERCISES

The document provides 15 examples of using different looping statements in Java such as for, while, do-while loops. It also demonstrates using break, continue and switch statements within loops. The examples range from simple programs that print numbers or strings to more complex ones that calculate sums or accept user input. Overall the examples illustrate the basic usage of common looping constructs in Java.

Uploaded by

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

LOOPINGEXERCISES

The document provides 15 examples of using different looping statements in Java such as for, while, do-while loops. It also demonstrates using break, continue and switch statements within loops. The examples range from simple programs that print numbers or strings to more complex ones that calculate sums or accept user input. Overall the examples illustrate the basic usage of common looping constructs in Java.

Uploaded by

Friel Mado
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

EXAMPLE PROGRAMS FOR LOOPING STATEMENTS

FOR LOOP:

// Printing the string for n number of times

1. class program1 {

public static void main(String[] args) {

int n = 5;

// for loop

for (int i = 1; i <= n; ++i) {

System.out.println("Java is fun");

// Printing the numbers for n number of times

2. class program2 {

public static void main(String[] args) {

int n = 5;

// for loop

for (int i = 1; i <= n; ++i) {

System.out.println(i);

}
// Printing the sum of n number of times

3. class program3 {

public static void main(String[] args) {

int sum = 0;

int n = 1000;

// for loop

for (int i = 1; i <= n; ++i) {

// body inside for loop

sum += i; // sum = sum + i

System.out.println("Sum = " + sum);

// Printing the numbers for n number of times using FOR EACH LOOP

4. class program4 {

public static void main(String[] args) {

// create an array

int[] numbers = {3, 7, 5, -5};

// iterating through the array

for (int number: numbers) {

System.out.println(number);

}
// Program for infinite loop – printing string

5. class program5 {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 10; --i) {

System.out.println("Hello");

6.

// Calculate the sum of all elements of an array using for each loop

class program6 {

public static void main(String[] args) {

// an array of numbers

int[] numbers = {3, 4, 5, -5, 0, 12};

int sum = 0;

// iterating through each element of the array

for (int number: numbers) {

sum += number;

System.out.println("Sum = " + sum);

}
WHILE LOOP

7.

// Program to display numbers from 1 to 5

class program7 {

public static void main(String[] args) {

// declare variables

int i = 1, n = 5;

// while loop from 1 to 5

while(i <= n) {

System.out.println(i);

i++;

8.

// Java program to find the sum of positive numbers

import java.util.Scanner;

class program8 {

public static void main(String[] args) {

int sum = 0;

// create an object of Scanner class

Scanner input = new Scanner(System.in);

// take integer input from the user

System.out.println("Enter a number");

int number = input.nextInt();

// while loop continues

// until entered number is positive


while (number >= 0) {

// add only positive numbers

sum += number;

System.out.println("Enter a number");

number = input.nextInt();

System.out.println("Sum = " + sum);

input.close();

DO-WHILE LOOP

9. // Java Program to display numbers from 1 to 5

import java.util.Scanner;

class program9 {

public static void main(String[] args) {

int i = 1, n = 5;

// do...while loop from 1 to 5

do {

System.out.println(i);

i++;

} while(i <= n);

}
10. // Java program to find the sum of positive numbers

import java.util.Scanner;

class program10 {

public static void main(String[] args) {

int sum = 0;

int number = 0;

// create an object of Scanner class

Scanner input = new Scanner(System.in);

// do...while loop continues

// until entered number is positive

do {

// add only positive numbers

sum += number;

System.out.println("Enter a number");

number = input.nextInt();

} while(number >= 0);

System.out.println("Sum = " + sum);

input.close();

11. SWITCH STATEMENT

// Program to print the statement based on the size

public class program11 {

public static void main(String[] args) {

int number = 29;

String size;

// switch statement to check size

switch (number) {
case 29:

size = "Small";

break;

case 42:

size = "Medium";

break;

// match the value of week

case 44:

size = "Large";

break;

case 48:

size = "Extra Large";

break;

default:

size = "Unknown";

break;

System.out.println("Size: " + size);

12. BREAK STATEMENT

// Program to print the numbers except 4

public class program12 {

public static void main(String[] args) {

// for loop

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

// if the value of i is 5 the loop terminates

if (i == 4) {

break;
}

System.out.println(i);

13.

//Program to print the positive numbers and quit the loop when negative number entered

import java.util.Scanner;

public class program13 {

public static void main(String[] args) {

Double number, sum = 0.0;

// create an object of Scanner

Scanner input = new Scanner(System.in);

while (true) {

System.out.print("Enter a number: ");

// takes double input from user

number = input.nextDouble();

// if number is negative the loop terminates

if (number < 0.0) {

break;

sum += number;

System.out.println("Sum = " + sum);

}
14. CONTINUE STATEMENT

// PROGRAM TO DISPLAY THE NUMBERS FROM 1 TO 4 AND 9,10

public class program14{

public static void main(String[] args) {

// for loop

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

// if value of i is between 4 and 9

// continue is executed

if (i > 4 && i < 9) {

continue;

System.out.println(i);

}
15.

// PROGRAM TO DISPLAY THE NUMBERS FROM 1 TO 10 SKIPPING NUMBER - 5

public class program20 {

public static void main(String[] args) {

//for loop

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

if(i==5){

//using continue statement

continue;//it will skip the rest statement

System.out.println(i);

You might also like