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 :
- /*Here we are trying to print even and odd numbers
- between the given range*/
- import java.util.* ;
- public class MINtech {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // getting lower limit
- System.out.println("Enter lower limit : ");
- int start = sc.nextInt() ;
- // getting upper limit
- System.out.println("Enter uppper limit");
- int end = sc.nextInt();
- //initializing two strings to hold even and odd numbers
- String even ="" ;
- String odd ="" ;
- // using for loop
- for(int i = start ; i<= end ; i++){
- // logic for even numbers
- if(i%2 == 0 ){
- even = even + i + " " ;
- }else{
- odd = odd + i + " " ;
- }
- }
- System.out.println("Even numbers are - " + even);
- System.out.println("Odd numbers are - " + odd);
- }
- }
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 :
- /*Here we are trying to print even and odd numbers
- between the given range*/
- import java.util.* ;
- public class MINtech {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // getting lower limit
- System.out.println("Enter lower limit : ");
- int start = sc.nextInt() ;
- // getting upper limit
- System.out.println("Enter uppper limit");
- int end = sc.nextInt();
- //initializing two strings to hold even and odd numbers
- String even ="" ;
- String odd ="" ;
- // using while loop
- while (start <= end ){
- if(start % 2 == 0){
- even += start + " ";
- }else{
- odd += start + " " ;
- }
- start++ ;
- }
- System.out.println("Even numbers are - "+ even);
- System.out.println("Odd numbers are - " + odd);
- }
- }
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 :
- /*Here we are trying to print even and
- odd numbers between the given range*/
- import java.util.* ;
- public class MINtech {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("provide your input : ") ;
- int n = sc.nextInt();
- do{
- System.out.println("Hello ! and Welcome to MINtech ") ;
- n++ ;
- }while(n<10) ;
- }
- }
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
No comments:
Post a Comment