C++ What if User Types Something Other Then Yes to Run Again
The practice while loop in C
Last updated on July 27, 2020
do… while loop #
Syntax:
do { // body of do while loop statement 1 ; argument 2 ; } while ( condition ); |
In do while loop kickoff the statements in the body are executed then the condition is checked. If the condition is true then one time again statements in the trunk are executed. This process keeps repeating until the condition becomes false. Equally usual, if the body of do while loop contains but one statement, so braces ({}) tin be omitted. Notice that different the while loop, in do while a semicolon(;) is placed afterwards the condition.
The exercise while loop differs significantly from the while loop because in do while loop statements in the body are executed at to the lowest degree in one case even if the condition is false. In the case of while loop the condition is checked first and if information technology true just then the statements in the trunk of the loop are executed.
The following program print numbers between 1 and 100 which are multiple of 3 using the do while loop:
1 ii 3 4 5 6 7 8 nine x 11 12 13 14 xv 16 17 18 19 | #include <stdio.h> // include the stdio.h int primary () { int i = ane ; // declare and initialize i to 1 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 past ane } while ( i < 100 ); // stop the loop when i becomes greater than 100 // signal to operating organisation everything works fine render 0 ; } |
Expected Output:
3 6 9 12 15 xviii 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 seven 5 78 81 84 87 90 93 96 99 |
How it works:
In line 5, we have declared and initialized variable i. Then, the control comes inside the torso of the exercise while loop. Within the body of the loop the if status (i%3==0) is tested, if information technology is true, then the statement inside the if block is executed. The statement i++ increments the value of i by 1. At last, the do while condition (i<100) is checked. If it is true then statements inside the torso of the loop are executed one time once again. This process keeps repeating equally long as the value of i is less than 100.
Where should I use do while loop? #
Most of the time you lot will use while loop instead of do while. However, at that place are some scenarios where do while loop suits best. Consider the following problem.
Let's say you want to create a program to find the factorial of a number. As you probably know that factorial is only valid for 0 and positive numbers. Here is i fashion you can approach this problem.
Let's say the user entered a negative number, so instead of displaying an error message and quitting the program, a better approach would be to ask the user over again to enter a number. Y'all take to keep asking until the user enters a positive number or 0. One time a positive number or 0 is entered, calculate factorial and display the result.
Let'southward see how we tin implement it using while and practice while loop.
Using while loop #
1 2 3 4 five 6 vii 8 nine 10 11 12 xiii 14 fifteen 16 17 18 19 20 21 22 | #include <stdio.h> // include the stdio.h int main () { int num ; char num_ok = 0 ; // proceed request for numbers until num_ok == 0 while ( num_ok == 0 ) { printf ( "Enter a number: " ); scanf ( "%d" , & num ); // if num >= 0 gear up num_ok = 1 and finish asking for input if ( num >= 0 ) { num_ok = 1 ; } } // calculate factorial } |
Using do while loop #
i 2 3 iv 5 6 7 eight 9 ten 11 12 xiii 14 | #include <stdio.h> // include the stdio.h int primary () { int num ; practice { printf ( "Enter a number: " ); scanf ( "%d" , & num ); } while ( num < 0 ); // keep asking for numbers until num < 0 // summate factorial } |
Detect that the solution using while loop is more involved, to achieve the same thing we accept to create an extra variable num_ok, and an additional if statement. On the other manus, the do while loop achieves the same affair without any trickery and it's more than elegant and concise.
Earlier we leave practise while loop, permit's accept i more example.
The Following program calculates Uncomplicated interest:
1 2 three iv 5 6 7 eight 9 10 11 12 13 14 15 xvi 17 18 xix 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /****************************************** Program to calculate the Simple interest * * SI = (Principal * Rate * Fourth dimension) / 100 * ******************************************/ #include <stdio.h> // include stdio.h int main () { float p , r , t ; char ch = 'y' ; do { 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 ( " \due north\n Calculate SI one more time ? ('y' for Yes, 'n' for no ) : " ); scanf ( " %c" , & ch ); // notice the preceding white space before %c } while ( ch == 'y' ); // keep request for P, R and T til the input is 'y' // bespeak to operating system everything works fine return 0 ; } |
Expected Output:
1 2 3 4 5 half dozen 7 8 9 ten 11 12 | Enter principal: 15000 Enter rate: 4.5 Enter t: three SI = 2025.00 Calculate SI 1 more time ? ('y' for Yeah, 'north' for no ) : y Enter primary: 20000 Enter rate: 5.4 Enter t: 4 SI = 4320.00 Calculate SI one more fourth dimension ? ('y' for Yes, 'n' for no ) : northward |
Source: https://overiq.com/c-programming-101/the-do-while-loop-in-c/
0 Response to "C++ What if User Types Something Other Then Yes to Run Again"
Post a Comment