You are on page 1of 5

11/11/2022

Lecture-7
Corse Title: Programming Language
Course Code: EL-255
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2022
Offered to: S.E. (Electronic Engineering)

Department of Electronic Engineering


NED University of Engineering and Technology Karachi, Pakistan

Chapter-3

Loops

1
11/11/2022

Loops
Loops are used to repeat a block of code.
There are three types of loops:

1. for
2. while
3. do…..while.

For loop
for (initialization; condition; step)
{
Statement 1; #include <stdio.h> Output
Statement 2; 0
int main() 1
…………...
{ 2
Statement n; int x; 3
} for ( x = 0; x < 10; x++ ) 4
{ 5
printf( "%d\n", x ); 6
} 7
getch(); 8
} 9
{
printf(“\n %d” x);
x++;
}
4

2
11/11/2022

For loop
#include <stdio.h> The loop will run from x=0 to x=9, i.e 10 times
int main()
{
int x;
{ // first time { // second time { // third time
x=0; x=1; x=2;
for ( x = 0; x < 10; x++ )
printf(“\n %d” x); printf(“\n %d” x);
{ printf(“\n %d” x);
x++; x++;
printf( "%d\n", x ); x++; }
} }
}
getch(); Output
} 0
{ // tenth time 1
x=9; 2
printf(“\n %d” x); 3
.......... x++; 4
5
}
6
printf(“\n outside loop %d”, x); // What will be printed 7
here??? 8
9
Outside loop 10 5

Program 3.1
Write a program to print all the 256 ASCII
characters in C-Language using for loop.
#include <stdio.h>

int main()
{
Make changes in this int x;
program
for ( x = 0; x < 10; x++ )
{
printf( "%d\n", x );
}
getch();
}

3
11/11/2022

ASCII Character
chart

Program 3.2
Write a program, by using for loop, which takes
five(5) integers as input and prints their sum and
average.

4
11/11/2022

Program 3.3
Write a program to print alphabets in upper case
from A to Z.
ASCII code of A is 65

THE END

10

You might also like