NAME : SHIVANK PANDEY


CLASS : IX-A


SUBJECT : COMPUTER APPLICATIONS


SCHOOL : CMS, RDSO BRANCH


TOPIC : LOOPS & NESTED LOOPS











LOOPING (WHILE & DO - WHILE)


WHAT IS LOOPING ?


Loops are among the most basic and powerful of programming concepts. A loop in a computer program is an instruction that repeats until a specified condition is reached. In a loop structure, the loop asks a question.


If the answer requires action, it is executed. The same question is asked again and again until no further action is required. Each time the question is asked is called an iteration. A computer programmer who needs to use the same lines of code many times in a program can use a loop to save time. A loop repeatedly executes code in its body until the loop conditional statement becomes false. A loop is divided into two parts :-


Loop Statement : This defines the time limit to be true for the continuous loop that is contingent on the attached conditional statement.


Loop Body : This holds the statement’s code or instruction; it is is executed with each loop cycle.


LOOP CONTROL STRUCTURE


### A statement that alters the execution of a loop from its designated sequence is a loop control statement. Rust & Java, for example, provides two loop control statements :-
### 1. A break statement inside a loop terminates the loop immediately.
### 2. A continue statement jumps to the next iteration of the loop, skipping any code in between.

TYPES OF LOOPS IN JAVA :-

while :

A while loop or indefinite loop is a set of instructions that is repeated as long as the associated logical expression is true. The following is the abstract syntax of a while loop block. When Java reaches a while loop block, it first determines if the logical expression of the while loop is true or false. If the expression is true, the code block will be executed, and after it is executed, the program returns to the logical expression at the beginning of the while statement. If it is false, then the while loop will terminate.

public class whileLoop{
    public static void main(){
        byte control = 90;
        byte times = 0;

     // While loop block.
        while(control > 5){
            control = control - 1;    
            times = times + 1;        
        }
    }
}


FLOWCHART OF A WHILE LOOP :-



do - while :

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in Java programming checks its condition at the bottom of the loop. A do - while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.

public class Main{
    public static void main(){
        byte control = 90;
        byte console = 0;
        do{
            control = control - 1;
            console = console + 1;
        }        
        while(control > 40);
    }
}


FLOWCHART OF A DO - WHILE LOOP :-





PROGRAMS ON LOOPS IN JAVA :

Write a program to print the following pattern in Java :

1
2
3
4
5
// Program to display numbers from 1 to 5

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

    // declare variables
    int i = 1, n = 5;

    // while loop from 1 to 5
    while(i <= n) {
      System.out.println(i);
      i++;
    }
  }
}



Write a program to print the following pattern in Java using do while loop :

1
2
3
4
5
// Java Program to display numbers from 1 to 5

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

    int i = 1, n = 5;

    // do...while loop from 1 to 5
    do {
      System.out.println(i);
      i++;
    } while(i <= n);
  }
}



TYPES OF NESTED LOOPS IN JAVA :-


The loops that consist of another loop inside it as a nest-like structure are built, and the outer loop monitors the number of executions of the inner loop, loops working in such structure where is known as nested loop. It is also known as a loop inside the loop.





NESTED FOR LOOPS : -

A for loop inside of another loop is called a nested for loop.

public class NestedFor{
    public static void main(){
        for(byte i = 1; i < 5; i++){
            for(byte c = 1; c < 5; c++){
                System.out.print(c);
            }
        }
        System.out.print("\n");
    }
}

PROGRAMS BASED ON NESTED FOR LOOPS :-

Write a Program to print the following pattern in Java :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
public class Main {
  public static void main() {

    int rows = 5;

    // outer loop
    for (int i = 1; i <= rows; ++i) {

      // inner loop to print the numbers
      for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
      }
      System.out.print("\n");
    }
  }
}



Write a program to print the following pattern in Java :

****
***
**
*
public class Main{
    public static void main(){
        for(byte i = 1; i <5; i++){
            for(byte c = 5; c > i; c--){
                System.out.print('*');
            }
            System.out.print("\n");
        }
    }
}