How does if/else work internally in all programming languages? [closed]

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.

14

It seems to me that you are asking two different questions:

  1. How if else works internally?
  2. What if is in terms of high level programming?

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

if (x >= y) {
    // main branch
} else {
    // else branch
}

could be translated into code (here some kind of pseudo-assembly) like

cmp (r1),(r2)    ;compare words at memory addresses pointed by registers r1 and r2
blt else         ;jump to else-part if (r1) is less than (r2)
   ...           ;insert the main branch code here
bra end_if       ;jump to end_if
else:
   ...           ;insert the else branch code here
end_if:

The actual machine code consists of just binary data but it is structurally similar with assembly code.

4

This answer is about languages like C, C++, Java, PHP and Javascript. There are other programming languages that work differently.

if is a keyword, and else is a keyword. When they are used in code, if and else together form a control flow statement.

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.

if else is not a function, but it can appear inside a function.

# This part is a function, and *could* be a class method or static method
function hello($name)
{

  # This part is a control flow statement, and can be *inside* a function
  if($name=='Peter')
  {
    #The next line is one statement
    echo 'Hello Pete!";
  } else {
    echo 'Hello $name!";
  }

}

These are examples of keywords: if, else, while, for, case, continue, function, return, class, public, private, protected, static, new.

Each programming language has different keywords. What your keywords are, and how they work are different in every language.

Keywords (if, else), operators (==, <, !=), variables ($name), string literals ('Peter'), functions (echo, hello) all work together to make statements. Statements do all most of the work, and are grouped into functions or methods (usually).

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.

8

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. cond in Lisp), but one must be built in.

6

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).

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
if (a == b) where a = 10, b = 10. Processor does 10-10=0. Once the result is zero – ZF(Zero Flag) is set.

So:

cmp a, b
jz equal
jnz notEqual

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.

  • JL stands for – Jump if Less
  • JG stands for – Jump if Greater

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 :

  • 1 : put the value punched in by the operator into register A
  • 2 : put the value punched in by the operator into register B
  • 3 : put the following value into register C
  • 4 : put the content of C into A
  • 5 : put the content of A into C
  • 6 : add A and B and put the result into register A
  • 7 : subtract A and B and…
  • 8 : multiply A and B…
  • 9 : divide A and B and put the quotient into register A
  • 10 : print the value that is in register A on a little piece of paper

So, the following “program” saved on, say, a punched card :

  • instruction 1 : 1
  • instruction 2 : 2
  • instruction 3 : 6
  • instruction 4 : 10

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 :

  • 11 : go to instruction #n, n being the value in the next “instruction”

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 :

  • 11 : go to instruction #n, only if the value in A is 0

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.

  • instruction 1 : 1
  • instruction 2 : 2
  • instruction 3 : 7
  • instruction 4 : 11
  • instruction 5 : 7
  • instruction 6 : 2
  • instruction 7 : 10

Well, that’s rather boring to write programs this way, so assembly languages were invented :

  • READ-A
  • READ-B
  • SUB
  • JMP-IF-ZERO .endif
  • DIVIDE
    .endif:
  • PRINT

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 :

a = read();
b = read();
a = a - b;

if (a != 0) {
   b = read();
   a = a / b;
}

print(a);

The way if is translated by your compiler in machine instructions : the condition after the if keyword is transformed as a test whose result is stored in A, and the number of the instruction where to jump is determined based on where your bracket closes.

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.

2

This in fact depends on the language you are talking about.

There are excellent answers here on how if and else are keywords and form control flow statements because when compiled to a sequence of instructions, these statements control the execution flow of the instruction sequence – which of course is sequential by default, but can have jumps in order to allow execution to depend on the current state. And this is certainly true for a whole set of languages.

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.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật