0% found this document useful (0 votes)
1 views

Looping concept

The document explains the concept of loops in programming, detailing types such as 'do-while', 'while', 'for', and 'switch'. It provides examples of the 'for' loop syntax, increment/decrement operators, and nested loops, along with sample code for printing numbers and calculating the sum of natural numbers. The document emphasizes the execution flow of loops and their termination conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Looping concept

The document explains the concept of loops in programming, detailing types such as 'do-while', 'while', 'for', and 'switch'. It provides examples of the 'for' loop syntax, increment/decrement operators, and nested loops, along with sample code for printing numbers and calculating the sum of natural numbers. The document emphasizes the execution flow of loops and their termination conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic loop Concept

Looping is the process where we can get the output of any value of our desired
times

We can divided loop as follows

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.

Same things will be occurred by decrement operator.

FOR:
The syntax of for is like
for(initialized the values; condition; increment/decrement
operator)
{
//Body of for loop
}

 The initialization statement is executed only once.


 Then, the test expression is evaluated. If the test expression is evaluated to
false, the for loop is terminated.
 However, if the test expression is evaluated to true, statements inside the
body of the for loop are executed, and the update expression is updated.
 Again the test expression is evaluated.
Basic loop Concept

This process goes on until the test expression is false. When the test
expression is false, the loop terminates.

// Print numbers from 1 to 10


#include <stdio.h>

int main() {
int i;

for (i = 1; i < 11; ++i)


{
printf("%d ", i);
}
return 0;
}
//output
1 2 3 4 5 6 7 8 9 10
1. i is initialized to 1.
Basic loop Concept

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.

// Program to calculate the sum of first n natural numbers


// Positive integers 1,2,3...n are known as natural numbers

#include <stdio.h>
int main()
{
int num, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// for loop terminates when num is less than count


for(count = 1; count <= num; ++count)
{
sum += count;
}

printf("Sum = %d", sum);

return 0;
}
Enter a positive integer: 10
Sum = 55
NESTED FOR LOOP
:
for ( init; condition; increment ) { //OUTER LOOP handle rows

for ( init; condition; increment ) { //INNER LOOP handle coloumn


statement(s);
//Put your any logic here
}
statement(s); //if suppose you want to change the rows then put \n here
}
Basic loop Concept

#include <stdio.h>

int main()

int n;// variable declaration

printf("Enter the value of n :");

// Displaying the n tables.

for(int i=1;i<=n;i++) // outer loop

for(int j=1;j<=10;j++) // inner loop

printf("%d\t",(i*j)); // printing the value.

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.

You might also like