Monday, 16 October 2023

JAVA

Loops in JAVA

Lets get started with for loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

syntax :

          for (statement 1; statement 2; statement 3) {
            // code block to be executed
          }
        

Elaborate with a simple example :

 
        
  1. /*Here we are trying to print even and odd numbers
  2. between the given range*/
  3. import java.util.* ;
  4. public class MINtech {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. // getting lower limit
  8. System.out.println("Enter lower limit : ");
  9. int start = sc.nextInt() ;
  10. // getting upper limit
  11. System.out.println("Enter uppper limit");
  12. int end = sc.nextInt();
  13. //initializing two strings to hold even and odd numbers
  14. String even ="" ;
  15. String odd ="" ;
  16. // using for loop
  17. for(int i = start ; i<= end ; i++){
  18. // logic for even numbers
  19. if(i%2 == 0 ){
  20. even = even + i + " " ;
  21. }else{
  22. odd = odd + i + " " ;
  23. }
  24. }
  25. System.out.println("Even numbers are - " + even);
  26. System.out.println("Odd numbers are - " + odd);
  27. }
  28. }

Output

    Enter lower limit : 
    1
    Enter uppper limit
    20
    Even numbers are - 2 4 6 8 10 12 14 16 18 20 
    Odd numbers are - 1 3 5 7 9 11 13 15 17 19 


    Enter lower limit : 
    20
    Enter uppper limit
    100
    Even numbers are - 20 22 24 26 28 30 32 34 36 38 40 
    42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 
    76 78 80 82 84 86 88 90 92 94 96 98 100 

    Odd numbers are - 21 23 25 27 29 31 33 35 37 39 41 
    43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 
    77 79 81 83 85 87 89 91 93 95 97 99 

        

Lets get started with While loop

The while loop loops through a block of code as long as a specified condition is true:

syntax :

          while (condition) {
            // code block to be executed
          }
        

Elaborate with a simple example :

 
        
  1. /*Here we are trying to print even and odd numbers
  2. between the given range*/
  3. import java.util.* ;
  4. public class MINtech {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. // getting lower limit
  8. System.out.println("Enter lower limit : ");
  9. int start = sc.nextInt() ;
  10. // getting upper limit
  11. System.out.println("Enter uppper limit");
  12. int end = sc.nextInt();
  13. //initializing two strings to hold even and odd numbers
  14. String even ="" ;
  15. String odd ="" ;
  16. // using while loop
  17. while (start <= end ){
  18. if(start % 2 == 0){
  19. even += start + " ";
  20. }else{
  21. odd += start + " " ;
  22. }
  23. start++ ;
  24. }
  25. System.out.println("Even numbers are - "+ even);
  26. System.out.println("Odd numbers are - " + odd);
  27. }
  28. }

Output

    Enter lower limit : 
    1
    Enter uppper limit
    20
    Even numbers are - 2 4 6 8 10 12 14 16 18 20 
    Odd numbers are - 1 3 5 7 9 11 13 15 17 19 


    Enter lower limit : 
    50
    Enter uppper limit
    90
    Even numbers are - 50 52 54 56 58 60 62 64 66 
    68 70 72 74 76 78 80 82 84 86 88 90 
    Odd numbers are - 51 53 55 57 59 61 63 65 67 
    69 71 73 75 77 79 81 83 85 87 89 

        

Lets get started with Do/while loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

syntax :

          do {
            // code block to be executed
          }
          while (condition);
        

Elaborate with a simple example :

 
       
  1. /*Here we are trying to print even and
  2. odd numbers between the given range*/
  3. import java.util.* ;
  4. public class MINtech {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. System.out.println("provide your input : ") ;
  8. int n = sc.nextInt();
  9. do{
  10. System.out.println("Hello ! and Welcome to MINtech ") ;
  11. n++ ;
  12. }while(n<10) ;
  13. }
  14. }

Output

  provide your input : 
  5
  Hello ! and Welcome to MINtech 
  Hello ! and Welcome to MINtech 
  Hello ! and Welcome to MINtech 
  Hello ! and Welcome to MINtech 
  Hello ! and Welcome to MINtech 


  provide your input : 
  11
  Hello ! and Welcome to MINtech 


        
https://www.highcpmrevenuegate.com/s4ttwmua2u?key=c02c9252219c2ddb8d72b452016a4796

No comments:

Post a Comment