มีหลายวิธีในการพิมพ์ตัวเลขโดยไม่ต้องใช้ลูป เช่น การใช้ฟังก์ชันเรียกซ้ำ คำสั่ง goto และสร้างฟังก์ชันนอกฟังก์ชัน main()
นี่คือตัวอย่างการพิมพ์ตัวเลขโดยใช้คำสั่ง goto ในภาษา C++
ตัวอย่าง
#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;