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

Class 9 - Continue - Do While

The document contains a series of Java programming exercises focusing on the use of 'break' and 'continue' statements in loops, including examples for stopping and skipping numbers based on specific conditions. It also includes examples of do-while loops demonstrating their functionality with various outputs. Each section provides code snippets for different scenarios, illustrating the control flow in Java programming.

Uploaded by

dobbywiiaa
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

Class 9 - Continue - Do While

The document contains a series of Java programming exercises focusing on the use of 'break' and 'continue' statements in loops, including examples for stopping and skipping numbers based on specific conditions. It also includes examples of do-while loops demonstrating their functionality with various outputs. Each section provides code snippets for different scenarios, illustrating the control flow in Java programming.

Uploaded by

dobbywiiaa
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
You are on page 1/ 7

Continue Statement

Q1 - Stop numbers when i == 3. Use break.


Students are shown this program:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
System.out.println("Breaking loop at " + i);
break;
}
System.out.println(i);

Q2 - Skip numbers when i == 3. Use continue


And then they are shown this program :
for (int i = 1; i <= 5; i++) {
if (i == 3) {
System.out.println("Skipping iteration at " + i);
continue;
}
System.out.println(i);
}

Q3 - Skip even numbers


public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
// Skip even numbers
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}

Q4 - Skip numbers when i == 4 using while loop


int i =0;
while(i<10){
if(i==4){
i++;
continue;
}
System.out.println(i);
i++;
}

Q5 - Skip numbers when i is greater than 10


public class q5 {
void main() {
for (int i = 0; i <= 20; i++) {
if (i >= 10) {
continue;
}
System.out.println(i);
}
}
}
Q6 - Skip numbers when i==3 or i==7 (Specific Numbers)
public class q5 {
void main() {
for (int i = 0; i <= 10; i++) {
if ((i==3) || (i==7)) {
continue;
}
System.out.println(i);
}
}
}

Q7 - Skip numbers when i is divisible by 2 and Stop when i == 5


public class q7 {
void main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
if(i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}

Q8 - Skip numbers between 4 and 7 (inclusive)


public class q8 {
void main() {
for (int i = 1; i <= 10; i++) {
if (i >= 4 && i <= 7) {
break;
}
System.out.println(i);
}
}
}
Q9 - Skip Numbers Divisible by both 2 and 3
public class q9 {
void main() {
for (int i = 1; i <= 10; i++) {
if (i%2==0 && i%3==0) {
continue;
}
System.out.println(i);
}
}
}

DO WHILE LOOP

Give the output for the programs given below:

Program 1
package com.journaldev.javadowhileloop;

public class JavaDoWhileLoop {


public static void main(String[] args) {

int i = 5;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}

Program 2

class DoWhileLoopExample {
public static void main(String args[]){
int i=10;
do{
System.out.println(i);
i--;
}while(i>1);
}
}

Program 3

public class Test {

public static void main(String args[]) {


int x = 10;
do {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
Program 4

class AssignmentOperator {
public static void main(String[] args) {
int sum = 0, i = 100;
while (i != 0) {
sum += i; // sum = sum + i;
--i;
}
System.out.println("Sum = " + sum);
}
}
Program 5

// Java program to illustrate do-while loop


class dowhileloopDemo
{
public static void main(String args[])
{
int x = 21;
do
{
// The line will be printed even
// if the condition is false
System.out.println("Value of x:" + x);
x++;
} while (x < 20);
}
}

Program 6. To add numbers till 0.0 is not entered.

import java.util.Scanner;

class Sum {
public static void main(String[] args) {

Double number, sum = 0.0;


Scanner input = new Scanner(System.in);

do {
System.out.print("Enter a number: ");
number = input.nextDouble();
sum += number;
} while (number != 0.0);

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


}
}

You might also like