[go: up one dir, main page]

0% found this document useful (0 votes)
120 views8 pages

Oopj

The document discusses the "break" and "continue" statements in Java and compares the "do-while" and "while" loops. The "break" statement terminates the loop immediately, while the "continue" statement skips the current iteration. A "break" can be used with a switch statement but not a "continue". A "while" loop checks the condition first before executing statements, whereas a "do-while" loop executes statements at least once and then checks the condition.

Uploaded by

Aakash Prajapati
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)
120 views8 pages

Oopj

The document discusses the "break" and "continue" statements in Java and compares the "do-while" and "while" loops. The "break" statement terminates the loop immediately, while the "continue" statement skips the current iteration. A "break" can be used with a switch statement but not a "continue". A "while" loop checks the condition first before executing statements, whereas a "do-while" loop executes statements at least once and then checks the condition.

Uploaded by

Aakash Prajapati
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/ 8

“BREAK” vs “CONTINUE”

AND

“DO WHILE vs “WHILE” in JAVA

CREATED BY : AKASH PRAJAPATI


4th SEM CSE B
190950131100

PROGRAMMING IN JAVA
CONTENTS
“BREAK” vs “CONTINUE”
 “BREAK”
 What is “Break” Statement ?
 Syntax of “Break” Statement
 Uses of “Break” Statement
 Understanding “Break” Statement with the
help of an Example
 “CONTINUE”
 What is “Continue” Statement ?
 Syntax of “Continue” Statement
 Uses of “Continue” Statement
 Understanding “Continue” Statement with the
help of an Example

“DO WHILE” LOOP vs “WHILE” LOOP


 “DO WHILE” LOOP
 What is “Do While” loop?
 Syntax of “Do While” loop
 Understanding “Do While” loop with the help
of a Flowchart and an Example
 “While” LOOP
 What is “While” loop?
 Syntax of “While” loop
 Understanding “While” loop with the help of a
Flowchart and an Example
“BREAK” vs “CONTINUE”
“BREAK”
What is “Break” Statement ?
The break statement in java is used to terminate from the loop immediately. When a break
statement is encountered inside a loop, the loop iteration stops there, and control returns from
the loop immediately to the first statement after the loop. Basically, break statements are used
in situations when we are not sure about the actual number of iteration for the loop, or we want
to terminate the loop based on some condition.

Syntax of “Break” Statement


break;

Uses of “Break” Statement


 To exit a loop.
 Used as a “civilized” form of goto.
 Terminate a sequence in a switch statement.
Understanding “Break” Statement with the help of an Example
Using break to exit a loop :
class exit {
public static void main(String[] args)
{
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++) {
// Terminate the loop when i is 5
if (i == 5)
break;
System.out.println(" " + i);
}
System.out.println("Out of Loop");
}
}
“CONTINUE”

What is “Continue” Statement ?


The continue statement in Java is used to skip the current iteration of a loop. We can use
continue statement inside any types of loops such as for, while, and do-while loop. Basically
continue statements are used in the situations when we want to continue the loop but do not
want the remaining statement after the continue statement.

Syntax of “Continue” Statement

continue;

Uses of “Continue” Statement


 To continue a loop.

Understanding “Continue” Statement with the help of an Example


class ap
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
// If the number is 2 skip and continue
if (i == 2)
continue;

System.out.print(i + " ");


}
}
}
“DO WHILE” LOOP vs “WHILE” LOOP
“DO WHILE” LOOP
What is “Do While” loop?
“do while” loop checks for the condition after executing the statements, and therefore is an
example of Exit Control Loop.

Syntax of “Do While” loop


do

statements..

while (condition);

Understanding “Do While” loop with the help of a Flowchart and an


Example
import java.io.*;

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

int i = 3;

do {
i++;
System.out.println("dowhile");
} while (i < 10);
}
}
“While” LOOP
What is “While” loop
A while loop is a control flow statement that allows code to be executed repeatedly based on a
given Boolean condition. The while loop can be thought of as a repeating if statement.

Syntax of “While” loop


while (boolean condition)
{
loop statements...
}

Understanding “While” loop with the help of a Flowchart and an


Example
import java.io.*;

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

int i = 3;

while (i < 10) {


i++;
System.out.println("while");
}
}
}
SUMMARY
“BREAK” vs “CONTINUE”
Break Continue

The break statement is used to terminate the The continue statement is used to skip the
loop immediately. current iteration of the loop.

break keyword is used to indicate break continue keyword is used to indicate continue
statements in java programming. statement in java programming.

We can use a break with the switch We can not use a continue with the switch
statement. statement.

The break statement terminates the whole The continue statement brings the next iteration
loop early. early.

It stops the execution of the loop. It does not stop the execution of the loop.

“DO WHILE” LOOP vs “WHILE” LOOP


While do-while

Condition is checked first then statement(s) is Statement(s) is executed atleast once,


executed. thereafter condition is checked.

It might occur statement(s) is executed zero


times, If condition is false. At least once the statement(s) is executed.

No semicolon at the end of while. Semicolon at the end of while.


while(condition) while(condition);

If there is a single statement, brackets are not


required. Brackets are always required.

Variable in condition is initialized before the variable may be initialized before or within the
execution of loop. loop.

while loop is entry controlled loop. do-while loop is exit controlled loop.

while(condition) do { statement(s); }
{ statement(s); } while(condition);

You might also like