打印无循环 [英] Print without loops

查看:186
本文介绍了打印无循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

打印1到1000没有循环或条件





#include <stdio.h>
#include<conio.h>

int fun(int n) {
--n && fun(n);
return printf( "\n%d", n+1);
}

int main(void) {
fun(1000);
getch();
return 0;
}

这是一个打印1到1000而不使用循环或if-else的程序。是否还有其他方法可以不使用循环或if-else。

this is a program which prints 1 to 1000 without using loops or if-else. Are there other ways of doing that not using loops or if-else.

推荐答案

递归实际上是一种循环方法。如果你只想避免使用while,for和if关键字那么你可以做一些令人讨厌的事情:

Recursion is really a method of looping. If you just want to avoid the while, for, and if keywords then you could do something nasty like:

int i = 0;
label:
printf("\n%d", n);
n++;
switch (i) {
  case 1000:
    break;
  default:
    goto label;
}

这篇关于打印无循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