0% found this document useful (0 votes)
15 views15 pages

Understanding the 'Continue' Statement in Java

The document explains the usage of the 'continue' statement in Java, which allows the program to skip the current iteration of a loop and proceed to the next one. It provides examples of how to implement 'continue' in different loop structures and demonstrates its application in various programming scenarios. Additionally, it includes assignments for practicing the concept of skipping invalid inputs in user-defined calculations.

Uploaded by

aspotkar
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)
15 views15 pages

Understanding the 'Continue' Statement in Java

The document explains the usage of the 'continue' statement in Java, which allows the program to skip the current iteration of a loop and proceed to the next one. It provides examples of how to implement 'continue' in different loop structures and demonstrates its application in various programming scenarios. Additionally, it includes assignments for practicing the concept of skipping invalid inputs in user-defined calculations.

Uploaded by

aspotkar
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/ 15

‘continue’

▪ Jump statements transfer the flow of control in a


Java program.

▪ Although programs can be written without using


the Jump statements, using them gives more clarity
to programs and helps a programmer to trace the
flow of control in complex and lengthy programs.
At the end of this session

 Understand the usage of the ‘continue’


statement
 Trace the output of a program using the dry
run technique
 Apply the ‘continue’ statement in a program
 Solve a program without using ‘continue’
 Execute a program using the ‘continue’
statement
 The “continue” statement continues with the
next iteration skipping the rest of the loop
statements in the loop body.

 In the case of for loop, “continue” transfers the


control to the update statement in the for loop
and then evaluates the test-expression.

 In the case of “do while” and “while” loop, the


program control passes to the test-expression.

 The next iteration is executed only if the test


expression evaluates to true.
A program that uses the
‘continue’ statement
1
*********
2
*********
3
*********
4
*********
5
*********
6
7
8
$$$$$$$$
9
$$$$$$$$
10
$$$$$$$$
42
86400
int x=0, sum=0,p=1; // method 1 without ‘continue’
while(++x<=10)
{
System.out.println(x);
if(x<=5)
{
sum=sum+x;
p=p*x ;
System.out.println(“*********”);
}
else if(x>=8)
{
sum=sum+x;
p=p*x
System.out.println(“$$$$$$$$”);
} // the green lines are repeated twice
}// end of while
System.out.println(sum);
System.out.println(p);
int x=0, sum=0,p=1; // method 1 with ‘continue’
while(++x<=10)
{
System.out.println(x);
if(x<=5)
System.out.println(“*********”);
else if(x>=8)
System.out.println(“$$$$$$$$”);
else
continue ; // if x is 6 or 7, rest of the statements in the loop are skipped
sum=sum+x; // these two statements are executed only when
p=p*x ; // x<=5 or x>=8
}// end of while
System.out.println(sum);
System.out.println(p);
// Accept 2 numbers (for 10 times) and display their quotient and remainder. If the
divisor or denominator is 0, then skip the process and go to next iteration
import java.util.Scanner;
class numb {
public static void main() {
int x,y;
Scanner in=new Scanner(System.in);
for(int i=1;i<=10;i++)
{
System.out.println("Enter numerator");
x=in.nextInt();
System.out.println("Enter denominator");
y=in.nextInt();
if(y==0)
{
System.out.println("Denominator cannot be 0");
System.out.println(“So calculation not done");
continue; //goes to the update statement in the for loop
}
System.out.println("Quotient :"+x/y);
System.out.println("Remainder :"+x%y);
} // for
} //main
}//class
// Accept 2 numbers (for 10 times) and display their quotient and remainder. If the
divisor or denominator is 0, then the user is prompted to enter the numbers again.
The loop control variable is decremented so that one chance is not lost.
class numb {
public static void main() {
int x,y;
Scanner in=new Scanner(System.in);
for(int i=1;i<=10;i++)
{ System.out.println(“Enter numerator”);
x=in.nextInt();
System.out.println(“Enter denominator”);
y=in.nextInt();
if(y==0)
{
System.out.println(“Denominator cannot be 0”);
System.out.println(“You will be given another chance…enter again”);
i--; // i is decremented, as this iteration should not be counted
continue; //goes to the update statement in the for loop
}
System.out.println(“Quotient :”+x/y);
System.out.println(“Remainder :”+x%y);
} // for
} }
Program to enter 5 numbers and find their sum. If a 0 or a negative number
is entered, then the calculation is skipped and the user is asked to input the
next number. The loop control variable is not incremented.
class eg1_continue {
public static void main()
{
double number, sum = 0.0;
Scanner input = new Scanner(System.in);
for (int i = 1; i < 6; ) {
System.out.print("Enter a number: ");
number = input.nextDouble();

if (number <= 0.0)


continue; // skips the remaining statements and goes to the conditional expression
// as there is no update statement in the loop

++i; //increments only when the user inputs +ve number


sum += number;
}
System.out.println("Sum = " + sum);
}
}
Program to enter 5 numbers and find their sum. If a 0 or a negative number
is entered, the calculation is skipped, but the loop control variable is
incremented.

class eg2_continue {
public static void main() {
double number, sum = 0.0;
Scanner input = new Scanner(System.in);

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


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

if (number <= 0.0)


continue; // skips the remaining statements and goes to the update statement

sum += number;
}
System.out.println("Sum = " + sum);
}
 Assignment:-
WAP to calculate the sum of maximum of 5
positive numbers entered by the user. If the
user enters negative number or zero, it is
skipped from calculation.
END

You might also like