Are "loops" a type of "conditional statement" in Java?

I have a project that says: "There will be no conditional statements (if, switch, or, ...)." I'm not sure if this includes for and while loops, since both technically work on conditions. Can I run away by saying that they are “conditional loops” instead?

+4
source share
2 answers

It would probably be acceptable to use loops ( for , while , ...), but you will need to check with the project author. I tend to relate to loops and conditional statements separately, since they usually have different goals ...

  • Conditional operators like if and switch will make a choice from a list of options. They run only once.
  • Loops, such as for and while , are usually designed to run a piece of code several times.

Of course, this is just a generalization, and everyone probably has a different opinion, but of course I treat them differently, because they have different primary goals.

For an extra loan, Wikipedia seems to agree. If Statement is a conditional statement, and For Loop is an iterative expression.

+4
source

for and while use (terminating) conditions rather than conditional statements, so the loops are fine on this basis.

Besides the loops, another option would be a ternary operator ? - this is not an operator, this is an operator, and you may be able to encode some conditional stream using these, that is, this code:

 int x; if (<some condition>) x = 1; else x = 2; 

can be encoded using the ternary operator:

 int x = <some condition> ? 1 : 2; 
+2
source

Source: https://habr.com/ru/post/1447382/


All Articles