In compiler design, several statement types influence the program's execution order, known as control flow statements. Here are some key ones:
-
Conditional statements:
if
statement: Executes a block of code only if a certain condition (boolean expression) evaluates to true.if-else
statement: Provides an alternative block of code to execute if the condition in theif
statement is false.
-
Looping statements:
while
loop: Repeatedly executes a block of code as long as a specific condition remains true.do-while
loop: Executes a block of code at least once, then continues looping as long as a condition is true.for
loop: Iterates a set number of times, often used for counting or processing elements in a collection.
-
Branching statements:
goto
statement (considered less desirable due to potential for spaghetti code): Jumps directly to a labeled statement elsewhere in the code.
-
Other statements affecting control flow:
switch
statement: Executes a specific block of code based on the value of a variable compared against multiple cases.break
statement: Exits a loop or switch statement prematurely.continue
statement: Skips the remaining code in the current iteration of a loop and jumps back to the beginning of the loop.
These statements allow programmers to control the order in which different parts of the code are executed, making programs more flexible and powerful. The compiler translates these statements into machine code instructions that achieve the desired control flow behavior.