有几种不使用循环而打印数字的方法,例如使用递归函数,goto语句和在函数外部创建main()
函数。
这是一个使用C ++语言使用goto语句打印数字的示例,
#include <bits/stdc++.h> using namespace std; int main() { int count=1; int x; cout << "Enter the max value of x : "; cin >> x; PRINT: cout << " " << count; count++; if(count<=x) goto PRINT; return 0; }
输出结果
Enter the max value of x : 1
在上面的程序中,我们使用GOTO语句打印了从1到100的数字,而没有使用循环和递归。
PRINT: cout << " " << count; count++; if(count<=x) goto PRINT;