5.8 The do-while Statement and H FIGURE 5.14 Validating Input Using do-while Statement 1. /* Returns the first integer between n_min and n_max en * Pre : n_min <= n_max 4. * Post: Result is in the range n_min through n_max. 2. 3.
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
If we wanted to rewrite the code on page 275 to use while loops instead of do-while loops, what would we have to do differently? Explain.
![275
5.8 • The do-while Statement and Flag-Controlled Loops
FIGURE 5.14 Validating Input Using do-while Statement
1. /*
Returns the first integer between n min and n max entered as data.
Pre : n_min <= n_max
2.
3.
4.
Post: Result is in the range n min through n_max.
* /
5.
6.
int
7.
get
_int (int n_min, int n_max)
8.
{
9.
int
in_val,
/* input - number entered by user
* /
10.
/* status value returned by scanf
* /
status;
11.
char
skip_ch;
/* character to skip
* /
12.
int
/* error flag for bad input
* /
error;
13.
/* Get data from user until in val is in the range.
* /
14.
do {
15.
/* No errors detected yet. */
16.
error = 0;
17.
/* Get a number from the user. */
18.
printf("Enter an integer in the range from %d ",
n_min);
19.
printf("to %d inclusive> ", n_max);
20.
status = scanf("%d", &in_val);
21.
22.
/* Validate the number. */
23.
if (status != 1) { /* in_val didn't get a number */
24.
error = 1;
scanf("%c", &skip_ch);
printf("Invalid character >>%c>>. ", skip_ch);
printf("Skipping rest of line.\n");
} else if (in_val < n_min || in_val > n_max) {
25.
26.
27.
28.
29.
error = 1;
30.
printf("Number %d is not in range.\n", in_val) ;
31.
32.
/* Skip rest of data line. */
33.
do
34.
scanf ("%c", &skip_ch);
while (skip_ch != '\n');
35.
36.
} while (error);
37.
38.
return (in_val);
39.
40. }
12.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3dacaeb0-b2a7-4104-ab9d-9efa5739e9a5%2F1aec2a37-0674-4145-b98c-2e8df34276fc%2F5qy9q5_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)