Skip to main content

C Programming #6 (Loop Control Instruction)



In C Programming if we want to repeat some statements here we make the use of loops.
There are three methods by which we can repeat part of program.

  1. Using for statement
  2. Using a while statement
  3. Using a do-while statement

The while loop:
It will repeat the program until the condition in while loop become false.
Example:
/*Calculation of simple interest for 3 sets of p, n and r*/
#include<stdio.h>
int main()
{
int p,n,count;
float r,si;
count=1;
while(count<=3)
{
printf("\nEnter values of p,n and r");
scanf("%d%d%f",&p,&n,&r);
si=p*n*r/100;
printf("Simple interest = Rs. %\nf",si);
count=count+1;
}
return 0;
}
Here this program while loop has the condition to repeat the program 3 times.
As in while loop at the end count is increased by 1 every time it execute and when it reaches to 3 control comes out of program and the program terminates.
Tips and Traps
  • As I mentioned in my previous blogs you can use logical operators anywhere in the program.
Example:
while(i<=10)
while(i>=10&&j<=15)
while(j>10&&(b<15||c<20))
  • Writing programs in faster
Example:
while(i<=10)
{
i=i+1;
}
is same as

while(i<=10)
{
i++
}

Note:For more tips and traps stay connected on our blogs and any kind of questions are most welcome.

Comments