0% found this document useful (0 votes)
30 views14 pages

Unit-4 Loops (Java)

Loops allow code to be repeatedly executed. The document discusses various looping statements in Java like while, for, do-while loops. It provides examples to print numbers from 1 to n using these loops. The continue and break statements are explained which allow skipping/terminating the current loop iteration early. Nested loops are also demonstrated with examples to print patterns using nested for loops.

Uploaded by

Parth Nakum
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)
30 views14 pages

Unit-4 Loops (Java)

Loops allow code to be repeatedly executed. The document discusses various looping statements in Java like while, for, do-while loops. It provides examples to print numbers from 1 to n using these loops. The continue and break statements are explained which allow skipping/terminating the current loop iteration early. Nested loops are also demonstrated with examples to print patterns using nested for loops.

Uploaded by

Parth Nakum
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/ 14

Chap-4

Introduction to loop
Repeatedly execute a block of statements
Looping Statements
• Sometimes we need to repeat certain actions several times or till the some criteria is satisfied.
• Loop constructs are used to iterate a block of statements several times.
• Loop constructs repeatedly execute a block of statements for a fixed number of times
or tillsome condition is satisfied
• Following are looping statements in any programming language,
o Entry Controlled while, for
o Exit Controlled do…while
o Unconditional Jump goto (It is advised to never use goto in a program)

Entry Controlled Loop: While


• While is an entry controlled loop.
• It executes a block of statements till the condition is true.
while(condition)
{
// true-block
}

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

• If the number of iteration is not fixed, it is recommended to use while loop.


//code will print 1 to 9
1. public class WhileLoopDemo {
2. public static void main(String[] args) {
3. int number = 1;

4. while(number < 10) {


5. System.out.println(number);
6. number++;
7. }
8.
9. } }
WAP to print day based on number entered
1. public class SwitchExampleDemo {
2. public static void main(String[] args)
3. {
4. int number = 20;
5. switch (number) {
6. case 10:
7. System.out.println("10");
8. break;
9. case 20:
10. System.out.println("20");
11. break;
12. default:
13. System.out.println("Not 10 or 20");
14. }//switch
15. }
16. }

WAP to print odd numbers between 1 to n


1. import java.util.*;
2. class WhileDemo{
3. public static void main (String[] args){
4. int n,i=1;
5. Scanner sc = new Scanner(System.in);
6. System.out.print("Enter a number:");
7. n = sc.nextInt();
8. while(i <= n){
9. if(i%2==1)
10.
System.out.println(i);
11. i++;
12. }
13. }}
WAP to print factors of a given number
1. import java.util.*;
2. class WhileDemo{
3. public static void main (String[] args){
4. int i=1,n;
5. Scanner sc = new Scanner(System.in);
6. System.out.print("Enter a Number:");
7. n = sc.nextInt();
8. System.out.print(" Factors:");
9. while(i <= n){
10. if(n%i == 0)
11. System.out.print(i +",");
12. i++;
13. }
14. }}

Entry Controlled Loop: for (;;) Loop


• for is an entry controlled loop
• Statements inside the body of for are repeatedly executed till the condition is true
for (initialization; condition; increment/decrement)
{
// statements
}

for(i=1; i <= 5; i++)


{
System.out.print("Hello World!");
}
• The initialization statement is executed only once, at the beginning of the loop.
• Then, the condition is evaluated.
o If the condition is true, statements inside the body of for loop are executed
o If the condition is false, the for loop is terminated.
• Then, increment / decrement statement is executed
• Again the condition is evaluated and so on so forth till the condition is true.
• If the number of iteration is fixed, it is recommended to use for loop.
//code will print 1 to 9
1. public class ForLoopDemo {
2. public static void main(String[] args){
3. for(int number=1;number<10;number++)
4. {
5. System.out.println(number);
6. }
7. }
8. }
WAP to print odd numbers between 1 to n
1. import java.util.*;
2. class MyProgram{
3. public static void main (String[] args){
4. int i=1;
5. Scanner sc = new Scanner(System.in);
6. n = sc.nextInt();
7. for(i=1; i<=n; i++) {
8. if(i%2==1)
9. System.out.println(i);
10. }//for
11. }//
12. }

