Loops are used to repeat one or more statements for a given condition.
The versitality of computer lies in it's abiility to perform a set of instruction repeatly. This involve repeating some question of program either specify no. of time vertical.
This repeative operator is done through loop control statement.
- Initialization
- Condition
- Increment/decrement
- While Loop
- Do while loop
- For loop
while (/* condition is true*/) { /* code */ ---the block keeps executing as long as the condition true }
If the condition never becomes false, the while loop keeps getting executed, such a loop is known as infinite loop
3. write a program to print natural numbers from 10 to 20 when initial loop counter is initialized to 0
The loop counter not need to be int, it can be float as well
i++ ---- i is increased by 1(i+1) i-- ---- i is decreased by 1
i++ means first print value then increment
++i means first incement then print value
The i++ method, or post-increment, is the most common way. In psuedocode, the post-increment operator looks roughly as follows for a variable i:
int j = i; i = i + 1; return j;
Since the post-increment operator has to return the original value of i, and not the incremented value i + 1, it has to store the old version of i.
The ++i method, or pre-increment, is much less common and is typically used by older programmers in languages such as C and C++.
i = i + 1; return i;
- +++ operator does not exist
- += is compound assignment operator
just like -=,+=,/=if i=5, i+=10 --> Incements i by 10-->15
very similar to while loop
While loop which executes at least once
syntax
do
{
/* code */
} while (/* condition */);
- While
- Checks the condition and then executes the code.
- Do while
- Executes the code and then checks the condition.
Syntax
for (initialize ; test ; increment or decrement)
{
/* code */
}
- Initialize
- Setting a loop counter to an initial value.
- Test
- Checking a condition.
- Increment
- Updating the loop counter.
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.
Using break statement
for (int i = 0; i < 1000; i++)
{
printf("%d\n",i);
if (i==5)
{
break;
}
}
Continue statement is used to immidiately 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.
int s =5;
int i=0;
while (i<10){
if(i!=s){
continue;
}
else
{
printf("%d",i);
i++;
}
}
- Write a program to print a multiplication table of a given number.
- Write a program to print multiplication table of 10 in reverse order
- Write a program to sum first 10 natural numbers.
a. Using For loop b. Using While loop c. Using Do while loop
- Write a program to calculate the sum of the numbers occuring in the multiplication table of 8.(Consider 8x1 to 8x10)
5. Write a program to calculate the factorial of a given number using a for loop 6. Repeat 8 using while loop 7. Write a program to check whether a given number is prime or not using loops
8. Implement 7 question with other loops
a. using while loop b.Using do while loop
9. Write a program to reverse any number
10. Write a program to add all digits of a given number
11. Write a program to sort all even and odd numbers