Pages

Monday, October 19, 2009

Loops

Loops or iterative statements are a very important ingredient of a good program. They can be defined as statements which repeats a block of code continuously. They are used when we want to execute a particular statement or statements till the user wishes. Without a loop if the user want to perform a particular task repeatedly he/she will have to write the code again and again manually. Copy/paste utility will save us from the trouble of typing but the program length will increase even then. The following drawbacks of manual typing proves that it is not a good idea to repeat the same code again and again.

1. More memory : More lines of code will require more memory to compile and to store the program in the disc.

2. More time : Typing the code again and again will need more time. More effort will be needed to type the code.

3. Difficult to debug : A large program will also be difficult to debug(making it error free).

4. Inaccurate : To check whether it is running for the exact number of times as we wanted will have to checked by manual counting. This can cause inaccuracy since manual counting can be incorrect.

These problems are sufficient to prove that it is not a good idea to use manual coding to perform repeated tasks. The solution is Loops.

C offers three type of loops

1. For

2. While

3. Do –While

Let’s explore how “for” works


The for loop

For is a time based loop and is used when we already know that how many time a task is to be done. For example if we are asked to print a message say “Programming is fun” for 30 times we can use this loop since we know that the task it to be performed 30 times.

Syntax:

for(start;condition;increment/decrement)

{

loop body

}

Where

· Start : Denotes the starting point of the loop. This is the value from where the loop will begin it execution.

· Condition : the loop continues to run as long as this condition is true. As soon as the condition becomes false the loop terminates.

· Increment /decrement : increases or decreases the value of the variable so that at any point the condition becomes false.

Example 1 : To print “C is fun” 30 times

void main()

{

int i;

for(i=1;i<31;i++)

{

printf(“\n C is fun”);

}

}

Run the program and you will the message “C is fun” printed 30 times.

Note :

1. Use can use any other variable name instead of “i” like – for(a=1;a<31;a++)

2. Running the above code 30 times can also be achieved through the following statement since all of them have the same meaning :

a. for(i=1;i<=30;i++)

b. for(i=0;i<30;i++)

c. for(i=100;i<131;i++)

d. for(i=250;i<=280;i++)

let’s compare the program with the syntax mentioned above:

· for (i=1; -- is the start value. It denotes that the initital value of I will be 1 so the loop will start from 1.

· i<31>

· i++ : the third part informs the compiler to increase the value of I by 1. If this part is omitted I will not be incremented and hence will stay at 1 making the condition false forever. Such a loop is called a “Infinite loop”

Program 2 – Printing the counting from 1 to 100

void main()

{

int i;

for(i=1;i<=100;i++)

printf(“\n%d”,i);

}

The output of the above program will be printing of numbers from 1 to 100. Inside the loop the value of i is being printed which increments by 1 after each step. It works like this :

i++ increments the value by 1 , similarly we can increment the variable by any other value by using the short hand assignment operator.

Program 3 – Incrementing the value by any other number

Let’s create a program to print even numbers from 1 to 50.

void main()

{

int i;

for(i=2;i<=50;i+=2)

printf(“\n%d”,i);

}

Note that the above for statement has “i+=2” in the increment section. This will increment the value of I by 2 rather than 1. Therefore you will a series of 2,4,6,8,….48,50. As you can see it is fairly easy to change the increment value to any number. For example i+=5,i+=20 will increase the value by 5 and 20 respectively.

Decrement Loops

We can also write decrement loops which will instead of incrementing the value decrements it. For this we’ll just have to replace the increment operator (++) with the decrement operator (- -). Take a look at the following program that prints the same series created before but in decreasing order.

void main()

{

int i;

for(i=10;i>0;i--)

printf(“\n%d”,i);

}

The above program will print the series 10,9,8,….1 since we are using (--) operator.


Some practical examples :

Example 1 : Program to print multiplication table of any number entered by the user.

void main()

{

int num,i,result;

printf(“\nEnter any number : “);

scanf(“%d”,&num);

for(i=1;i<=10;i++)

{

result=num*i;

printf(“\n%d”,result);

}

}

Example 2 : program to print sum of 10 numbers entered by the user

void main()

{

int num,i,sum=0;

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

{

printf(“\nEnter a value : “);

scanf(“%d”,&num);

sum+=num;

}

printf(“\nSum of all numbers is %d”,sum);

}

Example 3 : program to print the details of the highest scorer of an examination

void main()

{

int i,rollno,topper_rollno;

float per,topper_per,max=0.0;

char name,topper_name;

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

{

printf(“\nenter the details of student number %d : “,i+1);

printf(“\nenter rollno : “);

scanf(“%d”,&rollno);

printf(“\nenter name : “);

scanf(“%c”,&name);

printf(“\nenter perecentage : “);

scanf(“%f”,&per);

if(per>max)

{

max=per;

topper_rollno=rollno;

topper_name=name;

}

}

printf(“\nfollowing are details of the topper : “);

printf(“\nrollno : %d”,topper_rollno);

printf(“\nname : %c”,topper_name);

printf(“\npercentage : %f”,max);

}

Example 4 : program to count number of even and odd values

void main()

{

int i,no,even=0,odd=0;

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

{

printf(“\nenter a value : “);

scanf(“%d”,&no);

if(no%2==0)

even++;

else

odd++;

}

printf(“\ntotal number of even values : %d”,even);

printf(“\ntotal number of odd values : %d”,odd);

}

0 comments:

Post a Comment