WAP to print factors of a given number


1. import java.util.*;
2. class MyProgram{
3. public static void main (String[] args){
4. int i=1;
5. Scanner sc = new Scanner(System.in);
6. n = sc.nextInt();
7. for(i=1; i<=n; i++){
8. if(n%i == 0)
9. System.out.println(i);
10. }
11. }
12. }
Exit Controlled Loop: do…while
• do…while is an exit controlled loop.
• do-while loop is executed at least once because condition is checked after loop body.
• Statements inside the body of do…while are repeatedly executed till the condition is true.
• while loop executes zero or more times, do…while loop executes one or more times.
do
{
// true-block
}
while(condition) ;
//code will print 1 to 9
1. public class DoWhileLoopDemo {
2. public static void main(String[] args) {
3. int number = 1;
4. do {
5. System.out.println(number);
6. number++;
7. }while(number < 10) ;
8. }
9. }

WAP to print 1 to 10 using do-while loop


1. import java.util.*;
2. class MyProgram{
3. public static void main (String[] args){
4. int i=1;
5. Scanner sc = new Scanner(System.in);
6. n = sc.nextInt();
7. for(i=1; i<=n; i++){
8. if(n%i == 0)
9. System.out.println(i);
10. }
11. }
12. }
Continue: Skip the statement in the iteration
• Sometimes, it is required to skip the remaining statements in the loop and continue with
thenext iteration.
• continue statement is used to skip remaining statements in the loop.
• continue is keyword in java.
WAP to calculate the sum of positive numbers. •
1. import java.util.*;
2. class ContinueDemo{
3. public static void main(String[] args) {
4. int a,n,sum=0;
5. Scanner sc = new Scanner(System.in);
6. n = sc.nextInt();
7. for(int i=0;i<n;i++){
8. a = sc.nextInt();
9. if(a<0){
10. continue;
11. System.out.println("a="+a); //error:unreachable
statement
12. }//if
13. sum=sum+a;
14. }//for
15. System.out.println("sum="+sum);
16. }
17. }

Break: Early exit from the loop


• Sometimes, it is required to early exit the loop as soon as some situation occurs.
• E.g. searching a particular number in a set of 100 numbers. As soon as the number is
found itis desirable to terminate the loop.
• break is keyword in java.
• break statement is used to jump out of a loop.
• break statement provides an early exit from for, while, do…while and switch constructs.
• break causes exit from the innermost loop or switch.
WAP to calculate the sum of given numbers. User will enter -1 to •
terminate.
1. import java.util.*;
2. class BreakDemo{
3.
4. publicintstatic
a,sum=0;void main (String[] args){
5. System.out.println("enter numbers_ enter -1 to break");
6. Scanner sc = new Scanner(System.in);
7. while(true){
8. a = sc.nextInt();
9. if(a==-1)
10. break;
11. sum=sum+a;
12. }//while
13. System.out.println("sum="+sum);
14. }
15. }
Nested loop
loop within a loop

WAP to print given pattern (nested loop)


1. public static void main(String[] args) {
2. int n=5;
3. for(int i=1;i<=n;i++){
4. for(int j=1;j<=i;j++){
5. System.out.print("*");
6. }//for j
7. System.out.println();
8. }//outer for i
9. }
10. }
WAP to print given pattern (nested loop)
1. class PatternDemo{
2. public static void main(String[] args) {
3. int n=5;
4. for(int i=1;i<=n;i++){
5. for(int j=1;j<=i;j++){
System.out.print(j+"\t");
6. }//for j
7. System.out.println();
8. }//outer for i
9. }
10. }
Pattern Programs in Java
---------------------------------------------------------------------***----------------------------------------------------------------------
Display Patterns in java Program 3:

Program 1: class Pattern3{


public static void main(String args[]){
class Pattern1{ for(int i=5;i>=1;i--){
public static void main(String args[]){ for(int j=1;j<=i;j++){
for(int i=1;i<=5;i++){ System.out.print(i);
for(int j=1;j<=i;j++){ }
System.out.print("* "); System.out.println();
} }
System.out.println(); }
} }
}
} Output:3

