0% found this document useful (0 votes)
371 views32 pages

C Programming UNIT 3.1 Loops

This document discusses loops in C programming. It covers the different types of loops - for, while, do-while loops. It explains the syntax and flow of each loop type. It also discusses loop control statements like break, continue, goto and provides examples. Finally, it compares the different loop types and provides some exercises involving loops.

Uploaded by

rishika
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
371 views32 pages

C Programming UNIT 3.1 Loops

This document discusses loops in C programming. It covers the different types of loops - for, while, do-while loops. It explains the syntax and flow of each loop type. It also discusses loop control statements like break, continue, goto and provides examples. Finally, it compares the different loop types and provides some exercises involving loops.

Uploaded by

rishika
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 32

Programming in C

Language

UNIT 3
Unit 3 syllabus
Loop concepts
Arrays
Functions
C loops
You may encounter situations, when a
block of code needs to be executed
several times. In general, statements are
executed sequentially: The first statement
in a program is executed first, followed
by the second, and so on.
A loop statement allows us to execute a
statement or group of statements
multiple times.
// C program to illustrate need of loops
#include <stdio.h>
int main()
{ printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
return 0;
}
 There are mainly two types of loops in C Programming:
1. Entry Controlled loops: In Entry controlled loops the test condition is
checked before entering the main body of the loop. For Loop and
While Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops the test condition is
evaluated at the end of the loop body. The loop body will execute
at least once, irrespective of whether the condition is true or
false. do-while Loop is Exit Controlled loop.
C programming language provides the following types of loops to
handle looping requirements

Loop Type Description


For loop Executes a sequence of statements multiple
times and abbreviates the code that manages
the loop variable.
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
loop tests the condition at the end of the loop body
Nested loop You can use one or more loops inside any other
while, for, or do..while loop.
Loop Control Statements
Loop control statements change execution from its normal
sequence.

Control Statement Description


Break statement Terminates the loop or switch statement
and transfers execution to the statement
immediately following the loop or switch.

Continue statement Causes the loop to skip the remainder of


its body and immediately retest its
condition prior to reiterating.

Goto statement Transfers control to the labeled statement.


break statement in C
The break statement in C programming has the following two usages −
1. 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.
2. It can be used to terminate a case in the switch statement.
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.

Syntax
The syntax for a break statement in C is as follows −
break;
Break statement flowchart
Break statement example
#include <stdio.h>
int main ()
{
int a = 10;
while( a < 20 )
{ printf("value of a: %d\n", a);
a++;
if( a > 15)
{break; } }
return 0;
}
Continue statement in C
The continue statement in C programming 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.
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.

Syntax
The syntax for a continue statement in C is as follows −
continue;
Continue flowchart
Continue example
#include <stdio.h>
int main ()
{
int a = 10; /* local variable definition */
do { if( a == 15) /* do loop execution */
{
a = a + 1; /* skip the iteration */
continue; }
printf("value of a: %d\n", a);
a++; }
while( a < 20 );
return 0;
}
goto statement in C
A goto statement in C programming provides an unconditional jump
from the 'goto' to a labeled statement in the same function.
Syntax
The syntax for a goto statement in C is as follows −
goto label;
.. .
label: statement;
NOTE − Use of the goto statement is highly discouraged in any
programming language because it makes it difficult to trace the
control flow of a program, making the program hard to understand
and hard to modify. Any program that uses a goto can be rewritten to
avoid them.
Goto statement flowchart
for loop in C
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
The syntax of a for loop in C programming language is −
for ( init; condition; increment )
{
statement(s);
}
Here is the flow of control in a 'for' loop −
1. The init step is executed first, and only once. This step allows you to
declare and initialize any loop control variables. You are not required
to put a statement here, as long as a semicolon appears.
2. Next, the condition is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and the
flow of control jumps to the next statement just after the 'for' loop.
3. After the body of the 'for' loop executes, the flow of control jumps
back up to the increment statement. This statement allows you to
update any loop control variables. This statement can be left blank,
as long as a semicolon appears after the condition.
4. The condition is now evaluated again. If it is true, the loop executes
and the process repeats itself (body of loop, then increment step,
and then again condition). After the condition becomes false, the
'for' loop terminates.
For loop
flowchart
For loop example
C program for printing a multiplication table for a
given number
While loop in c
A while loop in C programming repeatedly executes a target
statement as long as a given condition is true.
Syntax
The syntax of a while loop in C programming language is −

Here, statement(s) may be a single statement or a block of


statements. The condition may be any expression, and true is any
nonzero value. The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the
line immediately following the loop.
While loop
flowchart
While loop example
do...while loop in C
Unlike for and while loops, which test the loop condition at the top of
the loop, the do...while loop in C programming checks its condition at
the bottom of the loop.
A do...while loop is similar to a while loop, except that it is guaranteed
to execute at least once.
The do-while loop is mostly used in menu-driven programs where the
termination condition depends upon the end user
Syntax
The syntax of a do...while loop in C programming language is −
do {
statement(s);
} while( condition );
do…while loop flowchart
 The conditional expression appears
at the end of the loop, so the
statement(s) in the loop executes
once before the condition is
tested.
 If the condition is true, the flow of
control jumps back up to do, and
the statement(s) in the loop
executes again. This process
repeats until the given condition
becomes false.
do….while example
#include <stdio.h>
int main ()
int a = 10;
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0; }
Difference between for loop and while loop
For loop While loop

Initialization may be either in loop Initialization is always outside the loop.


statement or outside the loop.
Once the statement(s) is executed then Increment can be done before or after the
after increment is done. execution of the statement(s).
It is normally used when the number of It is normally used when the number of
iterations is known. iterations is unknown.
Condition is a relational expression. Condition may be expression or non-zero
value.
For is entry controlled loop. While is also entry controlled loop.
for ( init ; condition ; iteration ) { while ( condition ) { statement(s); }
statement(s); }
used to obtain the result only when used to satisfy the condition when the
number of iterations is known. number of iterations is unknown
Difference between while and do-while loop
While loop do-while loop
Condition is checked first then statement(s) Statement(s) is executed atleast once,
is executed. thereafter condition is checked.
It might occur statement(s) is executed zero
At least once the statement(s) is executed.
times, If condition is false.
No semicolon at the end of while. Semicolon at the end of while.
while(condition) while(condition);
If there is a single statement, brackets are
Brackets are always required.
not required.
Variable in condition is initialized before the variable may be initialized before or within
execution of loop. the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) do { statement(s); }
{ statement(s); } while(condition);
Loop exercises
1. Write a program in C to display the first 10 natural numbers.
Expected Output :
1 2 3 4 5 6 7 8 9 10
2. Write a C program to find the sum of first 10 natural numbers.
Expected Output :
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
3. Write a program in C to display n terms of natural number and their sum.
Test Data : 7
Expected Output :
The first 7 natural number is :
1234567
The Sum of Natural Number upto 7 terms : 28
4. Write a program in C to display the n terms of odd natural number and
their sum .
Test Data
Input number of terms : 10
Expected Output :
The odd numbers are :1 3 5 7 9 11 13 15 17 19
The Sum of odd Natural Number upto 10 terms : 100
5. Write a C program to calculate the factorial of a given number.
Test Data :
Input the number : 5
Expected Output :
The Factorial of 5 is: 120

You might also like