You are on page 1of 39

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, 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 stars (100 Times)
#include<stdio.h>

int main()
{
int i;

for (i = 1; i <= 1000; 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 2 Span 20

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");

/* 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 Loop
#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 Loop
#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
while Loop

Dr. Yousaf, PIEAS


Printing the counting from 1 to 10
#include<stdio.h>
#include<stdio.h>
int main()
{ int main()
int i = 1; {
while (i <= 10) int i;
{
printf("i = %d\n", i); for (i = 1; i <= 10; i++)
i++; printf("*\n");
}
getchar(); getchar();
return 0; return 0;
} }
Dr. Yousaf, PIEAS
while Loop
For loops can usually be rewritten as while
loops:
initialization;
while ( loopContinuationTest )
{
statement;
increment;
}

Dr. Yousaf, PIEAS


while Loop
• Generic Form
while (condition)
statement
• Executes as expected:
1. condition is evaluated
2. If condition is false (i.e. 0), loop is exited (go to step 5)
3. If condition is true (i.e. nonzero), statement is executed
4. Go to step 1
5. Next statement
• Note:
for (exp1; exp2; exp3)
stmt;
is equivalent to
exp1;
while(exp2)
{ stmt; exp3; }

Dr. Yousaf, PIEAS


// While loop is a top-test loop
#include<stdio.h>

int main()
{
int x = 7;
while (x < 9)
{
printf(" %d is less than 9\n", x);
x++;
}
getchar();
return 0;
}

Dr. Yousaf, PIEAS


while Loop
#include <stdio.h>

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

Dr. Yousaf, PIEAS


Counting from 1 to 10
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i = 1; int i = 1;
do while (i <= 10)
{ {
printf("i = %d\n", i); printf("i = %d\n", i);
i++; i++;
} while (i <= 10) ; }
getchar(); getchar();
return 0; return 0;
} }
Dr. Yousaf, PIEAS
// Do While loop demonstration

#include<stdio.h>
int main()
{
int x = 11;
do
{
printf(" %d is less than 9\n", x);
x++;
} while (x < 9);

getchar();
return 0;
}

// It is a bottom-test loop instead of a top-test loop


Dr. Yousaf, PIEAS
The do while Loop
• The do/while repetition structure
– Similar to the while structure
– Condition for repetition tested after the body of
the loop is performed
• All actions are performed at least once
– Generic Format:
do
{
statement;
} while ( condition );
Dr. Yousaf, PIEAS
The do while Loop
• Standard repeat until loop
• Like a while loop, but with condition test at bottom.
• Always executes at least once.
• The semantics of do...while:
1. Execute statement
2. Evaluate condition
3. If condition is true go to step 1
4. Next statement

Dr. Yousaf, PIEAS


Flowchart of the do/while Loop

action(s)

true
condition

false

Dr. Yousaf, PIEAS


The do while Loop
#include <stdio.h>
int main()
{
int x;
x = 0;
do
{
/* The value of x will be printed at least one time
even though the condition is false*/
printf( "%d\n", x );
x++;
} while ( x != 10 );
getcahr(); return 0;
}

Dr. Yousaf, PIEAS


Comparison of
for, while, and do-while
loops

Dr. Yousaf, PIEAS


Loops
for (i = 0; i < 12; i++)
{
dowork();
}
……………………………………………………………………………………………..
i = 0;
while ( i < 12)
{
dowork();
i++;
}
……………………………………………………………………………………………..
i = 0;
do
{
dowork();
i++;
} while (i < 12); // use of semicolon
Dr. Yousaf, PIEAS
for loop
Preferably used where exact number of
iterations are known in prior.

Dr. Yousaf, PIEAS


The while Repetition Structure
Example:
int product = 2;
while ( product <= 1000 )
product = 2 * product;

true
product <= 1000 product = 2 * product

false

Dr. Yousaf, PIEAS


The while Repetition Structure

• 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

Dr. Yousaf, PIEAS


do while loop
Preferably used where loop should must run
atleast once.

Dr. Yousaf, PIEAS


Infinite Loops

Dr. Yousaf, PIEAS


Infinite Loop
// Infinite Loops, Page 226
#include<stdio.h>
int main()
{
int i = 7;
while (1) /* any non zero number can be provided
here. It is an infinite loop */
{
printf("i = %d\n", i);
i++;
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Infinite Loop
// Infinite Loops, Page 226
#include<stdio.h>
int main()
{
int i = 7;
do
{
printf("i = %d\n", i);
i++;
} while (1); /* any non zero number can be
provided here. It is an infinite loop */
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Infinite Loop
// Infinite Loops, Page 226
#include<stdio.h>
int main()
{
int i = 7;
for (; ;)
{
printf("i = %d\n", i);
i++;
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS

You might also like