Skip to content Skip to sidebar Skip to footer

C How to Call the While Loop Again

The do while loop in C

Last updated on July 27, 2020


do… while loop #

Syntax:

                                            practise                      {                      // trunk of exercise while loop                      statement                      i                      ;                      argument                      ii                      ;                      }                      while                      (                      status                      );                    

In do while loop first the statements in the body are executed then the status is checked. If the status is true then in one case once more statements in the body are executed. This procedure keeps repeating until the condition becomes fake. Every bit usual, if the body of do while loop contains only 1 argument, then braces ({}) tin be omitted. Observe that unlike the while loop, in do while a semicolon(;) is placed afterward the status.

The do while loop differs significantly from the while loop considering in do while loop statements in the body are executed at least in one case even if the status is false. In the instance of while loop the status is checked first and if it truthful only then the statements in the body of the loop are executed.

The post-obit program print numbers between ane and 100 which are multiple of 3 using the practise while loop:

                      ane  two  iii  4  five  6  seven  8  9 10 11 12 thirteen xiv 15 16 17 18 19
                                            #include                      <stdio.h> // include the stdio.h                                            int                      primary                      ()                      {                      int                      i                      =                      1                      ;                      // declare and initialize i to ane                      do                      {                      // check whether i is multiple of 3 not or not                      if                      (                      i                      %                      3                      ==                      0                      )                      {                      printf                      (                      "%d "                      ,                      i                      );                      // print the value of i                      }                      i                      ++                      ;                      // increase i by 1                      }                      while                      (                      i                      <                      100                      );                      // finish the loop when i becomes greater than 100                      // signal to operating system everything works fine                      render                      0                      ;                      }                    

Expected Output:

                      3 half-dozen 9 12 fifteen xviii 21 24 27 xxx 33 36 39 42 45 48 51 54 57 60 63 66 69 72 7 5 78 81 84 87 90 93 96 99                    

How information technology works:

In line v, nosotros have declared and initialized variable i. And so, the control comes inside the torso of the practise while loop. Within the body of the loop the if status (i%3==0) is tested, if it is true, and so the statement within the if block is executed. The statement i++ increments the value of i by i. At final, the practise while status (i<100) is checked. If it is true so statements inside the body of the loop are executed once again. This procedure keeps repeating equally long equally the value of i is less than 100.

Where should I use practice while loop? #

Virtually of the time you volition use while loop instead of do while. However, there are some scenarios where practice while loop suits best. Consider the following problem.

Permit's say you lot want to create a programme to detect the factorial of a number. Every bit you probably know that factorial is only valid for 0 and positive numbers. Here is one way you can approach this trouble.

Let's say the user entered a negative number, so instead of displaying an error message and quitting the plan, a better approach would exist to ask the user again to enter a number. You have to keep asking until the user enters a positive number or 0. Once a positive number or 0 is entered, summate factorial and display the result.

Let's see how we can implement information technology using while and do while loop.

Using while loop #

                      one  2  3  4  5  6  vii  8  9 x eleven 12 13 fourteen 15 sixteen 17 18 xix 20 21 22
                                            #include                      <stdio.h> // include the stdio.h                                            int                      chief                      ()                      {                      int                      num                      ;                      char                      num_ok                      =                      0                      ;                      // go along asking for numbers until num_ok == 0                      while                      (                      num_ok                      ==                      0                      )                      {                      printf                      (                      "Enter a number: "                      );                      scanf                      (                      "%d"                      ,                      &                      num                      );                      // if num >= 0 set num_ok = 1 and stop asking for input                      if                      (                      num                      >=                      0                      )                      {                      num_ok                      =                      1                      ;                      }                      }                      // calculate factorial                      }                    

Using do while loop #

                      1  two  3  4  5  6  7  8  9 10 xi 12 xiii 14
                                            #include                      <stdio.h> // include the stdio.h                                            int                      main                      ()                      {                      int                      num                      ;                      practise                      {                      printf                      (                      "Enter a number: "                      );                      scanf                      (                      "%d"                      ,                      &                      num                      );                      }                      while                      (                      num                      <                      0                      );                      // continue asking for numbers until num < 0                      // calculate factorial                      }                    

Notice that the solution using while loop is more than involved, to achieve the aforementioned matter nosotros have to create an actress variable num_ok, and an additional if statement. On the other hand, the do while loop achieves the aforementioned thing without any trickery and it's more elegant and concise.

Earlier we leave do while loop, allow's accept one more example.

The Following program calculates Simple interest:

                      1  ii  3  4  5  six  7  8  ix 10 eleven 12 13 14 15 16 17 18 nineteen xx 21 22 23 24 25 26 27 28 29 30 31 32 33 34
                                            /******************************************                                              Program to calculate the Uncomplicated involvement                                                                    *                                              * SI = (Principal * Rate * Time) / 100                                              *                      ******************************************/                      #include                      <stdio.h> // include stdio.h                                            int                      main                      ()                      {                      float                      p                      ,                      r                      ,                      t                      ;                      char                      ch                      =                      'y'                      ;                      exercise                      {                      printf                      (                      "Enter principal: "                      );                      scanf                      (                      "%f"                      ,                      &                      p                      );                      printf                      (                      "Enter rate: "                      );                      scanf                      (                      "%f"                      ,                      &                      r                      );                      printf                      (                      "Enter t: "                      );                      scanf                      (                      "%f"                      ,                      &                      t                      );                      printf                      (                      "SI = %.2f"                      ,                      (                      p                      *                      r                      *                      t                      )                      /                      100                      );                      printf                      (                      "                      \northward\northward                      Calculate SI one more than time ? ('y' for Yep, 'due north' for no ) : "                      );                      scanf                      (                      " %c"                      ,                      &                      ch                      );                      // notice the preceding white infinite before %c                                            }                      while                      (                      ch                      ==                      'y'                      );                      // keep asking for P, R and T til the input is 'y'                      // point to operating system everything works fine                      return                      0                      ;                      }                    

Expected Output:

                      ane  2  3  4  five  half dozen  seven  8  9 ten 11 12
                      Enter principal: 15000 Enter rate: 4.five Enter t: 3 SI = 2025.00  Summate SI 1 more time ? ('y' for Aye, 'due north' for no ) : y Enter primary: 20000 Enter rate: 5.4 Enter t: four SI = 4320.00  Calculate SI i more time ? ('y' for Yes, 'n' for no ) : n                    


Ezoic

smithvien2000.blogspot.com

Source: https://overiq.com/c-programming-101/the-do-while-loop-in-c/

Post a Comment for "C How to Call the While Loop Again"