Lập trình C++ - Lệnh go
Lệnh go trong ngôn ngữ C++
Trong ngôn ngữ C để nhảy (jump) đến nhãn được định nghĩa trước chúng ta dùng lệnh go
Cú pháp:
goto label;
Ví dụ dùng lệnh go:
#include <iostream>
using namespace std;
int main()
{
ineligible:
cout<<"You are not eligible to vote!\n";
cout<<"Enter your age:\n";
int age;
cin>>age;
if (age < 18){
goto ineligible;
}
else
{
cout<<"You are eligible to vote!";
}
}
Kết quả:
You are not eligible to vote!
Enter your age:
16
You are not eligible to vote!
Enter your age:
7
You are not eligible to vote!
Enter your age:
22
You are eligible to vote!