Chapter 4 Loop Control Instruction
Why loops
Sometimes we want our programs to execute a few sets of instructions over and over again for ex. printing 1 to 100, the first 100 even number, etc.
Hence Loops make it easy for programmers to tell the computer that a given set of instructions must be executed repeatedly.
Types of loops:-
Primarily, there are three types of loops in C language:
1. While loop
2. do-while loop
3. for-loop
We will look into this one by one
while loop
while(Condition is true) {
// code
//code => The block keeps executing as long as the condition is true
}
An example
int i=0
while (i<10)
printf ("The value of i is %d",i); i++;
NOTE: If the condition never becomes false, the while loop keeps getting executed. Such a Loop is known as an infinite loop.
Increament and decreament operators
i++ ----> i is decreased by 1.
i-- -----> i is decreased by 1
printf ("--i - %d",--i);
This first documents i and then print it
printf ("i-- =%d",i)
This first print i and then decrement it
* +++ operator doesn't exist
* += is compound assignment operator just like -=, +=, /=,&=
do-while loop
The syntax of do-while loop looks like this
do{
//code;
//code;
}while(Condition)
do-while loop works very similar to while loop.
while---> check the condition & then execute the code
do-while ---> Executes the code & then checks the code
Do-while loop= while loop which executes at least once
for loop
The syntax of the for while loop like this;
for (initialize; test; increment or decrement)
{
//code;
//code;
//code;
}
Initialize -->
Setting a loop counter to an initial value
Test ---> Checking a condition
Increment --> updating the loop counter
An Example:
for (i=0; i<3; i++){
printf("%d",i);
printf("\n");
}
Output:
0
1
2
A case of decrement for loop
for (i=5; i, i--)
printf("%d\n",i);
This for loop will keep on running until i become 0
The loop runs in the following steps :
1. "i" is initialized to 5
2. The condition "i" is tested
3. The code is executed
4. "i" is decremented
5. Condition "i" is checked 2 code is executed if its not 0
6. & so on until i is non 0
The Break statement in C
The break statement is used to exit the loop irrespective of whether the condition is true or false.
Whenever a "break" is encountered inside the loop .
The control is sent outside the loop
Lets us see with the help of an example
for(i=0, i<1000; i++){
printf("%d\n",i);
if (i==5){
break;
}
}
Output:-
0
1
2
3
4
5
and not 0 to 1000
The Continue statement in C
The continue statement is used to immediately move to the next iteration of the loop.
The control is taken to the next iteration thus, skipping everything below "continue" inside the loop for that iteration
Let us look at an example
int skip =5;
int i=0;
while (i<10){
if (i=skip)
continue; output =5
else
printf("%d",i);
} and not 0.....9
NOTES:
1. sometimes the name of the variable might not indicate
the behavior of the program.
2. break statement completely exists the loop.
3. continue statement skips the particular iteration of the loop
Comments
Post a Comment