Professional Documents
Culture Documents
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 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
I/P OA OA OA OA OA O/P
DCF1 DCF2 DCF20
80 km 80 km 80 km
The for loop
#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 Loop
//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 Loop
#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
while Loop
int main()
{
int x = 7;
while (x < 9)
{
printf(" %d is less than 9\n", x);
x++;
}
getchar();
return 0;
}
int main()
{
int x = 0; /* Don't forget to declare variables */
while ( x < 10 )
{ /* While x is less than 10 */
printf( "%d\n", x );
x++; /* Update x so the condition can be met
eventually */
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
do while Loop
#include<stdio.h>
int main()
{
int x = 11;
do
{
printf(" %d is less than 9\n", x);
x++;
} while (x < 9);
getchar();
return 0;
}
action(s)
true
condition
false
true
product <= 1000 product = 2 * product
false
• Repetition structure
– Programmer specifies an action to be repeated
while some condition remains true
– Psuedocode:
While there are more items on my shopping list
Purchase next item and cross it off my list
– while loop repeated until condition becomes
false