Question

In: Computer Science

What are pre-test loops. Which C++ statements use pre-test loops? Why pre-test loops are called 0...

  • What are pre-test loops.
  • Which C++ statements use pre-test loops?
  • Why pre-test loops are called 0 to N loops?
  • When do you use the pre-test loops? Give at least one example.

Solutions

Expert Solution

1). Pre-Test loops:-

"Pre-Test loops are kind of loops where loop condition is get evaluated first and according to the condition result, block of code inside the loop is get executed."

For exampe:-

"for" loop

"while" loop

Lets analyze structure of "for" loop.

"for(<Intialization>; <condition>; <updation of conditional variabe>)"

where, Initialization happens only first execution of loop,

then condition gets evaluated,

If condition results in "true", code inside the loop gets executed.

then updation happens,

Again condition gets evaluated,

If condition results in true, code inside the loop gets executes, otherwise loop breaks.

2). C++ statements which uses pre-test loop:-

"for" and "while" are two statements which uses pre-test loop mechanism in C++.

I have explained the mechanism of "for" statement in previous section where i've defined pre-test loop,

"while" does same as "for". But unlike "for" Initialization always happens before "while" statement, while with "for", we have choice, either we can intialize loop variable before the "for" loop or inside the paranthesis of "for" loop.

structure of "while" loop,

<Initialization of variable>

while(<condition>) {

// loop body

<updation>

}

<updation> step can be anywhere inside the block of loop, either at the top of the code or in between or at the end of the block.

3). Pre-Test loops are also called 0 to N loops because they execute from 0 to N times. For example if loop condition results in false at first check then loop won't execute at all, in this case it will execute 0 times otherwise loop will execute until condition results in false, at most for N times, thats why it is called 0 to N loop.

4.) Whenever it is strictly required not to execute loop if condition results in false or whenever we require execute a logic if condition results in true, we use pre-test loops.

for example if we are asked to sum up numbers from 0 to some value let say 'n-1' and 'n' can be anything from 0 to some large number (100 or 10000 or 10000000 anything, greater than or equal to 0), we will pre-test loops. Let say if we are to find out the sum of first 10 natural numbers (1-10, here n = 11) then loop structure will be something like this,

ans = 0;

for(i = 1; i < 11; i++) {

ans += i;

}


Related Solutions

c programing language This assignment, which introduces the use of loops, solves the following problem: given...
c programing language This assignment, which introduces the use of loops, solves the following problem: given a starting location in a city, how long does it take a “drunken sailor” who randomly chooses his direction at each intersection to reach the city’s border? You will read input values to set up the problem parameters, run several trials to determine an average number of steps for the sailor to reach the border, and output the results. This problem is an example...
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I...
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I need it in C++. Given an input string, set result to a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceded or followed by a letter -- so for example the "is" in "this" does not count.   • for input of "is test" → "is not test" • for input...
Objectives To reinforce the use of If-Else statements To learn how to use while loops Introduction:...
Objectives To reinforce the use of If-Else statements To learn how to use while loops Introduction: Mission to Mars Your friend has been playing a new Mars Colony simulator nonstop! They are always talking about how cool it would be if they could be a on the first real-life mission to Mars! To amuse your friend, you have decided to create a series of programs about the possible first colony on Mars. Problem: Where Can We Get the Best Deal?...
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
Code in c++, do not use loops, make it as simple as it can be. Thanks!...
Code in c++, do not use loops, make it as simple as it can be. Thanks! Background Well it has finally happened; AMC’s “The Walking Dead” has become a reality. The zombie apocalypse took place. It has been a couple of years since we have started to “rebuild” our society, and now is the time for you to be able to shine. We have come across technology that will allow for us to get back to life as it once...
Use nested for loops to generate the following patterns: C++ ***** ***** ***** (no space between...
Use nested for loops to generate the following patterns: C++ ***** ***** ***** (no space between rows of asterisks) * ** *** **** ***** (Again, no empty lines between rows) * ** *** ** * (no lines between rows) Last one, let the user pick the pattern by specifying the number of rows and columns.
Briefly explain to a non-statistician why ANOVA (which is a test of means) is called analysis...
Briefly explain to a non-statistician why ANOVA (which is a test of means) is called analysis of variance rather than analysis of means? what variances do we compare in anova?
Use RStudio. To test if a middle school class on geography is working, a pre- and...
Use RStudio. To test if a middle school class on geography is working, a pre- and post-test were given to students at the start and end of the semester. Assume that the scores were randomly selected from the two tests. Also, assume that that they are pairs of scores for ten students. Use the following data to test if the class improved students’ knowledge of geography. (hint: use “var.equal=TRUE” in your argument) Scores Pre-test:     77, 56, 64, 60, 57,...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if statements. . Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. • for input of "xxcaazz", "xxbaaz" → 3 • for input of "abc", "abc" → 2 • for input...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program simple. Q (5) Suppose you have been given the task to design a text editor which will take any multiline text from user and then display the statistics like total number of characters i.e., characters_count (excluding the white space and punctuations), words_count, and redundant_words_count. Create a structure named Text_Editor having four type members namely inserted_text (of type string), characters_count (of type unsigned int), words_count...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT