Jump Statements in Java

What are Jump Statements in Java?

जम्प Statement Control Statement होते हैं जो Program execution को एक location से दूसरे location पर ले जाते हैं। Jump Statement का उपयोग program control को unconditionally के भीतर एक location से दूसरे location पर transfer करने के लिए किया जाता है। Jump Statement का उपयोग आमतौर पर Loop या switch-case को अचानक समाप्त करने के लिए किया जाता है।

Jump Statements in Java

  • Break Statement
  • Continue Statement
  • Return Statement

Break Statement

Break Statement का उपयोग loop से बाहर निकलने और switch cases करने के लिए किया जाता है। जब control encounters में Break Statement का सामना करता है, तो यह loop के execution को समाप्त कर देता है और remaining statements के साथ जारी रहता है, यदि कोई हो।

public class Main 
{
  public static void main(String args[])
  {
      int i;
      for(i = 1; i <= 10; i++)
      {
      if (i == 5) break;
      System.out.print(i + "  ");
      }
   }
}

Output:

1  2  3  4

Break as a form of Goto

गोटो कीवर्ड C/C++ में आमतौर पर इस्तेमाल किया जाने वाला जम्प स्टेटमेंट है। हालाँकि, जावा गोटो के समान कार्य करने के लिए ब्रेक स्टेटमेंट का उपयोग करता है। जावा में कोड के ब्लॉक की पहचान करने के लिए लेबल का उपयोग किया जा सकता है। अर्धविराम के बाद कोई मान्य पहचानकर्ता एक लेबल का प्रतिनिधित्व करता है। ब्रेक कीवर्ड के बाद लेबल नाम का उल्लेख करके हम लेबल किए गए ब्लॉक से बाहर निकल सकते हैं।

public class Main {
  public static void main(String args[]) {
    
    first: {
      second: {
        third: {
          System.out.println("Before break");
          if (true)
            break second; // break out of second block
            // break third; // to break out of third block
          System.out.println("break third");
        }
        System.out.println("break second");
      }
      System.out.println("break first");
    }
  }
} 

Output:

Before break
break first

Continue Statement

जारी रखें एक जंप स्टेटमेंट है जिसका उपयोग तब किया जाता है जब लूप की वर्तमान पुनरावृत्ति को छोड़ना होता है। यह नियंत्रण को अस्थायी रूप से लूप से बाहर निकलने की अनुमति देता है और शेष कथनों को अनदेखा करता है। इस छलांग के बाद, नियंत्रण उसी लूप के अगले पुनरावृत्ति के साथ तुरंत चलता है।

public class Main 
{
  public static void main(String args[])
  {
      for (int i = 10; i > 0 ;i--)
      {
          if (i%2 == 0)
          continue;
          System.out.print(i+ "  ");
      }
   }
}

output:

9  7  5  3  1

Return Statement

Return Keyword का उपयोग किसी Program के control को उस method से वापस स्थानांतरित करने के लिए किया जाता है जिसे इसे कहा जाता है। चूंकि नियंत्रण प्रोग्राम के एक हिस्से से दूसरे हिस्से में जाता है, इसलिए रिटर्न भी एक जंप स्टेटमेंट है।

public class Main
{
    public static int add(int a, int b){
        int sum = a+b;
        return sum;
    }
    public static void main(String[] args) {
        int x = 5, y = 10;
        int sum = add(x, y);
        System.out.println("Sum of a and b: " + sum);
    }
}

output:

Sum of a and b: 15

 

Previous articleOperators in Java
Next articleC++ Function

LEAVE A REPLY

Please enter your comment!
Please enter your name here