You are on page 1of 30

1 Dr.

Poongundran Selvaprabhu, VIT 02/19/2024


C Programming

Dr. Poongundran Selvaprabhu,


Assistant Professor (Senior)
School of Electronics Engineering (SENSE),

2 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


3 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
Types of C Loops
There are three types of loops in C
language that is given below:
Do while
While
For
Nested Loops

4 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


There are mainly two types of
loops:
Entry Controlled loops: In this type of loops the test
condition is tested before entering the loop body. For
Loop and While Loop are entry controlled loops.
Exit Controlled Loops: In this type of loops the test
condition is tested or evaluated at the end of loop body.
Therefore, the loop body will execute atleast once,
irrespective of whether the test condition is true or
false. do – while loop is exit controlled loop

5 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


6 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
Sr.No Loop Type & Description

While Loop Repeats a statement or group of statements while a


given condition is true. It tests the condition before
executing the loop body.
Do- while It is more like a while statement, except that it tests
Loop the condition at the end of the loop body.

For Loop Executes a sequence of statements multiple times


and abbreviates the code that manages the loop
variable.
Nested Loops You can use one or more loops inside any other
while, for, or do..while loop.
7 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
Loop Control Statements
Sr.No Control Statement & Description

Break Terminates the loop or switch statement and


Statement transfers execution to the statement immediately
following the loop or switch

Continue Causes the loop to skip the remainder of its body


Statement and immediately retest its condition prior to
reiterating.

Goto Transfers control to the labeled statement.


Statement

8 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


9 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
For loop in C
The for loop is used in the case where we need to execute
some part of the code until the given condition is satisfied.
The for loop is also called as a per-tested loop. It is better to
use for loop if the number of iteration is known in advance.
The syntax of for loop in c language is given below:
for(initialization; condition; incr/decr)
{
// body of the loop
// statements we want to execute
}

10 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


11 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
 Initialization Expression: In
this expression initialize the
loop counter to some value.
for example: int i=1;
 Test Expression: In this
expression test the condition.
If the condition evaluates to
true then we will execute the
body of loop and go to update
expression otherwise we will
exit from the for loop. For
example: i <= 10;
 Update Expression: After
executing loop body this
expression increments
/decrements the loop variable
by some value. for example: i+
12 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
+;
Example program of For-loop
Output
#include<stdio.h>
1
int main() 2
{ 3
int number; 4
5
for(number=1;number<=10;number++)
6
//for loop to print 1-10 numbers 7
{ 8
printf("%d\n",number); 9
10
} //printf("%d\n",number);
13
return 0; }
Dr. Poongundran Selvaprabhu, VIT 02/19/2024
In for loop, in the
initialization part, we have
assigned value 1 to the
variable number. In the
condition part, we have
specified our condition and
then the increment part.
We have the value one
stored in number, after the
first iteration the value will
be incremented, and it will
become 2.
 This loop will keep on
executing until the value of
the variable becomes 10.
After that, the loop will be
Dr. Poongundran Selvaprabhu, VIT
14 terminated. 02/19/2024
Skipping the initial value expression,
#include <stdio.h>
int main()
{
int i=0;
int max = 10;
for (; i < max; i++)
{
printf("%d\n", i);
} return 0; }

15 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


Skipping the initial & Increment value expression,
#include <stdio.h>
int main()
{
int i=0;
int max = 10;
for (; i < max; )
{
printf("%d\n", i);
i++;
} return 0; }

16 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


Multiple Initialization in For loop
#include <stdio.h>
int main()
{
int i,j,k;
for(i=0,j=0,k=0;i<4, j<10,k<12;i++, j+=2 ,
k+=3)
{ Output
000
printf("%d %d %d\n",i,j,k); 123
} 246
369
printf("%d %d %d\n",i,j,k); 4 8 12
17 } Dr. Poongundran Selvaprabhu, VIT 5 10 15 02/19/2024
Task: Program is used to update the loop variable.
We can update more than one variable at the same time.

#include<stdio.h>
Output
void main () 02
{ 14
26
int i=0,j=2; 38
4 10
for(i = 0;i<5;i++,j=j+2)
{
printf("%d %d\n",i,j);
}
}
18 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
What is factorial?
Factorial of a number n is product of all positive
integers less than or equal to n. It is denoted as n!.
For example factorial of 5 = 1 * 2 * 3 * 4 * 5 = 120

19 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


factorial program in c
#include <stdio.h>
int main()
{ int c,n,f=1;
printf("Enter a number to calc its factorial: ");
scanf("%d",&n);
for(c=1;c<=n;c++)
{ f=f*c; }
printf("Factorial of %d = %d\n",n,f);
return 0; }
20 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
factorial program in c
#include <stdio.h>
int main()
{ long long int c,n,f=1;
printf("Enter a number to calc its factorial: ");
scanf("%llu",&n);
for(c=1;c<=n;c++)
f=f*c;
//printf("Factorial of %ld = %ld\n",n,f);
printf("Factorial of %llu = %llu\n",n,f);
return 0;
} 21 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
22 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
Syntax for Nested For loop:
for ( initialization; condition; increment ) {

for ( initialization; condition; increment ) {

// statement of inside loop


}

// statement of outer loop


}
23 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
Flow Chart

24 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


Nested For Loop

25 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


Nested For loop
#include <stdio.h>
int main() {
int i, j;
int table = 2;
int max = 5;
for (i = 1; i <= table; i++) { // outer loop
for (j = 0; j <= max; j++) { // inner loop
printf("%d x %d = %d\n", i, j, i*j);
}
printf("\n"); /* blank line between tables */
}}
26 Dr. Poongundran Selvaprabhu, VIT 02/19/2024
Output:
1 x 0 = 0 2 x 0 = 0
1 x 1 = 1 2 x 1 = 2
1 x 2 = 2 2 x 2 = 4
1 x 3 = 3 2 x 3 = 6
1 x 4 = 4 2 x 4 = 8
1 x 5 = 5 2 x 5 = 10

27 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


Infinitive for loop in C
To make a for loop infinite,
we need not give any
expression in the syntax.
Instead of that, we need to
provide two semicolons (;;)
to validate the syntax of
the for loop.
This will work as an
infinite for loop.

28 Dr. Poongundran Selvaprabhu, VIT 02/19/2024


Infinitive for loop in C
#include<stdio.h>
void main ()
{
for(;;)
{
printf("welcome to For Loop");
}
}
Creating infinite loops and exiting them with break
statements are used in many real life scenarios. Imagine a
server accepting connections for example, it is never
supposed to stop untill someone tells it to stop.
Dr. Poongundran Selvaprabhu, VIT
29 02/19/2024
Task
Write a C program to count frequency of digits in
an Integer using While and For loop?
Enter the Input:12223
output: 1- 1time
2- 3 times
3-1 time
Write a C Program to find the sum of the series
(1 +12 + 123 + 1234 + ….. –N) terms?
Enter the Input: 5
Output:
The Sum of Series: (1+12+123+1234+12345)-
30
5=13710
Dr. Poongundran Selvaprabhu, VIT 02/19/2024

You might also like