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

Lecture 8 Oct 15 2019

The document discusses for loops in C programming. It provides examples of using for loops to print stars, count from 1 to 10 and 1 to 1000. It explains that for loops are more efficient than hardcoding multiple print statements when repetitive tasks need to be performed. The generic format of a for loop is explained as for(initialization; condition; increment) and examples are given to illustrate initializing and updating the loop counter inside and outside the for loop brackets.

Uploaded by

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

Lecture 8 Oct 15 2019

The document discusses for loops in C programming. It provides examples of using for loops to print stars, count from 1 to 10 and 1 to 1000. It explains that for loops are more efficient than hardcoding multiple print statements when repetitive tasks need to be performed. The generic format of a for loop is explained as for(initialization; condition; increment) and examples are given to illustrate initializing and updating the loop counter inside and outside the for loop brackets.

Uploaded by

john kevin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Examples
Write a program that prints 1 star as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1 star as
#include<stdio.h>

int main()
{
printf("*");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints 2 stars as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 2 stars as
#include<stdio.h>

int main()
{
printf("*\n*");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints 10 stars as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 10 stars as
#include<stdio.h>

int main()
{
printf("*\n*\n*\n*\n*\n*\n*\n*\n*\n*");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1000 stars in the
format shown in the previous slides.

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1000 stars in the
format shown in the previous slides.

Too much labor work?

There MUST be an easier way

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting 1 to 10 as

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting 1 to 10 as
#include<stdio.h>

int main()
{
printf("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting from 1 to
1000 in the format shown in the previous slide.

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting from 1 to
1000 in the format shown in the previous slide.

• Too much labor work?


• There MUST be an easier way
• Is there any?
• YES

Dr. Yousaf, PIEAS


Examples
Write a program that prints counting from 1 to
1000 in the format shown in the previous slide.

• Too much labor work?


• There MUST be an easier way
• Is there any?
• YES
LOOPS
Dr. Yousaf, PIEAS
Loops

Dr. Yousaf, PIEAS


For Loop

Dr. Yousaf, PIEAS


Printing the stars (10 Times)
#include<stdio.h>

int main()
{
int i;

for (i = 1; i <= 10; i++)


printf("*\n");

getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the stars (100 Times)
#include<stdio.h>

int main()
{
int i;

for (i = 1; i <= 100; i++)


printf("*\n");

getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the counting from 1 to 10
#include<stdio.h>

int main()
{
int i;

for (i = 1; i <= 10; i++)


printf("%d\n", i);

getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the counting from 1 to 1000
#include<stdio.h>

int main()
{
int i;

for (i = 1; i <= 1000; i++)


printf("i = %d\n", 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");

/* 1. simple counted for loop */ for (count=0; count<32;


printf("Output with increment in loop\n"); count += 5)
for (count =1; count <=10; count++) printf("%d\n", count);
printf ("%d\n", count);
getchar();
/* 2. counting backwards */ return 0;
printf("Output with decrement in loop\n"); }
for (count = 56; count >48; count--)
printf("%d\n", count);
The for Statement
#include <stdio.h>
int main ()
{
int count;
/* initialization outside of loop */
count = 1;
for ( ; count < 7; count++)
printf("%d ", count);

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;

/* compound statements increment */

for (x=0, y=100; x<y; x++, y--)


{
printf("%d, %d\n", x,y);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The for Repetition Structure
• Format when using for loops
for ( initialization; loopContinuationTest; increment )
statement
• Example: No
for (i = 1; i <= 10; i++) semicolon
(;) after last
printf("i = %d\n", i); expression

– Prints the integers from one to ten

Dr. Yousaf, PIEAS


The for Structure: Observations
• Arithmetic expressions
Initialization, loop-continuation, and increment can
contain arithmetic expressions.
x = 2;
y = 10;

for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to

for ( j = 2; j <= 80; j += 5 )

Dr. Yousaf, PIEAS


The for Structure: Observations
• Notes about the for structure:
– "Increment" may be negative (decrement)
– If the loop continuation condition is initially
false
• The body of the for structure is not
performed
• Control proceeds with the next statement
after the for structure
– Control variable // for (i = 1; i <= 10; i++)
• Often printed or used inside for body, but not
necessary

Dr. Yousaf, PIEAS


For Loop
#include <stdio.h>
int main()
{
int x;
/* The loop goes if x < 10, and x increases by one in every loop*/

for ( x = 0; x < 10; x++ )


{
/* Keep in mind that the loop condition checks
the conditional statement before it loops again.
Consequently, when x equals 10 the loop breaks.
x is updated before the condition is checked. */

printf( "%d\n", x );
}
getchar()
return 0;
}
Dr. Yousaf, PIEAS

You might also like