0% found this document useful (0 votes)
64 views41 pages

Loop in C

The document discusses loops in C programming. There are three types of loops - for, while, and do-while loops. The for loop repeats a block of code a specific number of times based on a counter variable. It has three expressions for initialization, condition, and increment/decrement. The while loop repeats as long as a condition is true. It checks the condition first before running the code block. The do-while loop is similar but runs the code block at least once before checking the condition.

Uploaded by

mariamgodwin987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views41 pages

Loop in C

The document discusses loops in C programming. There are three types of loops - for, while, and do-while loops. The for loop repeats a block of code a specific number of times based on a counter variable. It has three expressions for initialization, condition, and increment/decrement. The while loop repeats as long as a condition is true. It checks the condition first before running the code block. The do-while loop is similar but runs the code block at least once before checking the condition.

Uploaded by

mariamgodwin987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Loops in C

Introduction
• Loops control statements are useful to execute a statement or a set of
statements for a particular number of times until the condition
evaluates to false.
• Loops are of two types, Pre-test loops and Post-test loops.
• In C Programming, we have three loops for, while and do while loops.
• For and while loops are pre-test loops and do-while loop is a post test
loop.
• Every loop has an expression or condition, and based on the result,
the compiler will either execute the code block or exit from the loop.
For Loop
For Loop in C Programming
• The For loop in C Programming is used to repeat a block of statements for a
given number of times until the given condition is False.
• C For loop is one of the most used loops in any programming language.
• The syntax of the for loop in C Programming:
// C for Loop Syntax
for (initialization; test condition; increment/decrement operator)
{
//Statement 1
//Statement 2
………
//Statement n
}
How for loop works
• If you observe the above syntax, The for loop in c has three expressions
separated by the semi-colons (;) and the execution of these expressions
are as follows:
• Initialization: for loop starts with the initialization statement so,
initialization of counter variables is done first (For example, counter = 1
or i = 1.).
• The initialization section is executed only once at the beginning of the
for loop.
• Test Condition: The value of the counter variable tested against the test
condition. If the condition is True, it will execute the statements inside
the for loop. If the condition fails, the for loop will terminate.
• Increment & decrement operator: This expression executed after the
end of each iteration. This operator helps to increase or decrease the
counter variable as per our requirement or needs.
Flow Chart
The execution process of the for loop
• Initialization: We initialize the counter variable(s) here. For example, i=1.
• Test condition: It will check for the condition against the counter variable. If
the condition is True, then it will execute the statements inside the for loop.
• If the condition is False, then the program will exit from the loop
• After completing the iteration, it will execute the Increment and decrement
Operator inside the for loop to increment or decrement the value.
• Again it will check for the condition after the value incremented.
• As long as the condition is True, the statements inside the for loop will
execute.
/* For Loop in C Programming Example */
#include <stdio.h>
int main()
{
int i, number, total= 0;
printf("\n Please Enter any integer\n");
scanf("%d", &number);

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


{
total = total + i;
}
printf("\nSum of n natural numbers is: %d\n", total);
return 0;
}
How it works
• From the given example, User can enter any value above 1 (let
say20 ), and total variable is initialized to 0.
• Next, we assign the user-entered value to a number variable.
• Then we are going to check whether the counter variable (i) is less
than or equal to the number.
• If the condition results true, i added to the total. Otherwise, it will exit
from the for loop.
• At the starting of the loop i =1 and n =20, so the condition will be True
until i value is incremented to 21.
• In the next section, we used ++ operator to increment the i value.
• After the incrementing process, it will repeat the process until the
condition results false (until I =21).
For Loop features
• The For loop in C has the flexibility to omit one or more sections from
the declaration.
• Although we can skip one or more sections from the for loop, we
have to put the semicolon (;) in place; otherwise, it will throw
compilation error.
• The following examples show you the features of For loop in C
programming
Initialize the counter variable can skip, as shown below:
int i=1;
for( ;i<=10;i++)
Here, the counter variable declared before the for loop.
Like initialization, we can also skip the increment part.
int i=1;
for( ;i<=20; )
{
//statements
i++;
}
The incrementing part declared in the for loop body
• For loop allows us to initialize more than one counter variable at a
time with comma separate:
for(i=1,j=20;i<j; i++)
• For loop also allows using multiple conditions.
• Instead of using comma we have to use the logical operator to
separate the two condition
for(i=1,j=20;i<=10 && j>=20; i++)
{
//statements j++;
}
• Like the test condition, for loop in C allows us to use more than one
increment operator as follows
for(i=1,j=1; i<=10&& j<=10; i++, j++)
Nested For Loop
• /* Nested For Loop in C Programming example */
#include <stdio.h>
int main()
{
int i, j;
for (i=9; i<=10; i++)
{
for (j=1; j<=10; j++)
{
printf("%d * %d = %d\n",i ,j, i*j);
}
}
return 0;
}
• In the first for loop, i initialized to value 9, and then it will check whether i is less than or
equal to 10.
• This condition is True until i reaches to 11.
• If this condition is True, then it will enter into second for loop. Otherwise, it will exit
from the for loop.
• C For Loop Iteration 1: for (i = 9; i <= 10; i++)
• The condition (9 <= 10) is True. So it will enter into the second loop.
• Nested Loop first iteration: for (j = 1; j <= 10; j++)
• The condition (1 <=10) is True. So statement inside the loop will printed
• 9*1=9
• Next, the value of j will increment to 2
• Nested Loop Second iteration: for (j = 2; 2 <= 10; 2++)
• The condition (2 <=10) is True
• 9*2=9
• It will repeat the process up to 10.
• Next j value is 11, condition (11 <= 10) fails. So it will exit from the nested or inner for
loop.
• NOTE: It will only exit from the inner loop (Second loop) but not from
the entire loop.
• C For Loop Iteration 2: for (i = 10; 10 <= 10; 10++)
• The condition (10 <= 10) is True. So it will enter into the second loop.
• Repeat the Nested For loop iteration.
• For Loop Iteration 3: for (i = 11; i <= 10; i++)
• i = 11, and the condition is False. For loop is terminated, No need to
check the second loop
WHILE LOOP
While Loop in C
• The while loop in C Programming is used to repeat a block of
statements for a given number of times until the given condition is
False.
• While loop in C starts with the condition, if the condition is True, then
statements inside the while loop will be executed.
• If the given condition is false, then it won’t be performed at least once.
The Syntax of While loop
{
While ( Condition )
{
statement 1;
statement 2;
………….
}
This is the statement Outside the While Loop but inside the main() Function
}
• First, the compiler will check for the condition inside the While loop.
• If the condition is True, the statement or group of statements under
the while loop block will execute.
• If the condition is False, the compiler will come out of the loop and
execute other statements outside the while loop.
• For a single statement inside a while loop in C Programming, curly
braces not needed.
• However, if we omit them for multiple statements, the compiler will
execute the first statement only.
• It is always good practice to use braces all the time.
Flow Chart for While loop in C Programming
How it Works
• At the beginning of the loop, While loop in C checks for the condition.
• If the condition is True, then it will execute the statements inside the
loop.
• Next, we have to use Increment & Decrement Operator inside the
while loop to increment and decrements the value.
• Again it will check for the condition after the value incremented.
• As long as the condition is True, the statements inside the while loop
will execute.
• If the condition is False, the compiler will exit from the While loop
Example
• /* While Loop in C Programming example */
#include <stdio.h >
int main()
{
int number, total=0;
printf("\n Please Enter any integer below 10 \n ");
scanf("%d", &number);
while (number <= 10)
{
total = total+number;
number++;
}
printf("\n Value of Total From the While Loop is: %d \n", total);
return 0;
}
How the program work
• In the given C while loop example, User will enter any value below 10
and total variable is initialized to 0.
• Next, the user-entered value will assigned to the number variable.
Then, the given number will test against the condition inside a while
loop.
• If the condition results true, the number added to the total.
Otherwise, it will exit from the loop
• In the next line, ++ operator is used to increment the number value.
• After incrementing, the process will repeat until the condition results
False.
Infinite While Loop in C Programming
• If you forgot to increment or decrement the value inside the while loop, then
while loop in C will execute infinite times, this is called an infinite loop
/* Infinite While Loop Example */
#include<stdio.h>
int main()
{
int x = 1;
while (x < 10)
{
printf("Value From the While Loop is: %d\n", x);
}
return 0;
}
• What is wrong from the given Example?
• X is always 1, and x is always less than 10.
• So, while loop statement will execute infinite times (infinity loop).
• Now, let us add an increment operator (x++) inside the C while loop
to the above example.
• /* While Loop Example */
#include<stdio.h>
int main()
{
int x = 1;
while (x < 10)
{
printf(" Value From the While Loop is: %d \n ", x);
x++;
}
return 0;
}
What if we an increment operator out of the loop?
#include<stdio.h>
int main()
{
int x = 1;
while (x < 10)
x++;
{
printf(" Value From the While Loop is: %d \n ", x);
}
return 0;
}
• The program will always print the final result and the results from each
iterations.
Do While Loop
• The Do While loop in C tests the condition at the loop end.
• Do While loop in C executes the code block lines at least once, even
the condition Fails.
• The While loop discussed in our previous article tests the condition
before compiler enters the code block.
• If the condition result is True, then only code inside the loop executes.
Otherwise, it won’t run at least once.
• There are some circumstances where it is required to execute some
operations (perform some statements) first and then check the
condition.
• In these situations, we can use Do While loop in C.
Do While loop Syntax
do
{
statement 1;
statement 2;
………….
statement n;
}
While (condition);
How it works
• First, it executes the lines inside the loop, and after arriving at the
end, the compiler checks the while condition.
• If the condition returns True, it recurs the process, and if it fails, the
do While Loop terminates.
Flow Chart
How it Works
• Variable initialization, and then it enters the Do While loop.
• Execute/Run a group of statements within the C Programming loop.
• Next, use Increment and Decrement Operator inside the loop to
increment or decrements the values.
• Next, it checks the while condition.
• If the condition output is True, the code inside the C Do while loop
executes again.
• The process will last until the condition fails.
• If it is False, compiler exits from the loop.
/* Do While Loop Example */

