Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

if statement
d
d programming
communitycreator

How to use the if statement in D

Chinweuba Elijah Azubuike

Overview

An if statement is part of the flow controls in programming. It enables the system to make certain decisions based on set conditions. In this shot, we will look at the if statement in D and how to use it.

Syntax

The syntax of the if statement is given below:

if(condition) {/** execution will take place only uf conditions are met */}
IF syntax

Parameter:

The if statement takes in a condition in the form of a boolean expression. If the boolean expression returns true, then the logic or code block in the if statement is executed. Let's look at an example:

Example

import std.stdio;
 
int main () { 
   /* defining variables */ 
   int x = 1; 
  
   /* check the boolean condition */ 
   if( x < 5 ) { 
      /* print only if condition returns true */ 
      writefln("x is less than 5" ); 
   } 
   writefln("value of x is : %d", x); 
  
   return 0;
}
Example on if statement in D

Code explanation

Line 5: We declare and define our variable, x.

Line 8: We place our condition x < 5 inside the if statement (). The condition states that unless x is less than 5, the codes inside the if curly bracket {} should not be executed.

Line 10: Our conditions are met, so writefln("x is less than 5"); is executed. This prints an output.

Line 12: We use writefln("value of x: %d", x) to output the value of x.

RELATED TAGS

if statement
d
d programming
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring