REPETITION / LOOPING IN C PROGRAMMING LANGUAGE
Repetiton / Looping :
- A process which is done contionusly until particular value. If the limit isn’t typed, then syntax will be error because that process will be an infinite loop.
Looping which is usually used is Counting Loops. Why is it called conting loops? Because its repetition is managed by a loop control variable whose value represents a count. It is used when we can determine how many loops will be needed exactly.
The pseudocode of Counting Loops :
Set loop control variable to an initial value of 0
While loop control variable <>
... //Do something multiple times
Increase loop control variable by 1.
Three types of Counting Loops in C Programming Language :
a. FOR
Syntax :
for(initialize; test; update)
{
//Steps to perform each iteration
}
Initialize - - > initial condition of control variable
Test - - > relational expression which is a condition
Update - - > change the value of control variable
For Example :
int p = 0;
int i;
for (i=0;i<10;i++)
{
p=p+i;
}
Note :
p = p + 1 is equal with p++ and p += 1.
Flowchart :
b. WHILE (Pre Tested
Syntax :
While(condition)
{
//Steps to perform. These should eventually
// result in condition being false
}
For Example :
int p=0;
int i=0; //initialization
while (i<10) //testing
{
p=p+i;
i++; //updating
}
Note :
If any of initialization, testing and updating are skipped, it may produce an infinite loop.
Flowchart :
c. DO-WHILE (Post Tested
Syntax :
Do
{
//Steps to perform. These should eventually
// result in condition being false
}
While (condition);
For Example :
int i=0 //initialization
int p=0;
do
{
p=p+1;
i++; //updating
}
while (i<10); //testing
Flowchart :
Debug and Test Looping Programs :
1. Using debugger programs.
2. Use several printf statement to output the value of variables.
3. Testing the programs with try and error.
0 comments:
Post a Comment