0% found this document useful (0 votes)
4 views3 pages

Lecture 8

This document discusses two types of loops in C programming: the do...while loop and the for loop. The do...while loop guarantees at least one execution before checking the condition, while the for loop is designed for a specific number of iterations with initialization, condition testing, and incrementing steps. Examples of both loops are provided to illustrate their syntax and functionality.

Uploaded by

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

Lecture 8

This document discusses two types of loops in C programming: the do...while loop and the for loop. The do...while loop guarantees at least one execution before checking the condition, while the for loop is designed for a specific number of iterations with initialization, condition testing, and incrementing steps. Examples of both loops are provided to illustrate their syntax and functionality.

Uploaded by

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

LECTURE 8 – BRANCHING & LOOPING (Con’t)

8.1. DO ….. WHILE Loop

Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming language checks its condition at the bottom of
the loop.

A do...while loop is similar to a while loop, except that a do...while loop is


guaranteed to execute at least one time.

The syntax of a do...while loop in C programming language is:


do
{
Codes or commands
}
while (condition);

Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute 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
execute again. This process repeats until the given condition becomes false.

Flowchart:

1
Example;
#include <stdio.h>
int main (void)
{ Output:
int a,b;
printf ("Enter first value");
scanf ("%d", &a);
printf ("Enter second value");
scanf ("%d", &b);
do
{
printf ("%d\n",a) ;
a=a+1;
}
while (a<b);
return 0;
}

8.2. FOR Loop


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.

The syntax of a for loop in C programming language is:


for (initialization; test condition; run every time command)
{
command;
}

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 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 backup 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.

2
Flowchart

Example;
#include <stdio.h> Output:
int main (void)
{
int a=1;
for (a=1; a<10; a++)
{
printf ("%d\n", a);
}
return 0 ;
}

You might also like