Why loops?
Sometimes we want our programs to execute a few sets of instructions over and over again. For eg. Printing 1 to 100, first 100 even numbers, etc.
Hence loops make it easy for a programmer to tell the computer that a given set of instructions must be executed repeatedly.
Types of Loops: Primarily, there are three types of loop 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 // The block keeps executing as long as the condition is true
// Code
}
An example:
Note:
If the condition never becomes false, the while loop keeps getting executed. Such a loop is known as an infinite loop.
Quick Quiz: Write a program to print natural numbers from 10 to 20 when initial loop counter i is initialized to 0.
The loop counter need not be int, it can be a float as well.
Increment and decrement operators
i++ (i is increased by 1)
i-- (i is decreased by 1)
printf(“—i=%d”,--i);
This first decrements i and then prints it
printf(“i--=%d”,i--);
This first prints i and then decrements it
- +++ operators does not exists => Important
- += is compound assignment operator just like -=, *=, /= & %= =>Also important
Do-while loop:
The syntax of do-while loop looks like this:
do {
//code;
//code;
}while(condition)
Do-while loop works very similer to while loop
While -> checks the condition & then executes the code
Do-while -> executes the code & then checks the condition
**do-while loop = while loop which executes at least once
For Loop
The syntax of for loop looks like this:
for( initialize; test; increment or decrement)
{
//code;
//code;
}
Initialize -> setting a lop counter to an initial value
Test -> checking a condition
Increment -> updating the loop counter
An example:
Output:
0
1
2
Quick Quiz: Write a program to print first n natural numbers using for loop.
A case of Decrementing for loop
for(i=5; i; i--)
printf(“%d\n”,i);
This for loop will keep on running until i becomes 0.
The loop runs in the following steps:
- i is initialized to 5
- The condition “i” (0 or none) is tested
- The code is executed
- i is decremented
- Condition i is checked and the code is executed if it's not 0.
- & so on until i is non 0.
Quick Quiz: Write a program to print n natural numbers in reverse order.
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 controls sent outside the loop.
Let us see with an example:
Output: 0, 1, 2, 3, 4, 5 and not 0 to 100
The continue statement in c
The continue statement in c is used to immediately move to the next 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:
Output: 5 and not 0................9
Notes:
1. Sometimes, the name of the variable might not indicate the behavior of the program.
2. Break statement completely exits the loop
3. Continue statement skips the particular iteration of the loop
Practice Examples
- Write a program to print the multiplication table of a given number n.
- Write a program to print multiplication table of 10 in reversed order
- A do-while loop is executed:
- At least once
- At least twice
- At most once
4. What can be done using one type of loop can also be done using the other two types of loops – True or False.
5. Write a program to sum the first ten natural numbers using a while loop.
6. Write a program to implement program 5 using for and do-while loop.
7. Write a program to calculate the sum of the numbers occurring in the multiplication table of 8.(Consider 8x1 to 8x10)
8. Write a program to calculate the factorial of a given number using for loop.
9. Repeat 8 using a while loop.
10. Write a program to check whether a given number is prime or not using loops.
11. Implement 10 using other types of loops.
Solve This Examples For Better Practice
No comments:
Post a Comment