Output:1

Program 4:
Program 2:
class Pattern4{
class Pattern2{ public static void main(String args[]){
public static void main(String args[]){ for(int i=5;i>=1;i--){
for(int i=1;i<=5;i++){ for(int j=1;j<=i;j++){
for(int j=1;j<=i;j++){ System.out.print("* ");
System.out.print(i); }
} System.out.println();
System.out.println(); }
} }
} }
} Output:4

Output:2
Program 5: Program 7:

class Pattern5{ class Pattern7{


public static void main(String args[]){ public static void main(String args[]){
int sp=20; int sp=20;
for(int i=1;i<=5;i++){ int i,j,k;
for(int k=1;k<=sp;k++){ for( i=1;i<=5;i++){
System.out.print(" "); for( k=1;k<=sp;k++){
} System.out.print(" ");
sp--; }
for(int j=1;j<=i;j++){ sp--;
System.out.print("* "); for( j=1;j<=i;j++){
} System.out.print("* ");
System.out.println(); }
} System.out.println();
} }
} sp=20;
for(i=4;i>=1;i--){
Output:5 for(k=4;k<=sp;k++){
System.out.print(" ");
}
sp++;
for(j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println();
}
}
Program 6: }

class Pattern6{ Output:7


public static void main(String args[]){
int sp=20;
for(int i=5;i>=1;i--){
for(int k=5;k<=sp;k++){
System.out.print(" ");
}
sp++;
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println(); Program 8:
}
} class Pattern8{
} public static void main(String args[]){
int n, c, k, space, count = 1;
Output:6 space =n=5;
for (c = 1; c <= n; c++){
for (k = 1; k < space; k++)
System.out.print(" ");
for (k = 1; k <= c; k++){
System.out.print("*");
if (c > 1 && count < c){
System.out.print("A");
count++;
} else
} c = c*(i-j+1)/j;
System.out.print("\n"); System.out.print(" "+c);
space--; System.out.print("\n");
count = 1; }
} }
} }
}
Output:10
Output:8

Program 9: Program 11:

class Pattern9{ class Pattern11{


public static void main(String args[]){
public static void main(String args[]){
int rows, i, j, number= 1; int i, space, rows, k=0, count = 0, count1 = 0;
rows=4; rows=5;
for(i=1; i<=rows; ++i) {
for(i=1; i <= rows; i++){
for(j=1; j <= i; ++j){ for(space=1; space <= rows-i; ++space) {
System.out.print(" ")
System.out.print(number);
++number; ++count;
} }
System.out.print("\n"); while(k != 2*i-1) {
} if (count <= rows-1) {
} System.out.print(" "+(i+k));
++count;
}
}
Output:9 else{
++count1;
System.out.print(" "+(i+k-2*count1));
}
++k;
}
count1 = count = k = 0;
System.out.print("\n");
}
}
}
Program 10:
Output:11
class Pattern10{
public static void main(String args[]){
int rows, c = 1, space, i, j;rows=5;
for(i=0; i<rows; i++){
for(space=1; space <= rows-i; space++)
System.out.print(" ")
if (j==0 || i==0)
c = 1;
Program 12: System.out.print("*");
}
class Pattern12{ sp = sp + 1;
public static void main(String args[]){ System.out.print("\n");
for(int i=1;i<=5;i++){ }
for(int j=1;j<=5;j++){ }
System.out.print("* "); }
}
System.out.println(); Output:14
}
}
}

Output:12

Program 15:
class Pattern15{
Program 13: public static void main(String args[]) {
int i,j,k,sp=1;
class Pattern13{ for (i=1; i<=5; i++) {
public static void main(String args[]){ for (k=sp; k<=5; k++) {
for (int i=1; i<=5; i++) { System.out.print(" ");
for (int j=5; j>=i; j--) { }
System.out.print(" "); for (j=0; j< i; j++) {
} System.out.print("*");
for (int k=1; k<=i; k++) { }
System.out.print("*"); sp = sp + 1;
} System.out.print("\n");
System.out.print("\n"); }
} sp = 1;
} for (i=4; i>=1; i--) {
} for (k=sp; k>=0; k--) {
Output:13 System.out.print(" ");
}
for (j=i; j>=1; j--) {
System.out.print("*");
}
sp = sp + 1;
System.out.print("\n");
}
}
}
Program 14: Output:15

