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

Lesson 8 - Loops

The document discusses different types of loops in C programming including for, while, and do-while loops. It provides the syntax and flow for each loop and examples to illustrate their usage. Break and continue statements are also covered as a way to control loop execution.

Uploaded by

J A Y T R O N
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)
12 views

Lesson 8 - Loops

The document discusses different types of loops in C programming including for, while, and do-while loops. It provides the syntax and flow for each loop and examples to illustrate their usage. Break and continue statements are also covered as a way to control loop execution.

Uploaded by

J A Y T R O N
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/ 42

LOOPS

LESSON 10
LOOPS
Loops are used in programming to
repeat a specific block until some
end condition is met. There are
three loops in C programming:
for loop
while loop
do...while loop
LOOPS
Let's consider a situation when
you want to print Welcome to
AKIM STATE
UNIVERSITY! five times.
 Here is a simple C program to
do the same:
LOOPS
It was simple, but again, let's
consider another situation when
you want to write Welcome to
AKIM STATE UNIVERSITY!
a thousand times.
We can certainly not write
printf() statements a thousand
times.
LOOPS
Almost all the programming
languages provide a concept
called loop,
which helps in executing one
or more statements up to a
desired number of times.
LOOPS
All high-level programming
languages provide various forms of
loops, which can be used to execute
one or more statements repeatedly.
Let's write the C program with the
help of a while loop and later, we
will discuss how this loop works:
LOOPS
The program makes use of a while loop,
which is being used to execute a set of
programming statements enclosed within
{....}.
Here, the computer first checks whether the
given condition, i.e., variable “i" is less than
5 or not.
 if it finds the condition is true, then the
loop body is entered to execute the given
statements.
LOOPS
Here, we have the following two
statements in the loop body:
 First statement is printf()
function, which prints Hello
World!
Second statement is i = i + 1,
which is used to increase the
LOOPS
After executing all the statements given
in the loop body,
the computer goes back to while( i < 5)
and the given condition, (i < 5), is checked
again.
And the loop is executed again if the
condition holds true. This process repeats
till the given condition remains true which
means variable “i" has a value less than 5.
LOOPS
A loop statement allows us to
execute a statement or group of
statements multiple times.
Lets check the general form of a
loop statement in most of the
programming languages:
General form of a loop statement
LOOPS
 So let's discuss the two most
important loops available in C
programming language.
Once you are clear about these
two loops, then you can check
other loops available in C and the
way they work.
The while Loop
The while Loop
A while loop available in C
Programming language has the
following syntax:
while ( condition )
{
/*....while loop body ....*/
}
The While Loop
The While Loop syntax can be represented with the
flowchart :
The While Loop
The following important
points are to be noted about a
while loop:
 A while loop starts with a
keyword while followed by a
condition enclosed in ( ).
The While Loop
Further to the while() statement,
you will have the body of the loop
enclosed in curly braces {...}.
A while loop body can have one
or more lines of source code to be
executed repeatedly.
The While Loop
 If the body of a while loop has just one
line, then its optional to use curly braces
{...}.
A while loop keeps executing its body till
a given condition holds true.
As soon as the condition becomes false,
the while loop comes out and continues
executing from the immediate next
statement after the while loop body.
The While Loop
A condition is usually a
relational statement, which is
evaluated to either true or false.
A value equal to zero is treated
as false and any non-zero value
works like true.
The do...while Loop
The do...while Loop
A while loop checks a given condition before it
executes any statements given in the body part.
C programming provides another form of loop, called
do...while,
It executes a loop body before checking a given
condition.
The do…..While Loop
It has the following syntax:
do
{
/*....do...while loop body ....*/
} while ( condition );
The do…..While Loop
The above code can be
represented in the form of a
flow diagram as shown
below:
Lets write our earlier
example using
do...while loop
#include <stdio.h>
main()
{
int i = 0;
do
{
printf( “Welcome to AKIM STATE UNIVERSITY!\n");
i = i + 1;
}while ( i < 5 );
}
The break statement
The break statement
When the break statement is
encountered inside a loop, the loop is
immediately terminated and
the program control resumes at the next
statement following the loop.
The syntax for a break statement in C
is as follows:
break;
The break statement
Following is a variant of the
above program, but it will
come out after printing
WELCOME TO AKIM
STATE UNIVERSITY! only
three times:
 #include <stdio.h>
 main()
 {
 int i = 0;
 do
 {
 printf( "Hello, World!\n");
 i = i + 1;
 if( i == 3 )
 {
 break;
 }
 }while ( i < 5 );
 }
The continue statement
The continue statement
The continue statement in C programming
language works somewhat like the break
statement.
Instead of forcing termination, continue
forces the next iteration of the loop to take
place, skipping any code in between.
The syntax for a continue statement in C
is as follows:
continue;
The Continue Statement
Following is a variant of
the above program, but it
will skip printing when
the variable has a value
equal to 3
#include <stdio.h>
main()
{
 int i = 0;
 do
 {
 if( i == 3 )
 {
 i = i + 1;
 continue;
 }
 printf( "Hello, World!\n");
 i = i + 1;
 }while ( i < 5 );
}
For Loop
For Loop
 The syntax of a for loop is:
for (initializationStatement; testExpression;
updateStatement)
{
// codes
}
How for loop works?

The initialization statement is executed


only once.
Then, the test expression is evaluated.
If the test expression is false (0), for loop is
terminated.
But if the test expression is true (nonzero),
codes inside the body of for loop is
executed and the update expression is
updated.
This process repeats until
the test expression is false.
The for loop is commonly
used when the number of
iterations is known.
END OF LESSON ON LOOPS
ASSIGNMENT

Briefly Discuss how the following pillars of OBJECT
OREINTED PROGRAMMING CONCEPTS are
implemented in programming.
Polymorphism
Abstraction
Encapsulation
Inheritance
NB: Submission should be done in hard copy

You might also like