📜  C ++中的跳转语句

📅  最后修改于: 2021-06-01 01:02:40             🧑  作者: Mango

如果满足某些条件,则使用跳转语句来操纵程序的流程。它用于终止或继续程序内部的循环或停止函数的执行。在C++中,有四个跳转语句: continuebreakreturngoto

继续它用于执行循环的其他部分,同时跳过条件内声明的某些部分,而不是终止循环,而是继续执行同一循环的下一次迭代。它与决策语句一起使用,该决策语句必须存在于循环内部。该语句可以在for循环whiledo-while内使用 环形。

语法

continue;

程序1:

考虑一个场景,其中所有数字110之间(数字5除外)。因此,在这种情况下,想法是在i的值为5之后使用continue语句。以下是相同的程序:

C++
// C++ program to demonstrate the
// continue statement
#include 
using namespace std;
  
// Driver code
int main()
{
    for (int i = 1; i < 10; i++) {
  
        if (i == 5)
            continue;
        cout << i << " ";
    }
    return 0;
}


C++
// C++ program to demonstrate the
// break statement
#include 
using namespace std;
  
// Driver Code
int main()
{
    for (int i = 1; i < 10; i++) {
  
        // Breaking Condition
        if (i == 5)
            break;
        cout << i << " ";
    }
    return 0;
}


C++
// C++ program to demonstrate the
// return statement
#include 
using namespace std;
  
// Driver code
int main()
{
    cout << "Begin ";
  
    for (int i = 0; i < 10; i++) {
  
        // Termination condition
        if (i == 5)
            return 0;
        cout << i << " ";
    }
    cout << "end";
  
    return 0;
}


C++
// C++ program to demonstrate the return
// statement in void return type function
#include 
using namespace std;
  
// Function to find the greater element
// among x and y
void findGreater(int x, int y)
{
    if (x > y) {
        cout << x << " "
             << "is greater"
             << "\n";
        return;
    }
    else {
        cout << y << " "
             << "is greater"
             << "\n";
        return;
    }
}
  
// Driver Code
int main()
{
    // Function Call
    findGreater(10, 20);
  
    return 0;
}


C++
// C++ program to demonstrate the
// goto statement
#include 
using namespace std;
  
// Driver Code
int main()
{
    int n = 4;
  
    if (n % 2 == 0)
        goto label1;
    else
        goto label2;
  
label1:
    cout << "Even" << endl;
    return 0;
  
label2:
    cout << "Odd" << endl;
}


输出:
1 2 3 4 6 7 8 9

中断如果满足条件,则用于终止整个循环。与满足条件后的continue语句不同,它会中断循环,并且循环的其余部分不会执行。 Break语句与决策语句一起使用,例如if,if-else或者 转变 在for循环内的语句,可以是for循环, while循环或do-while循环。它强制循环停止进一步迭代的执行。

语法

break;

程式2:

考虑一种情形,在该情形中,要打印一系列数字,但不遵循某个特定值K。因此,在这种情况下,想法是在i的值为K之后使用break语句。以下是相同的程序:

C++

// C++ program to demonstrate the
// break statement
#include 
using namespace std;
  
// Driver Code
int main()
{
    for (int i = 1; i < 10; i++) {
  
        // Breaking Condition
        if (i == 5)
            break;
        cout << i << " ";
    }
    return 0;
}
输出:

1 2 3 4

返回值它使控制权脱离函数本身。它比休息强。它使用该函数的执行之后或一些条件后到终止整个函数。每个函数都有一个return语句,带有除void()函数之外的一些返回值。尽管void()函数也可以具有return语句以结束该函数的执行。

语法

return expression;

程序3:

下面是演示return语句的程序:

C++

// C++ program to demonstrate the
// return statement
#include 
using namespace std;
  
// Driver code
int main()
{
    cout << "Begin ";
  
    for (int i = 0; i < 10; i++) {
  
        // Termination condition
        if (i == 5)
            return 0;
        cout << i << " ";
    }
    cout << "end";
  
    return 0;
}
输出:
Begin 0 1 2 3 4

解释:

上面的程序通过打印“ Begin”开始执行,然后for循环开始打印i的值,它将打印i的值(从04),但是一旦i等于5 ,它将终止整个函数,即永远不会去打印程序的“ end”语句。

void()函数中的return可以不带任何return类型地使用。

句法:

return;

计划5:

 下面是演示函数的return声明为void返回值的程序:

C++

// C++ program to demonstrate the return
// statement in void return type function
#include 
using namespace std;
  
// Function to find the greater element
// among x and y
void findGreater(int x, int y)
{
    if (x > y) {
        cout << x << " "
             << "is greater"
             << "\n";
        return;
    }
    else {
        cout << y << " "
             << "is greater"
             << "\n";
        return;
    }
}
  
// Driver Code
int main()
{
    // Function Call
    findGreater(10, 20);
  
    return 0;
}
输出:
20 is greater

Goto 此语句用于直接跳转到要调用它的程序部分。每个goto语句都与标签相关联,该标签将它们带到调用它们的程序的一部分中。标签语句可以写在程序中任何地方,无需在goto语句之前或之后使用。该语句很难理解程序的流程,因此避免在程序中使用它。

转到声明

句法:

goto label_name;
.
.
.
label_name:

计划6:

下面是演示goto语句的程序:

C++

// C++ program to demonstrate the
// goto statement
#include 
using namespace std;
  
// Driver Code
int main()
{
    int n = 4;
  
    if (n % 2 == 0)
        goto label1;
    else
        goto label2;
  
label1:
    cout << "Even" << endl;
    return 0;
  
label2:
    cout << "Odd" << endl;
}
输出:
Even

说明:上面的程序用于检查如果用户按下的数字为4 ,则该数字是偶数还是奇数,因此if语句满足条件,并且控制转到label1label1打印该数字为偶数。在这里,没有必要在goto语句之后编写标签语句,我们可以在goto语句之前编写它,这样也可以正常工作。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”