Repetitive Loop Control Structures ‘for statement’

Repetitive (Loop) Control Structures

Repetitive Loop Control Structures

Repetitive Loop Control Structures

Repetitive loop control structure is used to execute a sequence of program statements for either a specified number of times or until a particular condition is met, even though those statements happen to appear only once in the program. This repetitive operation is done through a loop control instruction.

Importance of Repetitive Control Structures

For beginners of C, it is most essential to understand the importance of repetitive control structures and why Structures would they be using these structures. Listing out the importance in points summarizes the following importance;

i.            To minimize code redundancy.

ii.            To minimize the overall storage space of a program.

iii.            To maximize the completion and execution time of a program.

iv.            To maximize the versatility of code by making it executable for desired number of times.

Using Repetitive Control Structures

The main reason behind using repetitive control structures is to execute a line of code or a group of statements for desired time until the condition is matched.

Let us take a simple example, say you need to write your name 10 times in a program.

Without using loop structures one could write the program as:

/*to write my name 10 times*/

#include<stdio.h>

void main()

{

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

printf(“The Computer Student\n”);

getch();

}

 

 

Now, writing same program using loop structures

/*to write my name 10 times*/

#include<stdio.h>

void main()

{

int j;

for( j=0; j<10; j++)

{

printf(“The Computer Students \n”);

}

getch();

}

 

In above examples, line of code was printf(“The Computer Students \n”); and condition was until the j counter reaches 10. See, using loop it decreased the writing of similar line of codes by 9 times. If you still think you want to use sequential structure due to complexity of repetitive structure then what would choose when you have to print a name for 1000 time, obviously repetitive structure would be a good option.

The ‘for’ Statement

The for loop statement is most popular and widely used looping structure. This statement allows specifying three things about a loop in a single line;

  1. Setting a loop counter to an initial value using assignment statements such as j = 1 and count = 0. The variables j and count are known as loop-control variables.
  2. Testing the loop counter to determine whether its value has reached the number of repetitions desired. The test-condition is a relational expression, such as j<10 that determines when the loop will exit. If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop.
  3. Increasing the value of loop counter each time the program segment within the loop has been executed. After single execution of body of loop the control is passed to for statement and the statement increases the control variable using increment operator i++ and the new value of control variable is again tested to see whether it satisfies the loop condition.

The general form of the for loop is

for ( initialization; test-condition; incremental; )

{

Body of loop;

}

 

Let’s take an real example,

for( x = 0; x <= 9; x = x+1)

{

printf(“%d”, x);

}

printf(“\n”);

 

This example’s output prints the digits from 0 to 9 in one line. As you see in example the three sections within parentheses must be separated by semicolons.

Remember: There is no semicolon at the end of the incremental section x=x+1.

Example to calculate the Factorial using for statement

Nested Loop with for statement

Nesting Loop statement is the way of writing loops statement, as we nested in if statements, a very similar approach is used. Let’s explore how is nested loops written in C Codes through this demonstration.

/* demonstration of nested loops */

#include<stdio.h>

main()

{

int outerloop, innerloop, loopcount;

for (outerloop = 1; outerloop<=3; outerloop++)

{

for (innerloop = 1; innerloop <=2; innerloop++)

{

printf(“Outerloop = %d  Innerloop = %d”, outerloop, innerloop);

}

}

getch();

}

 

When you run this program you will get the following output:

C Program Output

C Program Output

In this way you can nest as many loops inside your program as per the program’s need. Even you can nest multiple types of loops inside a loop.

Find examples of C Program Click here

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>