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

Lec 8 Programming Fundamentals (3)

Uploaded by

rabeyaasif1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lec 8 Programming Fundamentals (3)

Uploaded by

rabeyaasif1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CSC113-Programming Fundaments

Lecture 08
Week 05
Ms. Noor-ul-Huda
Senior Lecturer-I
Department of Computer Science
College of Computer Science and Information Systems
[email protected]
Lecture outcomes:
Iteration:
◦ While loop
◦ For loop
◦ Do-while loop
◦ Selection of proper loop type
Loops in C
• C has three loop statements: the while, the for, and
the do…while.
• The first two are pretest loops, and the the third is a
post-test loop.
• We can use all of them for event-controlled and
counter-controlled loops.

The while Loop


The for Loop
The do…while Loop
C Loop Constructs
The while Statement
Compound while Statement
PROGRAM 1 Process-control System Example
PROGRAM 2 A while Loop to Print Numbers
“FOR” Loop
The for loop in C language is used to iterate the statements or a
part of the program several times.
It is frequently used to traverse the data structures like the array
and linked list.
Syntax of for loop in C
The syntax of for loop in c language is given below:
for(Expression 1; Expression 2; Expression 3){
//code to be executed
}

© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.


EXAMPLE
Let's see the simple program of for loop that prints table of 1.
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d \n",i);
}
return 0;
}

© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.


Note
A for loop is used when a loop is to be executed a known
number of times. We can do the same thing with a while
loop, but the for loop is easier to read and
more natural for counting loops.
Comparing for and while Loops
PROGRAM 6-5 A Simple Nested for Loop
PROGRAM 6-5 A Simple Nested for Loop
do…while Statement
PROGRAM 6-6 Two Simple Loops
PROGRAM 6-6 Two Simple Loops
FIGURE 6-16 Pre- and Post-test Loops

You might also like