1
LOOPS IN C
by
Dr. Sumit Srivastava
Dept. of Computer Science & Engineering
Unit -II CS101 PPS @Sumit
Loops in C Programming
2
▪ A loop statement allows us to execute a statement or group of
statements multiple times.
▪ It repeats some portion of the program either a specified number of
times or until a particular condition is being satisfied.
▪ Types of Iterative/Looping statements:
❖ for loop
❖ while loop
❖ do-while-loop
CS101 PPS @Sumit
Loops in C Programming
3
CS101 PPS @Sumit
For loop in C
4
A for loop is a repetition control structure that allows you
to efficiently write a loop that needs to execute a specific
number of times.
Syntax:
for (initialization; condition; updation)
{
body;
}
CS101 PPS @Sumit
For loop
5
For loop
6
expression1 is an initialization, expression2 is the conditional
expression and expression3 is an updation.
In the for loop, expression1 is used to initialize the variable,
expression2 is evaluated and if the condition is true, then the body
of for loop will be executed and then the statements under
expression3 will be executed.
This process is repeated as long as the for loop condition is true,
once the condition is false control will return to the statements
following the for loop and execute those statements.
CS101 PPS @Sumit
Example:
7
WAP to print hello 5 times Output
using for loop.
1. Hello
#include< stdio.h > 2. Hello
3. Hello
int main( )
4. Hello
{ 5. Hello
int i;
for(i=1; i<=5; i++)
{
printf("%d. Hello!!\n",i);
}
return 0;
}
CS101 PPS @Sumit
While loop in C
8
Similar to for Loop, while statement creates a loop that repeats until
the test expression becomes false.
A while loop is also known as an entry loop because in a while loop
the condition is tested first then the statements underbody of the
while loop will be executed.
If the while loop condition is false for the first time itself then the
statements under the while loop will not be executed even once.
While loop in C
9
Syntax Flow Diagram
initialization;
while(condition)
{
body;
updation;
}
CS101 PPS @Sumit
10
Syntax Flow Diagram
While loop in C: Example
11
WAP to print hello 5 times
Output
using while.
#include <stdio.h > 1. Hello
int main ( ) 2. Hello
{
int i=1; 3. Hello
while(i<=5) 4. Hello
{ 5. Hello
printf("%d. Hello!!\n",i); i++;
}
return 0;
}
CS101 PPS @Sumit
do... while loop
12
Like while loop, do-while is also an iterative statement, but it tests
the condition at the end of the loop body.
The do-while is also known as an exit loop because in the do-while
loop, the statements will be executed first and then the condition is
checked.
If the condition of the while loop is true then the body of the loop
will be executed again and again until the condition is false. Once
the condition is false, the control will transfer outside the do-while
loop and execute statements followed soon after the do-while loop.
do... while loop
13
Syntax Flow Diagram
initialization;
do
{
body;
updation;
}
while(condition);
do... while loop
14
Syntax Flow Diagram
do... while loop (Example)
15
WAP to print hello 5 times Output
using do while.
1. Hello
#include < stdio.h >
2. Hello
int main( )
{ 3. Hello
int i=1; 4. Hello
do 5. Hello
{
printf("%d. Hello!!\n",i);
i++;
}
while(i<=5);
return 0;
}
CS101 PPS @Sumit
nested loops in C
16
C programming allows to use one loop inside another loop.
loop nesting is that you can put any type of loop inside any
other type of loop. For example, a 'for' loop can be inside a
'while' loop or vice versa.
CS101 PPS @Sumit
nested for loop in C
17
The syntax for a nested for loop statement in C is as follows −
for ( init; condition; increment ){
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}
CS101 PPS @Sumit
nested while loop
18
The syntax for a nested while loop statement is as follows −
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
CS101 PPS @Sumit
nested do...while loop
19
The syntax for a nested do...while loop statement is as follows −
do {
statement(s);
do {
statement(s);
}while( condition );
}while( condition )
CS101 PPS @Sumit
20 Loop Control Statements
CS101 PPS @Sumit
Loop Control Statements
21
▪ Loop control statements change execution from its normal sequence.
When execution leaves a scope, all automatic objects that were created
in that scope are destroyed.
▪ C supports the following control statements.
1. break statement: Terminates the loop or switch statement and transfers
execution to the statement immediately following the loop or switch.
2. continue statement: Causes the loop to skip the remainder of its body
and immediately retest its condition prior to reiterating.
3. goto statement: Transfers control to the labeled statement.
CS101 PPS @Sumit
Break Statment
22
▪ Break :
The break statement in C has the following two usage:
1. in loops
2. in switch – case
➢ The break statement stops the current iteration of loop and exit
(When a break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the next
statement following the loop).
➢ Break, only terminates the current loop in which it occurs.
➢ It can be used to terminate a case in switch.
CS101 PPS @Sumit
Break Statement
23
Syntax Flow Diagram
break;
➢ If you are using nested loops, the break
statement will stop the execution of the
innermost loop and start executing the
next line of code after the block.
CS101 PPS @Sumit
Break: Example
24
Program Output
#include < stdio.h > 1. Hello
int main( )
2. Hello
{
int i; 3. Hello
for (i=1; i<10; i++)
{
if(i==4)
{
break;
}
printf("%d. Hello",i);
}
return 0;
CS101 PPS @Sumit
}
Break: Example
25
#include <stdio.h>
Output
int main () {
/* local variable definition */ value of a: 10
int a = 10;
value of a: 11
/* while loop execution */ value of a: 12
while( a < 20 ) {
value of a: 13
printf("value of a: %d\n", a); value of a: 14
a++;
value of a: 15
if( a > 15) {
/* terminate the loop using break statement */
break;
}
}
return 0; CS101 PPS @Sumit
}
Continue Statement
26
The continue statement stops the current iteration of loop and
continue the next iteration.
CS101 PPS @Sumit
Continue Statement
27
The continue statement stops the current iteration of loop and
continue the next iteration.
The continue statement in works somewhat like the break statement.
Instead of forcing termination, it forces the next iteration of the loop
to take place, skipping any code in between.
CS101 PPS @Sumit
Continue Statement
28
For the for loop, continue statement causes the conditional test
and increment portions of the loop to execute.
For the while and do...while loops, continue statement causes
the program control to pass to the conditional tests.
CS101 PPS @Sumit
Continue : Example
29
Syntax Flow Diagram
continue;
CS101 PPS @Sumit
Continue : Example
30
Program Output
#include < stdio.h > 1. Hello
int main( )
{ 2. Hello
int i; 3. Hello
for (i=1; i<10; i++)
{ 5. Hello
if (i==4 || i==7) 6. Hello
{
continue; 8. Hello
} 9. Hello
printf("%d. Hello",i);
}
return 0;
}
CS101 PPS @Sumit
Continue : Example
31
#include <stdio.h>
int main () { Output
/* local variable definition */
int a = 10; value of a: 10
/* do loop execution */ value of a: 11
do {
value of a: 12
if( a == 15) { value of a: 13
/* skip the iteration */
a = a + 1; value of a: 14
continue;
} value of a: 16
printf("value of a: %d\n", a);
value of a: 17
a++; value of a: 18
}while( a < 20 ); value of a: 19
return 0; CS101 PPS @Sumit
}
goto Statement
32
▪ A goto statement in C programming provides an unconditional jump
from the 'goto' to a labeled statement in the same function.
▪ NOTE − Use of goto statement is highly discouraged in any
programming language because it makes difficult to trace the control
flow of a program, making the program hard to understand and hard
to modify.
CS101 PPS @Sumit
goto Statement
33
Syntax Flow Diagram
goto label;
..
.
label: statement;
Here label can be any plain text except C
keyword, and it can be set anywhere in the C
program above or below to goto statement.
CS101 PPS @Sumit
goto Statement
34
Syntax Flow Diagram
goto label;
..
.
label: statement;
Here label can be any plain text except C
keyword, and it can be set anywhere in the C
program above or below to goto statement.
CS101 PPS @Sumit
goto statement: Example
35
#include <stdio.h>
int main () {
Output:
value of a: 10
/* local variable definition */
int a = 10; value of a: 11
/* do loop execution */
value of a: 12
LOOP:do { value of a: 13
if( a == 15) { value of a: 14
/* skip the iteration */
a = a + 1; value of a: 16
goto LOOP; value of a: 17
}
value of a: 18
printf("value of a: %d\n", a);
a++; value of a: 19
}while( a < 20 );
return 0; CS101 PPS @Sumit
}
36 Input / Output in C
CS101 PPS @Sumit
Input / Output in C
37
printf: This function is used to display output to the output screen.
The general syntax of this function is as follows.
printf("format string",var_list);
TYPE FORMATTERS:
DATA_TYPE FORMATTER
-----------------------------------------------------
int %d %X %x %o %u %p
float %f
double %lf %Lf
char %c
string %s
CS101 PPS @Sumit
Input / Output in C
38
TYPE FORMATTERS Escape Sequence
DATA_TYPE FORMATTER SEQ DETAIL
--------------------------------------- ------------------------
int %d %X %x %o %u %p \n New Line
float %f \t Tab
double %lf %Lf \b Back Space
char %c \f Form Feed
string %s \r Carriage Return
\a Alarm
\\ Back Slash
\" Double Quote
CS101 PPS @Sumit
Input / Output in C
39
scanf: This function allows us to enter data from keyboard that
will be formatted in a certain way.
The general form of scanf ( ) statement is as follows:
scanf ("format string",var_address);
CS101 PPS @Sumit
Input / Output in C
40
Example OUTPUT
Enter value a: 17
#include < stdio.h >
Enter value b: 23
int main( )
{
int a, b; User Input is:
printf ("Enter value a: "); A=17 & B=23
scanf ("%d", &a);
printf ("Enter value b: ");
scanf ("%d", &b);
printf ("User Input is:\n");
printf ("A=%d & B=%d", a, b);
return 0;
}
CS101 PPS @Sumit
Input / Output in C
41
getchar: This function allows us to enter character from
keyboard.
The general form of getchar( ) statement is as follows:
char ch = getchar( );
CS101 PPS @Sumit
Input / Output in C
42
Example OUTPUT
Enter Any Char: F
#include< stdio.h >
int main( ) Input is: F
{
char choice;
printf("Enter Any Char: ");
choice = getchar();
printf("Input is: %c",choice);
return 0;
}
CS101 PPS @Sumit
43
THANK YOU
CS101 PPS @Sumit