Jump Statements in C++ - break, continue, goto and return

Jump Statements in C++ are used to change the normal flow of program execution. They transfer control from one part of a program to another, depending on the statement used.

C++ में Jump Statements का उपयोग program execution के सामान्य flow को बदलने के लिए किया जाता है। ये अलग-अलग statements के माध्यम से program control को एक स्थान से दूसरे स्थान पर transfer करते हैं।

C++ provides the following jump statements:

  1. break

  2. continue

  3. goto

  4. return

1. break Statement

The break statement is used to terminate the nearest enclosing loop or switch statement immediately. After executing break, program control moves to the statement following that loop or switch.

break statement का उपयोग nearest enclosing loop या switch statement को तुरंत terminate करने के लिए किया जाता है। break execute होने के बाद program control loop या switch के बाद आने वाले statement पर चला जाता है।

Syntax:

break;

Example:

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
break;
cout << i << " ";
}
return 0;
}

Output:

1 2 3 4

Explanation (व्याख्या):

When the value of i becomes 5, the break statement terminates the loop immediately. Therefore, the numbers after 4 are not displayed.

जब i का मान 5 हो जाता है, तब break statement loop को तुरंत terminate कर देता है। इसलिए 4 के बाद की संख्याएँ display नहीं होती हैं।

2. continue Statement

The continue statement is used to skip the remaining statements of the current loop iteration and proceed to the next iteration.

continue statement का उपयोग current loop iteration के शेष statements को skip करके अगली iteration पर जाने के लिए किया जाता है।

Syntax:

continue;

Example:

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
if (i == 3)
continue;
cout << i << " ";
}
return 0;
}

Output:

1 2 4 5

Explanation (व्याख्या):

When i becomes 3, the continue statement skips the cout statement for that iteration. The loop then proceeds with the next iteration.

जब i का मान 3 होता है, तब continue statement उस iteration के लिए cout statement को skip कर देता है। इसके बाद loop अगली iteration के साथ आगे बढ़ता है।

3. goto Statement

The goto statement is used to transfer program control directly to a labelled statement within the same function.

goto statement का उपयोग program control को उसी function के अंदर किसी labelled statement पर सीधे transfer करने के लिए किया जाता है।

Syntax:

goto label;
// Statements
label:
// Statements

Example:

#include <iostream>
using namespace std;
int main()
{
int i = 1;
start:
cout << i << " ";
i++;
if (i <= 5)
goto start;
return 0;
}

Output:

1 2 3 4 5

Explanation (व्याख्या):

The label start marks a position in the program. The goto start statement transfers control back to that label as long as i <= 5 is true.

इस program में start एक label है, जो program में एक position को दर्शाता है। जब तक i <= 5 condition true रहती है, goto start statement control को वापस उसी label पर भेजता है।

Note: The excessive use of goto can make a program difficult to read and maintain. Structured loops such as for, while, and do-while are generally preferred.

goto का अधिक उपयोग program को समझने और maintain करने में कठिन बना सकता है। इसलिए सामान्यतः for, while और do-while जैसे structured loops को प्राथमिकता दी जाती है।

4. return Statement

The return statement is used to terminate the execution of a function and transfer control back to the calling function. It can also return a value from a function.

return statement का उपयोग function का execution समाप्त करके control को calling function में वापस भेजने के लिए किया जाता है। यह function से कोई value भी return कर सकता है।

Syntax:

return;

Or

return expression;

Example:

#include <iostream>
using namespace std;
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(10, 20);
cout << "Sum = " << result;
return 0;
}

Output:

Sum = 30

Explanation (व्याख्या):

The add() function returns the sum of two numbers using return a + b;. The returned value is stored in the result variable. The return 0; statement in main() indicates successful program termination.

add() function, return a + b; के माध्यम से दो संख्याओं का sum return करता है। प्राप्त value को result variable में store किया जाता है। main() में return 0; successful program termination को दर्शाता है।

5. Difference Between break, continue, goto and return

StatementPurpose
breakTerminates the nearest enclosing loop or switch.
continueSkips the remaining statements of the current loop iteration.
gotoTransfers control to a labelled statement in the same function.
returnTerminates the current function and optionally returns a value.

Statement

उपयोग
breakNearest enclosing loop या switch को terminate करता है।
continueCurrent loop iteration के शेष statements को skip करता है।
gotoउसी function के labelled statement पर control transfer करता है।
returnCurrent function को terminate करके optionally value return करता है।

Jump Statements are useful for controlling the flow of execution in C++ programs. The break statement terminates a loop or switch, continue skips the current iteration, goto transfers control to a label, and return exits a function. These statements should be used appropriately to make programs clear and easy to maintain.

C++ programs में execution flow को control करने के लिए Jump Statements उपयोगी होते हैं। break loop या switch को terminate करता है, continue current iteration को skip करता है, goto control को किसी label पर transfer करता है और return function से बाहर निकलता है। Program को clear और maintainable बनाने के लिए इनका उचित उपयोग करना चाहिए।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)