Repetition Structure (Loop)

In our day to day life, most of the things are repeated. Days and nights repeat themselves 30 times a month. Four seasons replace each other every year. We can see similar phenomenon in the practical life. For example, in the payroll system, some procedures are same for all the employees. These are repeatedly applied while dealing with the employees. So repetition is very useful structure in the programming.
Let’s discuss a problem to understand it thoroughly. We have to calculate the sum of first 10 whole numbers i.e. add the numbers from 1 to 10. Following statement may be one way to do it.
cout << “Sum of first 10 numbers is = “ << 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; This method is perfectly fine as the syntax is right. The answer is also correct. This procedure can also be adopted while calculating the sum of numbers from 1 to 100. We can write the above statement adding all the digits from 1 to 100. But this method will not be suitable for computing the sum of numbers from 1 to 1000.The addition of a very big number of digits will result in a very ugly and boring statement. Let’s analyze it carefully. Our first integer is 1, is there any other way to find out what is the next integer? Yes, we can add 1 to the integer and get the next integer which is 2. To find the next integer (i.e. 3) we add 1 to the previous integer (i.e. 2) and get the next integer which is 3. So whenever we have to find out the next integer, we have to add 1 to the previous integer. We have to calculate the sum of first 1000 integers by taking a variable sum of type int. It is a good programming practice to initialize the variable before using it. Here, we initialize the variable sum with zero.

Now we get the first integer i.e. 1. We add this to the sum (sum becomes 0 + 1 = 1). Now get the next integer which can be obtained by adding 1 to the previous integer i.e. 2 and add it to the sum (sum becomes 1 + 2 = 3). Get the next integer by adding 1 to the previous integer and add it to the sum (sum becomes 3 + 3 = 6) and so on. This way, we get the next integer by adding 1 to the previous integer and the new integer to the sum. It is obvious that we are repeating this procedure again and again i.e. adding 1 to the previous integer and add this new integer to the sum. So we need some repetition structure in the programming language. There are many looping constructs in C Language. The repetition structure we are discussing in this lecture is 'while loop structure'. ‘while’ is also a key word of 'C' so it cannot be used as a variable name.
While means, 'do it until the condition is true'. The use of while construct can be helpful in repeating a set of instructions under some condition. We can also use curly braces with while just like we used with if. If we omit to use the braces with while construct, then only one statement after while will be repeatedly executed. For good programming practices, always use braces with while irrespective of the number of statements in while block. The code will also be indented inside the while block as Indentation makes the code easy to understand.
The syntax of while construct is as under:


The logical expression contains a logical or relational operator. While this logical expression is true, the statements will be executed repeatedly. When this logical expression becomes false, the statements within the while block, will not be executed. Rather the next statement in the program after while block, will be executed.
Let’s discuss again the same problem i.e. calculation of the sum of first 1000 integers starting from 1. For this purpose, we need a variable to store the sum of integers and declare a variable named sum. Always use the self explanatory variable names. The declaration of the variable sum in this case is:


The above statement has performed two tasks i.e. it declared the variable sum of type int and also initialized it with zero. As it is good programming practice to initialize all the variables when declared, the above statement can be written as:


Here we need a variable to store numbers. So we declare a variable number of type int. This variable will be used to store integers.



As we have declared another variable of int data type, so the variables of same data type can be declared in one line.


Going back to our problem, we need to sum up all the integers from 1 to 1000. Our first integer is 1. The variable number is to be used to store integers, so we will initialize it by 1 as our first integer is 1:


Now we have two variables- sum and number. That means we have two memory locations labeled as sum and number which will be used to store sum of integers and integers respectively. In the variable sum, we have to add all the integers from 1 to 1000. So we will add the value of variable number into variable sum, till the time the value of number becomes 1000. So when the value of number becomes 1000, we will stop adding integers into sum. It will become the condition of our while loop. We can say sum the integers until integer becomes 1000. In C language, this condition can be written as:



The above condition means, 'perform the action until the number is 1000 or less than 1000'. What will be the Action? Add the number, the value of number is 1 initially, into sum. This is a very simple statement:


Let’s analyze the above statement carefully. We did not write sum = number; as this statement will replace the contents of sum and the previous value of sum will be wasted as this is an assignment statement. What we did? We added the contents of sum and contents of number first (i.e. 0 + 1) and then stored the result of this (i.e. 1) to the sum.
Now we need to generate next integer and add it to the sum. How can we get the next integer? Just by adding 1 to the integer, we will get the next integer. In ‘C’, we will write it as:


Similarly in the above statement, we get the original contents of number (i.e. 1). Add 1 to them and then store the result (i.e. 2) into the number. Now we need to add this new number into sum:


We add the contents of sum (i.e. 1) to the contents of number (i.e. 1) and then store the result (i.e. 2) to the sum. Again we need to get the next integer which can be obtained by adding 1 to the number. In other words, our action consists of only two statements i.e. add the number to the sum and get the next integer. So our action statements will be:


Putting the action statements in while construct:


Let's analyze the above while loop. Initially the contents of number is 1. The condition in while loop (i.e. number <= 1000) will be evaluated as true, contents of sum and contents of number will be added and the result will be stored into sum. Now 1 will be added to the contents of number and number becomes 2. Again the condition in while loop will be evaluated as true and the contents of sum will be added to the contents of number .The result will be stored into sum. Next 1 will be added to the contents of number and number becomes 3 and so on. When number becomes 1000, the condition in while loop evaluates to be true, as we have used <= (less than or equal to) in the condition. The contents of sum will be added to the contents of number (i.e. 1000) and the result will be stored into the sum. Next 1 will be added to the contents of number and number becomes 1001. Now the condition in while loop is evaluated to false, as number is no more less than or equal to 1000 (i.e. number has become 1001). When the condition of while loop becomes false, loop is terminated. The control of the program will go to the next statement following the ending brace of the while construct. After the while construct, we can display the result using the cout statement. cout << “ The sum of first 1000 integers starting from 1 is “ << sum; The complete code of the program is as follows:

The output of the program is:


While construct is a very elegant and powerful construct. We have seen that it is very easy to sum first 1000 integers just with three statements. Suppose we have to calculate the sum of first 20000 integers. How can we do that? We just have to change the condition in the while loop (i.e. number <= 20000). Overflow Condition
We can change this condition to 10000 or even more. Just try some more numbers. How far can you go with the limit? We know that integers are allocated a fixed space in memory (i.e. 32 bits in most PCs) and we can not store a number which requires more bits than integer, into a variable of data type, int. If the sum of integers becomes larger than this limit (i.e. sum of integers becomes larger than 32 bits can store), two things can happen here. The program will give an error during execution, compiler can not detect such errors. These errors are known as run time errors. The second thing is that 32 bits of the result will be stored and extra bits will be wasted, so our result will not be correct as we have wasted the information. This is called overflow. When we try to store larger information in, than a data type can store, overflow condition occurs. When overflow condition occurs either a run-time error is generated or wrong value is stored.

For previous lesson click here: if/else Sample Program
For next lesson click here: While Sample Program


the easiest way to learn programming
introduction to programming
Repetition Structure (Loop)

0 comments:

Post a Comment

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger