# D (The Programming Language)/d2/Conditionals and Loops

| | Topics: | Language and Phobos - DGui (example placeholder - can be any library) |
| Previous | Lessons: | 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - T |

# Lesson 10: Conditionals and Loops

Conditionals and loops are essential for writing D programs.

## Concepts
### The if and else Statements

Using if allows you to make part of your code only execute if a certain condition is met.
In fact, if the section of code that's inside the if or else is only one line long, you can omit the curly brackets.
As a result, this is often seen:

### The Condition

The condition that goes inside of the parentheses in conditional statements such as if can be anything convertible to bool. That includes integral and floating-point types (true if nonzero, false if otherwise) and pointers (null is false, and dynamic arrays (always true).

### The while Loop

A while loop will allow you to repeat a block of code as long as a certain condition is met. There are two forms of the while loop:
and
The difference is, in the first example, if condition1 is false, do_this is never called, while in the second example it would be called once (the conditional check happens after the code is executed once).

### The foreach Loop

This loop is for iteration. Take a look at these two ways to use foreach:

### The for Loop

This type of looping is the most complex, but it is also the one that gives a lot of control. It is defined in the same way as other C-like languages:
The initialization expression is executed only once during the beginning. Then condition is checked to be true or false. If it is true, the code inside of the conditional block (inside of the brackets) is executed. After that execution, the counting expression is executed. Then, the condition is checked, and if it is true, the loop continues. For example:
You can even omit parts of what goes inside the parentheses of the for. These two are equivalent:

### break and continue

These are two statements that are used inside of loops.
The break statement breaks out of the loop. Whenever the break statement is encountered, the loop is immediately exited. This statement can go inside of while, for, foreach, and switch blocks (you will learn about those later).
The continue statement causes a loop to restart at the beginning. Let's see, through code, exactly how this works. This code example counts to 7 but skips 5.

### Switches and More

D allows absolute branching with labels and goto.
Do not use these unless if you have to. Code that uses labels can most often be written with more readable looping constructs, like for, while, and foreach.
There is something in D, C and C++ called the switch. D's switches are actually more powerful than C and C++'s switches.
Note that you must have a break in order to get out of the switch. Otherwise, fall-through occurs. Also, you can use goto.
Strings can be used in case. This is a feature that's not in C or C++.

### else

(It doesn't seems to work in recent compilers). You can use else for foreach, while, and for loops, too. If any of those loops have an else clause, then the else is only executed if the loop terminates normally (i.e. not with break).