Vous êtes sur la page 1sur 4

Rizvi College of Engineering OOPM (JAVA) Lab

Name: ……………………………………………..

Department: Computer Engineering

Class & Semester: S.E (SECOND Year) , Sem III

Subject: OOPM (JAVA) Lab


_________________________________________
Expt. No. 02
Title: Write a program to print following
pattern using labeled break and
continue statement.
*
**
***
****
*****

Date:

Subject In-charge Sign:


…………………………….

Computer Department (Sem III) Experiment No. 02 Page | 1


Rizvi College of Engineering OOPM (JAVA) Lab

Experiment No. 02
Aim: Write a program to print following pattern using labeled break and continue statement.

*
**
***
****
*****

Software: jdk1.6
Notepad

Theory:
This covers the following topics:
1) Break and continue statement
2) Algorithm
3) Program
4) Output

 Break and Continue:


The statements break and continue in Java alter the normal control flow of
compound statements. The break statement immediately jumps to the end (and
out) of the appropriate compound statement. The continue statement
immediately jumps to the next iteration (if any) of the appropriate loop. A
continue statement does not apply to a switch statement or a block statement, only
to compound statements that loop: for, while, and do-while.

Java does not have a general goto statement. But the statements break and
continue take the place of most of uses of the goto. Java does allow any
statement to be labeled as in

Label: statement

This is useful only for those compound statements, because break and continue
can target a labeled compound statement. In this case they take the form:

Computer Department (Sem III) Experiment No. 02 Page | 2


Rizvi College of Engineering OOPM (JAVA) Lab

break label;
continue label;

 Algorithm:

Step 1: Start
Step 2: Create a class LabelTest.
Step 3: Start of a class with main method.
Step 4: Initialize a label of break and continue.
Step 5: Start a row loop.
Step 6: Check if (rows==6) jump to break label.
Step 7: Start a column loop.
Step 8: Check if (column>row) jump to continue label.
Else print the pattern.
Step 9: Stop.

 Program:
class LabelTest
{
public static void main(String args[])
{
stop: {
nextRow:
for(int row=1;row<=7;row++)
{ if (row==6)
break stop;
System.out.println();
for(int column=1;column<=10;column++)
{
if(column>row)
continue nextRow;
System.out.print(" * ");
}
}
}
}
}

Computer Department (Sem III) Experiment No. 02 Page | 3


Rizvi College of Engineering OOPM (JAVA) Lab

 Output:

 Conclusion: Thus we have studied and performed Labeled continue and break jump
statements.

Computer Department (Sem III) Experiment No. 02 Page | 4

Vous aimerez peut-être aussi