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

=Lesson 10: Conditionals and Loops= Conditionals and loops are essential for writing D programs.

The and   Statements
Using  allows you to make part of your code only execute if a certain condition is met. if(condition that evaluates to true or false) {        // code that is executed if condition is true } else { // code that is executed if condition is false }

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. if(condition1) do_this; else if(condition2) do_that; // only executed if condition1 is false, but // condition2 is true else do_the_other_thing; // only executed if both condition1 and condition2 are false As a result, this is often seen: if (condition1) { do_something1; something_more1; } else if(condition2) { do_something2; something_more2; } else if(condition3) { do_something3; something_more3; } else if(condition4) { do_something4; something_more4; } else { do_something_else; }

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

The 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: while(condition1) { do_this; } and do { do_this; } while(condition1)

The difference is, in the first example, if condition1 is false,  is never called, while in the second example it would be called once (the conditional check happens after the code is executed once).

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

The 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:

for(initialization; condition; counting expression) { ... }

The initialization expression is executed only once during the beginning. Then condition is checked to be  or. If it is, 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, the loop continues. For example:

You can even omit parts of what goes inside the parentheses of the. These two are equivalent:

and
These are two statements that are used inside of loops.

The  statement breaks out of the loop. Whenever the  statement is encountered, the loop is immediately exited. This statement can go inside of,  ,  , and   blocks (you will learn about those later).

The  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. Do not use these unless if you have to. Code that uses labels can most often be written with more readable looping constructs, like,  , and.

There is something in D, C and C++ called the. D's switches are actually more powerful than C and C++'s switches. Note that you must have a  in order to get out of the switch. Otherwise, fall-through occurs. Also, you can use. Strings can be used in. This is a feature that's not in C or C++.

(It doesn't seems to work in recent compilers).

You can use else for,  , and   loops, too. If any of those loops have an  clause, then the   is only executed if the loop terminates normally (i.e. not with  ).