質問

Someone once said to me that you can perform any programming functions with just if - else statements.

I want to know:

  1. Is this statement true? (From my understanding turing-completeness depends on the ability to loop and having if/else doesn't make something turing-complete)
  2. Is it possible to write a for loop with only if else statements?

How would you loop through this with only if else? ["fruit", "cars", "water"]

役に立ちましたか?

解決

Engineers can use a recursion to opposite for-while statements. Recursion is the process of repeating items in a self-similar way.

The computation of Fibonacci numbers is a great example for understanding the recursion (the most simple and trivial example).

public int fibonacci(int n) { if (n <= 1) return n; else return fibonacci(n-1) + fibonacci(n-2); }

But recursion in most popular programming languages has some disadvantages: (a) it's slow on big iterations and (b) uses a lot of memory for the stack of execution.

So, yes it's possible to write a for loop with only if-else statements.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top