yt

Header Ads

Chapter 11 What is Loop and Types Explain in Details.

Hi, In this chapter learn what is loop and types of loop with a simple program. 

LOOP STATEMENTS 

 Iteration/ Loop Statements Introduction :- 

कई बार ऐसा हो सकता की आप एक ही statement को बार बार execute करने की जरुरत पड़े। Iteration statements कुछ statements तब तक बार बार execute करते है जब तक की दी हुई condition true होती है। Condition false होने पर ये execution बंद हो जायेगा। जैसे आपको किसी variable की value बार बार print करनी है या करने के लिए आपको बार बार print statements लिखने की आवश्यकता नहीं है। आप सिर्फ एक ही बार iteration statement लिखते है और ये बताते है की इस statement को कितनी बार execute करवाना है।

Iteration statements statement को बार बार execute करवाने के काम आते है। इन्हे looping statements भी कहते है। 
ये 3 प्रकार के होते है । 

1. For Loop:- 

For loop सभी loops में सबसे simple और सबसे ज्यादा यूज़ किये जाने वाला loop है। For loop का structure इस प्रकार होता है। 

for(initialization;condition;increment)

{

//statements

  • For loop के initialization part में आप एक variable को initialize करते है। इसकी value integer/float होती है Depend on you। ये आपके loop का starting point होता है। आप इस variable की जो value देते है, loop वही से शुरू होता है। ध्यान रहे की इस variable को condition में जरूर include किया जाता है नहीं तो loop execute नहीं हो सकता है। 
  • For loop के condition part में आप loop की condition देते है। ये वो condition होती है जिससे की loop terminate होता है। यदि आपने सही condition नहीं दी तो loop infinite तक चलता रहेगा। जब तक ये condition true रहती है तब तक loop चलता रहता है। Condition false होते ही loop terminate हो जायेगा।

  • में Increment part initialized variable को increment किया जाता है ताकि loop का execution आगे बढ़ सके। और जितनी condition दी है, उतना execute हो कर terminate हो जाये। यदि आप variable को increment नहीं करेंगे तो आपका लूप आगे नहीं बढ़ेगा।

An Example: 

class Loop{

public static void main(String args[]){

for(int i=1;i<= 10; i++) 

System.out.println("Numbers form 1 to 10 "+i);

}

}

2. While loop:

While loop, for loop से थोड़ा अलग होता है। While loop में आप initialization statement loop के बाहर देते है। Brackets में केवल condition होती है और increment part block के अंदर होता है। Increment part को भी statements के निचे लिखा जाता है।

class WhileS{

public static void main(String args[]){

int i =1;

while(i<=10){

System.out.println(i);

i++;

}

}

}

While loop का सबसे बड़ा feature ये है की आपको इसमें variable initialize करने की जरुरत नहीं होती है। यदि आप चाहे तो loop को terminate करने के लिए कोई दूसरी condition भी यूज़ कर सकते है। यंहा पर आपके program के variables से related कोई भी condition आ सकती है।

While loop को Bina Statements Ko initialize किये भी यूज़ किया जा सकता है। जबकि for loop के साथ ऐसा नहीं किया जा सकता है।

3. Do-While loop

For और while loop में statements तब execute होते है जब दी हुई condition true होती है। यानि की statements के execute होने से पहले condition check होती है। लेकिन do-while loop में पहले एक बार statements execute होते है उसके बाद | Condition Check की जाती है। ऐसा....

            Syntax:

do

{

// statements

} 

while(condition);

 Note: Here now just explain for your understanding loop and types with syntax: 

            Same as some important topic used in this chapter.

1.Jump statements:- 

Jump statements program में एक जगह में से दूसरी जगह जाने के काम आते है। इनके बारे निचे दिया जा रहा है।

2.Break statements:-

Break statement का प्रयोग आप statement की sequence को break करने, के लिए करते है। जैसा की आपने पहले देखा की break statements को switch statements से exit होने के लिए यूज़ किया गया था। यदि switch cases में break statement यूज़ नहीं किये जाये तो सभी cases के statements execute होंगे। ऐसे ही आप loop से भी exit कर सकते है बिना loop के पूरा हुए। आप सिर्फ एक break statement लगाते है और loop वही पर exit हो जाता है।  break;

3.Continue statement

कई बार ऐसी आवश्यकता होती है की आप आपके loop की किसी iteration को execute नहीं करवाना चाहते है। ऐसी condition में आप उस iteration को skip करने के लिए continue statement यूज़ करते है। जैसे की आप चाहते है की 1 से 100 तक के number print हो लेकिन ये वो ही number होने चाहिए जो 2 के multiple हो । ऐसा करने के लिए आप 1 से 100 तक loop चलाएंगे और loop के अंदर check करेंगे की number 2 से divide हो रहा है या नहीं।

चलाएंगे और loop के अंदर check करेंगे की number 2 से divide हो रहा है या नहीं। यदि number 2 से divide नहीं हो रहा है तो continue statement execute होगा और वो iteration skip हो जाएगी। और next iteration automatically शुरू हो जाएगी।  continue;

4.Return statement

Return statement का यूज़ आप ज्यादातर method को return करने के लिए करते है। जैसे की आप कोई value return कर रहे हो। ये statement method definition में सबसे last में होता है। इस method के यूज़ से program उस method से बाहर आ जाता है। हर method में केवल एक ही return statement होता है और वह एक ही value return कर सकता है।  return;

        So Now Discuss all the important point in java loop in short. 

Loop in Java :

Loop: 

  • Loop is a part of control flow statements.
  • To run the particular block of code continuously until a required condition is fulfill is called looping.
  • Loop is used when there is a need to execute a part of program multiple times.

Some point about all the loop:
  • There is a control variable, called the loop counter.
  • The control variable must be initialized; in other words, it must have an initial value.
  • The increment/decrement of the control variable, which is modified each time the iteration of the loop occurs.
  • The loop condition that determines if the looping should continue or the program should break from it.

1. For Loop in Java:

  • In for loop there are three part initialization,condition and increment/decrement.
  • Initialization part executes only once.
  • Condition part defines the condition for the execution of code block.
  • Increment and decrement part is add or sub of value.
  • All the three part of for loop are optional.
Syntax:-


Example  1:  Print 1 to 10 using for loop 


class Loop1{
    public static void main(String args[]){
        for(int i=1; i<=10; i++){
            System.out.println(i);
        }
    }
}



Example 1.1: Print 10 to 1 using for loop 


class Loop11{
    public static void main(String args[]){
        for(int i=10; i>=1; i--){
            System.out.println(i);
        }
    }
}


Example 1.2:- Print table of any number get by user value.


class Table{
    public static void main(String arr[]){
            // int num=Integer.parseInt(args[]); //used simple methods

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter any number");
        int num = sc.nextInt();
        for(int i=1; i<=10; i++){
            // System.out.print("%d * %d = %d"+num,+i,+num*i);
            System.out.println(num+" * "+i+" = "+num*i);
        }
    }
}


For Your Practice Questions:
Q1. Find Factorial of any number using for loop.
Q2. Find Add of 1 even number from 1 to 100. 
 and more than question used for your practice .


2.While Loop in Java:

  • In While loop there are three part initialization,condition and increment/decrement.
  • Initialization part executes only once.
  • Condition part defines the condition for the execution of code block.
  • Increment and decrement part is add or sub of value.
  • All the three part of for loop are optional.
  • It's body will execute until the given condition is true.
Syntax: 
Some Important Example in While loop:- 

2.1 Write a Program print 1 to 100 using while loop.


class Loop21{
    public static void main(String args[]){
        int i=1;
        while(i<=100){
            System.out.println(i);
            i=i+1;
        }
    }
}



    Explanation :- In the above example the body of while will execute again and again as long as                                  variable X is less then 10. 


2.2 W.A.P print 100 to 1 using while loop.


class Loop21{
    public static void main(String args[]){
        int i=100;
        while(i>=1){
            System.out.println(i);
            i=i-1;
        }
    }
}


    
2.3 W.A.P print find Factorial  of any number.
Some Hints find for factorail:-
   Factorial of  5=1*2*3*4*5
   Factorial of  6=1*2*3*4*5*6
   Factorial of  N=1*2*3*4*5..........*N



class fact2{
    public static void main(String args[]){
    int fact=1;
    int i=1, num=5;
    while(i<=num){
        fact= fact*i;
        i++;
    }
    System.out.print("Factorail value of:- "+fact);

    }
}




Popular Example For Your Practice Set:
  • Practice Set:1-  Display Sum of N natural Numbers.
  • Practice Set:2-  Check Prime yes or not
  • Practice Set:3-  Print the Fibonacci Series 
  • Practice Set:4-  Print Pyramids and Patterns
  • Practice Set:5-  Multiply Two Matrices 
  • Practice Set:6-  Find The Standard Value
  • Practice Set:6-  Find Leap Year Yes or Not

3. Do While Loop in Java :

  • It's body will executes until the given condition is true.

Example 3.1: Find a program using while loop print 1 to 10


class Dowhile{
    public static void main(String args[]){
        int a=1;
        do {
            System.out.println(a);
            a++;
        }while(a<=10);
    }
}


Example 3.1: Find a program using while loop print factorial value  get by user input.



class Dowhile1{
    public static void main(String args[]){
        // Scanner sc= new Scanner(System.in);
        // System.out.println("Enter any number");
        // int num= sc.nextInt();

        // int num=Integer.parseInt(arg[]);  // USed for get user value
        int a=1;
        int fact=1;
        do {
            fact=fact*a;
            a++;
        }while(a<=num);
        System.out.println("Factorail value is "+fact);
    }
}



Difference between while and do while loop:

  • Do-While:- The difference between while and do while loop is that in the case of while loop, if the condition is false it's body will not executes but in the case of do while loop it's body will execute at least one time either condition true or false. 
  • While loop is a entry control loop and do while loop is exit control.  

4. For Each Loop in Java :

  • It is Special type of loop which is used to mostly with array [index_number].
  • Array is Special type of datatype, stored groups of  collocation but of similarly datatype  with declared with index number[].  next chapter discuss on details. 
  • It stores each element of array in a variable and executes the body of loop.
  • It starts with for keyword like a normal for loop.
Syntax: 



Example of For -Each loop: 

Example 4.1 Find 1st program with for each loop 


class Foreach
{
 public static void main(String[] args)
 {
  //array declaration
  int ar[]={10,50,60,80,90};
  for (int value:ar){

   System.out.println(value+" ");      
  }
 }
}



Example 4.1 Find Same program with for 


class Foreach1
{
 public static void main(String[] args)
 {
  //array declaration
  int ar[]={10,50,60,80,90};
  for (int i = 0; i <ar.length; i++)
   System.out.print(ar[i]+" ");      
 }

}


Guys i hope this post is  helpful for you, well i will suggest you please keep practice for lots of program in loop statements then your skills is developed for your future, Thnaks for given your valuable time.

Don't forget try this program:

Popular Example For Your Practice Set:
  • Practice Set:1-  Display Sum of N natural Numbers.
  • Practice Set:2-  Check Prime yes or not
  • Practice Set:3-  Print the Fibonacci Series 
  • Practice Set:4-  Print Pyramids and Patterns
  • Practice Set:5-  Multiply Two Matrices 
  • Practice Set:6-  Find The Standard Value
  • Practice Set:6-  Find Leap Year Yes or Not


No comments

Theme images by Dizzo. Powered by Blogger.