Is the if statement considered a method?

An interesting discussion has appeared among my peers regarding whether the expression “if” is a considered method? Although "if" is added using the word operator, it still behaves similarly to the simple irreversible method.

For example:

if(myValue) //myValue is the parameter passed in { //Execute } 

Similarly, a method can perform the same operation:

 public void MyMethod(myValue) { switch(myValue) { case true: //Logic break; case false: //Logic break; } } 

Is it accurate to invoke (consider) the if statement in a simple, predefined method in a programming language?

+49
language-agnostic programming-languages if-statement condition conditional-statements
Dec 29 '11 at 9:37 a.m.
source share
14 answers

In languages ​​such as C, C ++, C #, Java, IF there is an operator implemented as a reserved word, which is part of the core language. In programming languages ​​of the LISP family (the circuit comes to mind) IF is an expression (meaning that it returns a value) and is implemented as a special form. On the other hand, in pure object-oriented languages ​​such as Smalltalk, IF really is a method (more precisely: a message) that is usually implemented in the Boolean class or in one of its subclasses.

Bottom line: The true nature of a conditional IF depends on the programming language and the programming paradigm of that language.

+106
Dec 29 2018-11-21T00:
source share

No, the if statement is not like a C # method. Consider the ways in which it does not look like a method:

  • The objects in the containing block are in the "if" body area. But the method does not get any access to the binding environment of its caller.
  • In many languages, methods are members of something - such as, perhaps. Statements are not members.
  • In languages ​​with first-class methods, methods can be transmitted as data. (In C #, by converting them to delegates.) The if statements are not first-class.
  • etc. The differences are innumerable.

Now it makes sense to think of some things as a method, rather than an "if". Many operators, for example, are very similar to methods. There is very little conceptual difference between:

 decimal x = y + z; 

and

 decimal x = Add(y, z); 

And in fact, if you parse the addition of two decimal places in C #, you will find that the generated code is actually a method call.

Some operators have unusual characteristics that make it difficult to characterize them as methods:

 bool x = Y() && Z(); 

differs from

bool x = And (Y (), Z ());

in a language that seeks to evaluate the arguments of a method; in the first case, Z () is not evaluated if Y () is false. In the second, both are rated.

Your creation of the if method rather asks a question; the implementation is more complicated than the if statement. Saying that you can emulate an “if” using a switch is to say that you can imitate a bike with a motorcycle; replacing something simple with something more complex is not convincing. It would be wiser to point out that the switch is actually a bizarre if.

+42
Dec 29 '11 at 22:44
source share

You cannot create the myIfStatement() method and expect the following to work:

 ... myIfStatement(something == somethingElse) { // execute if equal } else { // execute if different } 

if is a control statement and cannot be replicated by a method, and you cannot replace a method call if:

 myVariable = if(something == somethingElse); 

if cannot be overloaded.

These are some signs that if not a method, but there are others that I suspect.

+14
Dec 29 2018-11-21T00:
source share

It depends on the language exactly, but in C, java, perl, no, these are language commands. Reserved words. If they were functions, you could overload them and get pointers to them and do everything you can do with functions.

This is more a philosophical question than a programming question.

+8
Dec 29 2018-11-21T00:
source share

The method has a signature, and its main intention is resuable logic, whereas if it is just a condition that controls the flow of execution. If you understand the assembly, you would know that both of them differ even at a very low level.

+4
Dec 29 2018-11-12T00:
source share

Of course, you can write If() and IfElse() methods, but that does not make them the same.

If() defined as an operator in the language, at the same level as method calls. But there are differences in ao syntax and optimization capabilities.

So: No, the If() operator is not a method . You cannot, for example, not assign it to a delegate.

+3
Dec 29 '11 at 9:41
source share

Considering that the if is a method only makes it confusing, in my opinion. The similarity to the method call is superficial.

The if is one of the statements that control the flow of execution. When it is compiled into native machine code, it will evaluate the expression and make a conditional jump.

Pseudocode:

 load myValue, reg0 test reg0 jumpeq .skip ; code inside the if .skip: 

If you use else , you will get two transitions:

 load myValue, reg0 test reg0 jumpeq .else ; code inside the if jmp .done .else: ; code inside the else .done: 
+3
Dec 29 2018-11-12T00:
source share

Is the if statement considered a method?

No, this is not considered a method, as you have already seen in other answers. However, if you had a question: “Does it behave like a method?”, Then the answer may be yes depending on the language. Any language that supports first-class functions can do without a built-in constructor / statement such as if . Ignore all the fluffy things like return values ​​and syntax, because basically it is just a function that evaluates to a boolean and , if true , then it executes some block of code. Also ignore OO and functional differences, because the following examples can be implemented as a method of the Boolean class in any language that is used as Smalltalk.

Ruby supports blocks of executable code that can be stored in a variable and passed to methods. So, here is the custom _if_ function implemented in Ruby. The material in { .. } is part of the executable code passed to the function. It is also known as a block in Ruby.

 def _if_ (condition) condition && yield end # simple statement _if_ (42 > 0) { print "42 is indeed greater than 0" } # complicated statement _if_ (2 + 3 == 5) { _if_ (3 + 5 == 8) { puts "3 + 5 is 8" } _if_ (5 + 8 == 13) { puts "5 + 8 is 13" } } 

We can do the same in C, C ++, Objective-C, JavaScript, Python, LISP and many other languages. Here is a javascript example.

 function _if_(condition, code) { condition && code(); } _if_(42 > 0, function() { console.log("Yes!"); }); 
+3
Jan 19 '12 at 5:22
source share

If this were classified as a method, then, of course, we would be in the field of OO, but we will not do this, so I assume that we are talking about functions. Of course, a function / subroutine can be written to replicate if behavior (I think this is actually a function in the lisp / scheme.).

I would not classify it as a function or even a subroutine, but simply controlled the flow.

+2
Dec 29 '11 at 9:41
source share

If by a method we understand a block of code that can be called, and the control flow is automatically returned to the caller when the method ends, then if are not methods. The control flow does not return anywhere after if .

+2
Dec 29 2018-11-21T00:
source share

The IF statement is a conditional loop function used in most lanuages ​​that executes a path stream from evaluating the logical condition true or false. In addition to the case of branch prediction, this is always achieved by selectively changing the control flow based on some condition.

The IF construct is the simplest and most necessary logic used in programming. It allows you to create building blocks for functions.

+2
Jan 04 2018-12-12T00:
source share

Yes , if is a function in some languages, although it is rare and use is limited.

Typically, a construct has something like if(booleanCondition, functionPointerToCallIfConditionTrue, functionPointerToCallIfCondtionFalse) . This can be used as a delegate for other functions if you want.

Mathematica, for example, behaves this way, and even C # can do it with a bit of work if you use Linq expressions; Take a look at System.Linq.Expressions.Expression.IfThenElse .

+2
Jan 21 '12 at 18:25
source share

No. You will not go back when you are done with if. This is just a control expression.

0
Dec 29 '11 at 9:47 a.m.
source share

Note that in your example, you replaced one “select statement” (C # 4 specification, section 8.7), an if statement (section 8.7.1) with another, switch statement (section 8.7.2). You also reorganized the select statement into a separate method. However, you have not replaced the use of a select statement with a method.

The answer to your question is no.

0
Dec 29 '11 at 21:50
source share



All Articles