General Discussion Of Loops: A Programming Construct

general Discussion Of Loopsa Loop Is A Programming Construct

C Loopsgeneral Discussion Of Loopsa Loop Is A Programming Construct

C++ Loops General Discussion of Loops A loop is a programming construct that allows a group of statements, called the loop body, to be executed zero or more times. For example, a loop might cause a group of statements to be executed 12 times. A loop is controlled in part by the loop continuation condition. As long as the loop continuation condition remains true, the loop will continue to execute. In order for a loop to stop, it must have a terminating action, i.e. statement(s) in the loop body which when executed insure that the continuation condition will eventually become false.

A loop control variable is a variable whose change may cause the continuation condition to become false. A common error is to not initialize the control variable. To help ensure that the loop does not become an infinite loop, always make sure to do the following three things with the LCV (loop control variable): 1) Initialize the LCV, almost always done before the loop. 2) Test the LCV, this is done inside the continuation condition. 3) Update or change the LCV, inside the loop body.

There are three kinds of loops that are commonly available in programming languages. 1) The while loop executes an indefinite and possibly zero number of repetitions. 2) The do-while loop executes an indefinite number of times, but always at least once. 3) The for loop is convenient when the number of repetitions is known in advance. C++ Loops The above three loop types are available in C and C++, but the while loop is the most often used; lets see some examples.

Example 1: A while loop that determines the number of digits in a positive integer, N. Assume that N has previously been declared and given a value. int Copy = N; // don’t want to destroy N! DigitCount = 1; // all numbers have at least 1 digit while ( Copy >= 10) // count the rest of the digits { ++DigitCount; // found one more digit Copy /= 10; // get rid of rightmost digit } When the loop finishes, DigitCount holds the number of digits in the integer N. Example 2: A do-while loop that determines the number of digits in integer, N, same as above. int Copy = N; // don’t destroy original N DigitCount = 0; // do-while loop will increment this at least once do { ++DigitCount; // found one more digit Copy /= 10; // get rid of rightmost digit } while ( Copy != 0); // count the rest of the digits.

Note the ‘;’ A for loop could also be used to count digits, but let’s instead look at an example more suited to the strong points of the for loop. Example 3: A loop to print all of the upper case letters. char Ch; for ( Ch = ‘A’; Ch

It is not difficult to see that a for loop is a “repackaged†while loop. Note that the for loop code is more compact, and is not always easily read as the equivalent while loop. Choosing a Loop Following the rules below will lead to code that is easier to write and read: If the number of loop repetitions is known in advance, favor the for loop. If the number of loop repetitions is NOT known in advance, but may be zero, favor the while loop. If the number of loop repetitions is NOT known in advance, but is at least 1, favor the do-while loop.

Avoid temptations to always use a for loop, because it leads to more compact code! Exercise: What would be the loop of choice in writing the following code? 1) Code to print the numbers from 1 to 100. 2) Code that requires the user press the enter key to continue. The answer to 1) is clearly a for loop.

The code below displays the numbers from 1 to 100 in rows that each has 10 numbers. Note that control variable can be declared via the initial for loop statement. for ( int N = 1; N

It also is typical of a common loop technique, i.e. get first item, N while N is not equal to stop value { get next item, N } / LAB #8 source : LAB8.CPP Action : Practice in hand tracing through program and using the IDE debugger. The comments have purposely been left out ---------------------------------------------------------------------/ #include using namespace std; void Just_Peachy(int A, int B, int & N); void main() { char Ch = 'A'; int X, Y; X = 4.56; Y = int(Ch); for (int i = 5; i >= 3; i--) { Just_Peachy(i, X, Y); if (char(Y) == 'E') Ch = 'W'; } } /**/ void Just_Peachy(int A, int B, int & N) { int T, Mod; Mod = A % B; if (Mod == 0) T = 2; else T = 1; N = N + T; } C101 Laboratory Session #8 Lab Goals: 1) Learning how to hand trace through a program.

2) Learning how to use the IDE debugger with Microsoft visual C++. For the following lab you will need to copy the file “lab8.cpp†from my website. This lab has two parts; the first part is to hand trace through the attached program and answer the questions about it. The second part is to use the built in debugger to verify most of your answers and see how helpful the debugger can be in solving logic problems with your program. For more detailed information on the debugger see the given handout on it. #include using namespace std; void Just_Peachy(int A, int B, int & N); void main() { char Ch = 'A'; int X, Y; X = 4.56; Y = int(Ch); for (int i = 5; i >= 3; i--) { Just_Peachy(i, X, Y); if (char(Y) == 'E') Ch = 'W'; } } /**/ void Just_Peachy(int A, int B, int & N) { int T, Mod; Mod = A % B; if (Mod == 0) T = 2; else T = 1; N = N + T; } 1) How many reference parameters are there in the Just_Peachy function? ____________ 2) How many value parameters are there in the Just_Peachy function? _______________ 3) How many total local parameters are there in the Just_Peachy function? ___________ 4) What is the initial value of the variable “Y’? ______________________ 5) Before the first call to the function, what is the value of the variable “T’? __________ 6) How many times does the “for’ loop execute? ________________ 7) After the first time through the “for’ loop what are the values of the following: X ___________ Y ______________ Ch _______________ 8) At the first time in the function and just before you leave it, what is value of “Mod’? _____ 9) For what value of “i’, if any, does the “if’ statement come true in the main function? ______ 10) For what value of “i’, if any, will “Mod == 0’ in the function? _____________ 11) When the “for’ loop is done what are the values of: X _______ Y _______ Ch ________ T ________ N ________ A _______ B ______ 12) What is the ASCII value of ‘E’? ____________ C101 Laboratory Session #7 Lab Goals – 1) Learning how to write functions.

2) Writing functions that return information back via the function name. 3) Writing functions that return information back via reference parameters. Write a program that has user enter three integers, the largest of the three will then be displayed. Use a function that will accept the three integers from the main function and then calculate the maximum of those three and pass it back. You need to do this problem two ways.

The first way is to write a function that returns the maximum value back using a value returning function, that is it uses the word return, with a value, in the function body. The second way is to write the function so that the largest integer is passed back via a reference parameter, so now the function is a void function. You need to have two separate programs to do this or you could do it in one program, which ever you find easier. You also must have a loop structure of some kind that allows the user to continue until they press ‘N’ or ‘n’ to stop. Use an if –else if structure to determine which number is the largest.