Senin, 02 Desember 2013

Looping Statements in Computer Programming, C Language

Introduction

We have learnt about branching statements that transfer the control from one point to another point in the computer programming. In this article, we concentrate on another important constructs like loop statements, which executes a set of statements repeatedly. Sometimes, it is required to execute a set of statements repeatedly in c programming. In such cases, the looping statements are used.

The statements that enables the programmer to execute a set of statements repeatedly till the required activity is completed, called looping statements or simply loop statements. These statements are also called repetitive or iterative statements. The statements within a loop may be executed for a fixed number of times or until a certain condition is reached. The various types of loop statements that are supported by the C language are as follows:

For Loop

A set of statements may have to be repeatedly executed for a specified number of times. In such situations, we use for loop statement. This type of loop is defined as an iterative statement that causes a set of statements to be executed repeatedly for a fixed number of times. If we know well in advance as how many times a set of statements have to be executed repeatedly, then for loop is the best choice.

The syntax of for loop as follows:
for (initialization; condition; increment/decrement)
{
    statement1;
    statement2;
    statement3;
}

Where

  • for: is the reserve word or keyword
  • Initialization: used to provide starting value to the variable. It should end with semicolon (;).
  • Condition: used to determine whether value of variable has reached the number of repetitions desired. It should end with semicolon (;).
  • Increment/Decrements: It is used to update the variable value by increment or decrements.

Note: Initialization, condition and updating statement can be multiple statements separated by comma (,).

In for loop some points have to keep in mind by c programmer, which are:

  • The initialization statement executes first and only once when the execution begins.
  • After this, condition is executed. If the condition is evaluated as the True then the body of for loop is executed. After executing the body of the loop the increment/decrements statement is executed. It is normally update the variable value by the specified step size. Then termination condition statement is executed and the process is repeated.
  • If the condition is evaluated to False, then the control comes out of the loop without executing the body of the loop. Later, the rest of the code is executed.

As long as condition is evaluated to True, the body of for loop and the updating statement are executed.
For example

for(i=1; i<=5;i++)
{
  printf(“%d”,i);
}
printf(“\nOut of the loop”);

In the above example,

  • For loop is executed for the first time and initialize the value of i =1.
  • Then, condition is checked and it is true since i = 1.
  • So, control enters into body of the loop and displays 1 on the screen using printf statement.
  • Then i is incremented by 1 and condition is checked again. The loop is repeatedly executed for i=1, 2, 3, 4 and 5, all these values are displayed one after other.
  • Finally, when i will be 6, the condition fails and control comes out of the loop and the message "Out of the loop" is displayed on the screen.

Here's an example to find sum of N natural numbers using For loop.

Algorithm:
Step1:    [Read the number of terms]
Read: N
Step2:    [Initialization]
    Sum = 0
Step3:    for i = 1 to N in steps of 1 do
    Sum = Sum + i
    [End of for]
Step4:    Write: Sum
Step5:    Exit

Looping Statements in Computer Programming, C Language


C program to find sum of natural number

main()
{
  int i,n,sum=0;
  printf("Enter the number of terms here:\n");
  scanf("%d",&n);
  for(i=1; i<=n; i++)
   {
     sum=sum+i;
   }
printf("The sun of n %d term = %d",n,sum);
getch();
}

Looping Statements in Computer Programming, C Language

Branching Statements in C

Tidak ada komentar:

Posting Komentar