if(true/false){
//if condition is true
}else {
// if condition is false
}
Everyone knows about the structure of if-else. But what is if actually? Is it a class method, static method or keyword? I'm little confused about this.
Everyone knows about the structure of |
|||||||||||||||||||||
|
|
It seems to me that you are asking two different questions:
This answer is for question 1. For question 2 see other answers. Gustav Bertram's answer seems very clear. Before your code gets executed, it must be translated into machine code. The translation is done by either a compiler (like a C-compiler) or an interpreter (like Ruby interpreter) or a virtual machine (like with Java). Control structures like if-else are translated into a pattern of machine code instructions. The actual pattern depends on the target machine and the compiler/interpreter/virtual machine. An if-else-structure like
could be translated into code (here some kind of pseudo-assembly) like
The actual machine code consists of just binary data but it is structurally similar with assembly code. |
|||||||||||||
|
|
This answer is about languages like C, C++, Java, PHP and Javascript. There are other programming languages that work differently.
Class methods, or static methods are always functions or procedures that are attached to a class. "Method" means the same thing as "function" or "procedure", but on a class.
These are examples of keywords: Each programming language has different keywords. What your keywords are, and how they work are different in every language. Keywords ( I think you should try Code Academy. It teaches basic programming, and it might help you understand the parts of programming statements. You should also try to find a book that teaches your programming language. Such books should explain the parts of statements too. If one book is not clear, try another one. |
|||||||||||||||
|
|
It can actually be several things. In imperative languages like C++ or Java, it is a statement type. In pure object-oriented languages like Smalltalk it is an instance method of boolean. In pure functional languages it is a function. But in all languages it is a primitive. Some form of conditional must be built into the language. It may be slightly different type of conditional (e.g. |
|||||||||||||||
|
|
As the others have already explained, control flow is usually implemented with branching instructions. But, jumps are not always necessary. In some ISAs there is a so called "conditional execution": instructions will be bypassed if they're marked as conditional and a flag after a previous comparison operation is not set. Jumps can be mispredicted and may thrash pipeline, whereas a short sequence of conditional instructions might be much cheaper. Another interesting form of low level control flow is a so called "zero-overhead looping", common in many modern DSP processors (e.g., in Hexagon). |
|||
|
|
|
Once you get down to the metal, it is a jump/branch statement -- basically a fancy goto. Higher level languages have control structures that let you implement common low level activities in a clear and understandable manner. But when you get down to it, it's a bunch of registers, jumps and poking things in and out of memory. |
|||||
|
|
Internally on x86/64 architecture if/else/then is generated with internal CMP mnemonic. It compares the results and sets the EFLAGS register with appropriate flags(zero, overflow, etc.). Internally if the arguments are equal their minus product is zero. For example
So:
JZ - Jump if Zero is equal to JE as well as JNZ(Not Zero) is equal to JNE(Not Equal). They are equal and JE and JNE exist just because of readability.
|
|||
|
|
|
It really depends on the language, of course. Since you're giving a pseudo-C example, I'll stick to how it works in most C-like languages. A program has to be translated to machine-code in the end, so we have to talk about machine code first. Let's start with an (imaginary, but that's the idea) very old computer who is able to perform these operations only :
So, the following "program" saved on, say, a punched card :
Would put a number in A, then another in B, add them and put the result in A then print it on a piece of paper. The point is, very old computers did not have any possibility of conditional execution or loops. A program was nothing more than perform this calculation, then this one, then this one, and so one. They were little more than a calculator to which you gave a list of calculations to perform. That soon became a problem : very often, we want a calculation not to be performed in some situations ; for example, if the current value is 0, I don't want to divide another number by it ! Computers had to be enhanced, and that was a big and (somewhat) complicated improvement : now, your computer has to be aware of where it is in the current program flow and act on it ; just reading the current instruction and going to the next one is not enough. That means you have to add the following instructions to go forward :
Very good, now you can skip instructions if needed ; for example, you can skip the division instruction if you do not want to perform it. Well, actually, we will always jump, so that's a rather useless statement. Let's rewrite it this way :
Yep, an "if statement", that's where it is actually implemented : in a machine instruction. It's very low level and limited, but that's actually not a problem because you can do anything you want with it. It's hard to use, but high-level languages will deal with that for you. Note that it is really very powerful : you can implement loops with it, too (loops are a jump backward rather than forward). Let's write the following program : read two numbers, subtract them. If the result is not 0, read a third number, divide it by the result and print the overall result.
Well, that's rather boring to write programs this way, so assembly languages were invented :
And, since assembly is dull and error-prone too, we invented high-level languages such as C. The following pseudo-C program is the equivalent as above :
The way |
|||
|
|
|
This in fact depends on the language you are talking about. There are excellent answers here on how But there are a couple of languages, such as Smalltalk and Lisp (probably the most prominent examples), that actually implement their equivalent of control flow statements as methods or macros respectively, over a very basic mechanism for conditional execution/jumps. |
|||||||||
|