class Pattern14{
public static void main(String args[]) {
int i,j,k,sp=1;
for (i=5; i>=1; i--) {
for (k=sp; k>=0; k--) {
System.out.print(" ");// only 1 space
}
for (j=i; j>=1; j--) {
Program 16: Program 18:

class Pattern16{ class Pattern18{


public static void main(String args[]) { public static void main(String args[]) {
int i, j=5, k, x; int i,j,k;
for (i=1;i<=5;i++) { for (i=1;i<=5;i++) {
for (k=1;k<=j;k++) { for (j=1;j<=5;j++) {
System.out.print(" "); if(j<=i)
} System.out.print(j);
for (x=1;x<=i;x++) { else
System.out.print(i); System.out.print(" ");
System.out.print(" "); }
} for (j=5;j>=1;j--) {
System.out.print("\n"); if(j<=i)
j=j-1; System.out.print(j);
} else
} System.out.print(" ");
} }
System.out.print("\n");
Output:16 }
}
}
Output:18

Program 17:
class Pattern17{
public static void main(String args[]) {
int i,j,k;
Program 19:
for (i=1;i<=5;i++) {
for (j=5;j>=1;j--) { class Pattern19{
if(j<=i) public static void main(String args[]) {
System.out.print(j); int i,j,k;
else for (i=1;i<=5;i++) {
System.out.print(" "); j=i;
} for (k=1;k<=i;k++) {
System.out.print("\n"); System.out.print(j++);
} }
} System.out.print("\n");
} }
}
Output:17
}
Output:19
Program 20: for (j=i;j<5;j++) {
System.out.print(" ");
class Pattern20{ }
public static void main(String args[]) { for (k=1;k<(i*2);k++) {
int i,j; System.out.print(k);
for (i=1;i<=4;i++) { }
for (j=i;j>1;j--) System.out.print("\n");
System.out.print(j); }
for (j=1;j<=i;j++) for (i=4;i>=1;i--) {
System.out.print(j); for (j=5;j>i;j--) {
System.out.print("\n"); System.out.print(" ");
} }
} for (k=1;k<(i*2);k++) {
} System.out.print(k);
Output:20 }
System.out.print("\n");
}
}
}
Output:22

Program 21:
class Pattern21{
public static void main(String args[]) {
int i,j;
for (i=1;i<=5;i++) {
for (j=1;j<=5;j++) { Program 23:
if(j==5 || j==1 || i==1 || i==5) class Pattern23{
System.out.print("1"); public static void main(String args[]) {
else
int i,j;
System.out.print(" "); for (i=1;i<=10;i++) {
}
for (j=1;j<=i;j++) {
System.out.print("\n");
System.out.print(i*j);
} }
} System.out.print("\n");
} }
Output:21 }
}
Output:23

Program 22:
class Pattern22{
public static void main(String args[]) {
int i, j, k;
for (i=1;i<=5;i++) {
Program 24: Output:25

class Pattern24{
public static void main(String args[]) {
int i,j,r,k=1;
r=10;
System.out.print("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++){
for(j=1;j<=i;j++,k++)
System.out.print(k);
System.out.print("\n");
}
}
}

Output:24

Program 25:
class Pattern25{
public static void main(String args[]) {
int i, j, rows;
/* Input number of rows from user */
rows=8;
for(i=1; i<=rows; i++) {
/* Print trailing spaces */
for(j=1; j<=rows-i; j++) {
System.out.print(" ");
}
/* Print stars and center spaces */
for(j=1; j<=rows; j++) {
if(i==1 || i==rows || j==1 || j==rows)
System.out.print("*");
else
System.out.print(" ");
}
System.out.print("\n");
}
}
}

You might also like