D Programming – Break Statement

D Programming - Break Statement

This topic is about D Programming – Break Statement.

The break statement in D programming language has the following two usages −

  • When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
  • It can be used to terminate a case in the switch statement (covered in the next chapter).

If you are using nested loops (i.e., one loop inside another loop), the break statement stops execution of the innermost loop and start executing the next line of code after the block.

Syntax

The syntax for a break statement in D is as follows −

break;

Flow Diagram

Example

import std.stdio;
 
int main () {
   /* local variable definition */
   int a = 10;

   /* while loop execution */
   while( a < 20 ) {
      writefln("value of a: %d", a);
      a++;
      
      if( a > 15) {
         /* terminate the loop using break statement */
         break;
      }
   }
 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

To know more, Click Here.

This Post Has One Comment

Leave a Reply