Fundamentals of Programming & Procedural Programming

Universität Duisburg-Essen       PRACTICAL TRAINING TO THE LECTURE    Fundamentals of Programming  & Procedural Programming        Session Three: ...
Author: Ashlee Hancock
18 downloads 0 Views 203KB Size
Universität Duisburg-Essen      

PRACTICAL TRAINING TO THE LECTURE   

Fundamentals of Programming  & Procedural Programming 

     

Session Three: Iteration: Finite and Infinite Loops, Break and Continue        

Name:

Matriculation-Number:

First Name:

Group-Number:

Tutor:

Date:

          Prof. Dr.Ing. Axel Hunger Dipl.-Ing. Joachim Zumbrägel Universität Duisburg-Essen Faculty of Engineering, Department Electrical Engineering and Information Technology Computer Engineering

Procedural Programming/Lab3   

 



Introduction  The ability to execute repeatedly a same set of statements is a fundamental capability of a  computer. This is known as ‘iteration’ or in other words as ‘looping’. A loop is used to execute a set  of statements within a program.  So, for example, a program may ask a user to re‐enter a data if they enter invalid data values. If the  user still enters invalid data values, it can again ask the user whether the user wants to retry or stop.  This process would keep on repeating till the user enters valid data values or quits. 

What is a loop?  A loop keeps on executing a set of statements till a particular condition is met, i.e. true or false. In C,  there are basically three forms of loops known as the for loop, the while loop, and the do‐while  loop. 

The For Loop  Let’s start off with an example. If you wanted to print numbers 1 to 5 on the same line, you could  write a program in C as  Example:  #include int main() { int num = 1; printf("%d printf("%d printf("%d printf("%d printf("%d

", ", ", ", ",

num++); num++); num++); num++); num++);

return 0; }

  Though the above example would work fine, yet we could rewrite it by using a for loop:  #include int main() { int num; for (num = 1; num