11/11/2022
Lecture-8
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
Using for loop
Form Comment
for ( i=0 ; i < 10 ; i++ )
Single Statement
Statement1;
for ( i=0 ;i <10; i++) Multiple Statements within for
{
Statement1;
Statement2;
Statement3;
}
for ( i=0 ; i < 10;i++) ; For Loop with no Body ( Carefully Look at the Semicolon )
for (i=0,j=0;i<100;i++,j++) Multiple initialization & Multiple
Statement1; Update Statements Separated by Comma
for ( ; i<10 ; i++) Initialization not used
for ( ; i<10 ; ) Initialization & Update not used
for ( ; ; ) Infinite Loop,Never Terminates
Nested for loop
for ( initialization; condition; increment )
{
for ( initialization; condition; increment )
{
// statement of inside loop
}
// statement of outer loop
}
2
11/11/2022
Write a program to print the following pattern
#include <stdio.h>
void main (void)
{
1 1 1 1 1 1 1 1 1 1 int i, j;
2 2 2 2 2 2 2 2 2 2 for(i = 1; i<10; i++)
3 3 3 3 3 3 3 3 3 3 {
.
. for(j =1; j <=10; j++)
. {
. printf("%d ", i);
. } // end of internal loop
9 9 9 9 9 9 9 9 9 9 printf(“\n”);
}// end of external loop
} // end of main
5
Write a program to print the following pattern
#include <stdio.h>
a a a a a a a a a a void main (void)
b b b b b b b b b b {
. int i, j;
. for(i = 1; i<10; i++)
. {
.
for(j =1; j <=10; j++)
{
k k k k k k k k k k printf("%d ", i);
} // end of internal loop
Make changes in this printf(“\n”);
program }// end of external loop
} // end of main
6
3
11/11/2022
Write a program to print the following pattern
#include <stdio.h>
void main (void)
{
A B C D int i, j;
E F G H int k=65;
. for(i = 1; i<=7; i++) // controlling rows
. {
. for(j =1; j <=4; j++) // printing alphabet
. {
Y Z printf("%c \t", k); What change to
k++; be made here ?
} // end of internal loop
printf(“\n”);
Question: }// end of external loop
How to stop printing at “Z” ?
} // end of main
7
THE END