Lecture 8 Oct 15 2019
Lecture 8 Oct 15 2019
int main()
{
printf("*");
getchar();
return 0;
}
int main()
{
printf("*\n*");
getchar();
return 0;
}
int main()
{
printf("*\n*\n*\n*\n*\n*\n*\n*\n*\n*");
getchar();
return 0;
}
int main()
{
printf("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
getchar();
return 0;
}
int main()
{
int i;
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the stars (100 Times)
#include<stdio.h>
int main()
{
int i;
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the counting from 1 to 10
#include<stdio.h>
int main()
{
int i;
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the counting from 1 to 1000
#include<stdio.h>
int main()
{
int i;
getchar();
return 0;
}
Dr. Yousaf, PIEAS
20-Spans System
Span 1 Span 20
Span 2
I/P DCF1
OA OA DCF2 OA OA DCF20
OA O/P
80 km 80 km
80 km
The for Statement
#include<stdio.h>
• The most important looping structure in C.
• Generic Form: int main()
for (initial ; condition ; increment ) {
statement
• initial, condition, and increment are C expressions. int i;
• For loops are executed as follows:
1. initial is evaluated. Usually an assignment for (i = 1; i <=
statement. 1000; i++)
2. condition is evaluated. Usually a relational printf("i =
expression.
3. If condition is false (i.e. 0), fall out of the loop
%d\n", i);
(go to step 6.)
4. If condition is true (i.e. nonzero), execute getchar();
statement
5. Execute increment and go back to step 2.
return 0;
6. Next statement }
The for Statement
//For statement examples /* 3. for loop counting by
#include <stdio.h> 5's */
int main ()
{ printf("Output with increment
int count; of 5\n");
printf(“\n");
/* increment outside of loop */
count = 1;
for ( ; count < 7; )
{
printf("%d ", count);
count++;
}
getchar(); return 0; }
The for Statement
#include <stdio.h>
int main ()
{
int count;
int x, y;
for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to
printf( "%d\n", x );
}
getchar()
return 0;
}
Dr. Yousaf, PIEAS