#include <stdio.h>
int main()
{
int number, total=0;

printf("\n Please Enter any integer below 10 \n");


scanf("%d", &number);
do
{
total = total + number;
printf(" Number = %d\n", number);
printf(" Total Value is: %d\n", total);
number++;
}
while (number< 10);
printf(" Total Value from outside the Loop is: %d \n", total);
return 0;
}
• In C do while loop program, User will enter any value below 10, and the
total variable initialized to 0.
• User entered value will assign to the number variable, and then the
number is added to the total.
• In the next line, we used ++ operator to increment the number value.
• After this line, the value in a Number variable tests against the while
condition.
• If the condition results true, it repeats the process.
• Otherwise, it will exit from the loop
• In the next line, we used one more printf statement to show that it is
coming from outside the while loop.
Difference between While loop and Do While
loop
• Although, Do While and While loop looks similar, they differ in their
execution.
• While loop, the condition is tested at the beginning of the loop, and if
the condition is True, then only statements in that loop will be executed.
• It means While loop executes the code block only if the condition is
True.
• For Do While loop, the condition tests at the end of the loop.
• So Do While executes the statements in the code block at least once
even the condition Fails.
While loop in C Programming Example
• In the following program, we are declaring integer and then check
whether 0 is greater than ten or not.
• If the condition is True, then it will print this statement, “X is Greater
than 10”.
• There is one more printf statement outside the While loop, and this
statement will execute after the while loop.
#include <stdio.h>

int main()
{
int x=0;

while(x > 10)


{
printf("\n x is Greater Than 10\n");
}

printf("\nStatement Outside the while loop\n");


return 0;
}
We are going to write the same example using do while in c
language
/* Do While in C Example */
#include <stdio.h>

int main()
{
int x=0;

do
{
printf("\n X is Greater Than 10 \n");
}
while(x > 10);

printf("\n Statement Outside the while loop \n");


return 0;
}
• Although the condition fails, the statement inside the do while loop
executed once because of the condition tested after the execution of
the statements.

You might also like