Looping concept
Looping concept
Looping is the process where we can get the output of any value of our desired
times
1. Do –while
2. While
3. For
4. Switch
Those are also known as Control statement
So we discuss about for loop before we go beyond let discuss about some
certain operators
Like increment and decrement operator.
Increment/decrement
Can be divided into 2 categories – pre and post
So let discuss about pre and post increment
Int i=5;
I++;
It gives the out put 6
And also ++I it is also give same output 6
So where is the exact difference
When we use pre increment ++i in that point of time first it increment the
value by 1 i.e. i= i+1;
Then it will works
Where the post increment is first it works with by its default value then it will
increment its value i.e i=5 then it will incremented by 1.
FOR:
The syntax of for is like
for(initialized the values; condition; increment/decrement
operator)
{
//Body of for loop
}
This process goes on until the test expression is false. When the test
expression is false, the loop terminates.
int main() {
int i;
2. The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body
of for loop is executed. This will print the 1 (value of i) on the screen.
3. The update statement ++i is executed. Now, the value of i will be 2. Again,
the test expression is evaluated to true, and the body of for loop is executed.
This will print 2 (value of i) on the screen.
4. Again, the update statement ++i is executed and the test expression i < 11 is
evaluated. This process goes on until i becomes 11.
5. When i becomes 11, i < 11 will be false, and the for loop terminates.
#include <stdio.h>
int main()
{
int num, count, sum = 0;
return 0;
}
Enter a positive integer: 10
Sum = 55
NESTED FOR LOOP
:
for ( init; condition; increment ) { //OUTER LOOP handle rows
#include <stdio.h>
int main()
printf("\n");
//Output
o First, the 'i' variable is initialized to 1 and then program control passes to the i<=n.
o The program control checks whether the condition 'i<=n' is true or not.
o If the condition is true, then the program control passes to the inner loop.
o The inner loop will get executed until the condition is true.
o After the execution of the inner loop, the control moves back to the update of the outer loop,
i.e., i++.
o After incrementing the value of the loop counter, the condition is checked again, i.e., i<=n.
Basic loop Concept
o If the condition is true, then the inner loop will be executed again.
o This process will continue until the condition of the outer loop is true.