Course Title: Introduction to Programming (CS201)

 

·        Lecture No. 6

 

Repetition Structure (Loop)

Overflow Condition

Sample Program 1

Sample Program 2

 

 

Properties of While loop

Flow Chart

Sample Program 3

Tips

 

 

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.

 

 

 

 

 

 

 

NEXT