- C Programming Tutorial
- C Programming useful Resources
C Programming Sentinel Controlled Loop
- A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages − C programming language provides the following type of loops to handle looping requirements.
- Sentinel Controlled Loop When we don't know exactly know how many times loop body will be executed known as Sentinel Controlled Loop, for example - Reverse a given number, such kind of problem will be solved using sentinel controlled loop. Consider the code snippet.
- Selected Reading
(c) sentinel loop. A loop structure that tests the loop condition after executing the loop body is called a (d) post-test loop (although choice (b)-loop-and-a-half-is a strategy used to execute post-test loops in Python) A priming read is part of the pattern for a(n) (c) sentinel loop (p. Instructor What are sentinel values?In programming, we can use sentinel valuesto indicate the end of a loop,or the end of a series of input.So if we have loop that asks the userto enter in a list of names,we have to have some way for the userto identify that they're done entering valid names.So maybe we'll tell them to enter a qto indicate they're done.Let's write a program that uses a sentinel. The break statement in C programming has the following two usages − When a 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).
The break statement in C programming has the following two usages −
When a 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, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.
Syntax
The syntax for a break statement in C is as follows −
Sentinel Loop Syntax C++
Flow Diagram
Sentinel While Loop C++
Example
When the above code is compiled and executed, it produces the following result −
PowerPoint pptx | Acrobat pdf
Objectives
While engaging with this module, you will..
- become literate in the C++ syntax of looping
- apply your understanding of loops from the last module to create a working C++ loop
- analyze the differences between a while and a do-while loop
The while Loops
The syntax for the while is:
while(expression)
statement
And the details:
- while is a reserved word.
- Expression must be fully contained within the parentheses.
- expression is a valid C++ expression that evaluates to true or false or a numerical value.
- statement is a simple or compound C++ statement (with all semi-colons included).
- This is a pre-test loop. This means that the condition in expression is checked before the body of the while loop (statement) might possibly be executed. This implies that the body of the loop may never be executed.
- How it works: expression is evaluated. If it is true, the body (statement) is executed and control passes back up to expression to be evaluated again. If it is false, control passes out of the loop statement.
So, it is easy to see that, as the loop executes, something in the controlling expression must change! If not, then the loop will execute the body of the loop ad infinitum, or until the power company cuts your power. Since that may take a while (no pun intended), let's just avoid them.
Beatles muro do classic rock. while (7)
cout<<'hello'<
This is a ridiculous loop. It is an infinite loop. Remember that 7 ≠ 0 is true. The expression 7 is checked for truth value, the output statement is executed, 7 is checked for value and is true, the output statement is executed again, etc ad infinitum. This loop has no LCV, hence no initialization, check, or update of the LCV.
cout<<'enter number between 5 and 89, inclusive: ';
cin>>input;
while (input < 5 || input > 89)
{
cout<<'that value is unacceptable.. try again: ';
cin>>input;
}
cout<<'the value '<
Thus, we can now 'cleanse' user input in the sense that we can range check values prompted for. Note: in this course, you will always assume that the value given upon prompt of a user is the right type. That is, if you prompt for a char, you get a char; if you prompt for a number, you get a number, etc. The code to insure this is long and we don't need to fool with it. So you see that the input from the initial prompt/readin is the LCV that is checked in the while expression, updated inside the loop, and initialized before the loop. Execution cannot leave the loop until proper input is obtained.
long sum = 0;
short counter = 1; // LCV initialization
while (counter <= 100) // LCV check
{
sum += counter;
counter++ ; // LCV update
}
cout<<'sum of first 100 integers is '<
Here's an example that could demonstrate a tutorial for learning math:
const string QUESTION1 = 'What is 8 – 3 ?'; //if question1 changes, change ANS_Q1
const short ANS_Q1 = 5;
cout<<'QUESTION #1: '< cin>>ans;
while ( (ans – ANS_Q1) != 0 )
{
cout<<'Your answer is incorrect. Please try again.'< cout< cin>>ans;
}
cout<<'Your answer is CORRECT! Congratulations!'<
Sentinel While Loop
So, you see that there is a computation in the expression that controls the loop. This is perfectly permissible. More complicated expressions are indeed allowed. Notice that, in this last example for instance, if you remove the cin>>ans; statement inside the loop, you will end up with an infinite loop. Why?
Now, you can see that the while statement is a pre-check loop; the condition (expression) is checked before the loop body is repeated (or not). Thus, the body is never guaranteed to execute even once.
The do-while Statement:
The C++ syntax is:
do
statement
while (expression);
And the details:
const string QUESTION1 = 'What is 8 – 3 ?'; //if question1 changes, change ANS_Q1
const short ANS_Q1 = 5;
cout<<'QUESTION #1: '< cin>>ans;
while ( (ans – ANS_Q1) != 0 )
{
cout<<'Your answer is incorrect. Please try again.'< cout< cin>>ans;
}
cout<<'Your answer is CORRECT! Congratulations!'<
Sentinel While Loop
So, you see that there is a computation in the expression that controls the loop. This is perfectly permissible. More complicated expressions are indeed allowed. Notice that, in this last example for instance, if you remove the cin>>ans; statement inside the loop, you will end up with an infinite loop. Why?
Now, you can see that the while statement is a pre-check loop; the condition (expression) is checked before the loop body is repeated (or not). Thus, the body is never guaranteed to execute even once.
The do-while Statement:
The C++ syntax is:
do
statement
while (expression);
And the details:
- do and while are reserved words.
- Expression must be fully contained within the parentheses.
- expression is a valid C++ expression that evaluates to true or false or a numerical value.
- statement is a simple or compound C++ statement (with all semi-colons included).
- Do not forget the semicolon after the closing paren.
- This is a post-test loop. This means that the condition in expression is checked after the body of the loop (statement) is executed. This implies that the body of the loop will always be executed at least once. I emphasize this point because it is very relevant.
- How it works: Statement is executed. Then, expression is evaluated. If it is true, statement is executed again. If false, control passes out of the do-while statement.
Thus, you see that in this type of loop, the body of the loop will be executed at least once. Thinking of what you might want done at least once but possibly many times, prompting/reading in comes to mind.
short age, count = 0;
do
{
cout<<'Please enter your age: ';
cin>>age;
count++;
if (age <= 0 || age > MAX_AGE)
{
cout<<'This is not a valid value!'< if (count MAX_ALLOWABLE_TRIES)
exit(1); // continued bad input; bailing out!
}
} while (age <= 0 || age > MAX_AGE); Windows 7 64-bit download.
Thus, we may now range-check out user input with a single prompt/read in. The user cannot exit this loop without entering reasonable information. Notice also that I have put the while on the same line as the closing brace. This is done not for the compiler, but for formatting reasons. If the while was put on the next line, it appears that we have begun another while loop, confusing anyone who reads the code. Also notice that I have included a counter variable that will keep track of how many times the user attempts to enter information. If that number of attempts reaches a predetermined 'level of tolerance' (MAX_ALLOWABLE_TRIES), then the exit function is invoked and the run of the program terminated.
For example, prompt for and read in an integer between 2 and 174, inclusive, that is a multiple of three.
do
{
cout<<'Enter an integer between 2 and 174 (inclusive) that is a multiple of 3: ';
cin>>input;
} while (!(input >= 2 && input <= 174 && input%3 0));
Here I have built a logical expression that describes what I want to include, and then negated it with the ‘!' operator. C2800nm advsecurityk9 mz 151 4 m10 bin.