CIS-104 T Computer Fundamentals: BSME 2015-19

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

CIS-104 T Computer Fundamentals

BSME 2015-19

DCIS-PIEAS
By
Nauman Shamim
Todays Session

For loop
Syntax
Examples
Nested For Loop
For loop
Syntax
for ( 1-Initialization; 2-Test-Condition; 3-Update ){
statement(s);
}

Initialization : This part of for loop is executed once only


Allows you to initialize variables for controlling loop
Test : This part determines whether to execute body of the for
loop or not
If condition is true the body of the loop is executed otherwise the
program skips to the first statement after body of the loop
Update: Allows user to update any loop variables
After body of loop is executed the program jumps to this part of the
loop
For loop behavior

This is how a for loop executes its various parts


1. Start by executes the initialization part of the loop
2. Check the test condition and execute body of the for loop
if test condition is true
3. Execute statements in update part of the loop and check
the test condition again
4. Keep repeating these steps until the test-condition
becomes false or loop is broken
When loop finishes , program executes the statements
after body of the for loop
For loop flow control
Example-1

#include<stdio.h>

#include<conio.h> Output

void main(){ Value of i = 0

Value of i = 1
int i;
Value of i = 2
for(i=0;i<5;i++)
Value of i = 3
printf(\nValue of i = %d,i); Value of i = 4

getche();

}
Loop walk trough
Lets execute this loop step by step
for(i=0;i<4;i++)
printf(\nValue of i = %d,i);

Initialize i , i = 0
Is i<4 , yes, execute body of loop and update Output:
printf(\nValue of i = %d,i); Vallue of i = 0
i=1;
Is i<4, yes, execute the body of loop and update Output:
printf(\nValue of i = %d,i); Vallue of i = 1
i=2
Is i<4, yes, execute the body of loop and update Output:
printf(\nValue of i = %d,i); Vallue of i = 2
i=3
Is < 4 , yes, execute body of the loop and update Output:
printf(\nValue of i = %d,i); Vallue of i = 3
i=4
Is i<4 No as i=4, body of loop will not be executed and loop ends
for loop optional parts

Some parts of for loop are optional


Initialization
Test-condition
Update
All of these are optional parts
Following are valid for loops
int i=0; for( ; i<5 ; i++)
int i=0 ; for( ; i<5 ; ) //i should be updated somewhere inside
loop

int i=0 ; for( ; ; ) //allowed but not very useful loop


For loop vs while loop
#include<stdio.h> #include<stdio.h>
#include<conio.h>
#include<conio.h>
void main(){
void main(){ int i=1;
int i; while(i<5){
printf(\n i=%d,i )
for(i=1; i<5; i++)
i++; //should be updated last
printf(\n i=%d,i ) }
i=1; //re-initialize

for(i=1; i<5; i++) while(i<5){


printf(\n i=%d,i )
printf(\n i square =%d,i*i
i++;
) }
getch() getch();
}
}
comparison
While Loop For Loop
Loop control variable need to be It is easy to trace how the loop
initialized outside of while statement control variable is initialized
and it is often missed Control variable is updated in last,
Loop control variable has to be and it happens automatically
updated in the last statement Loop can simply be used again
Re-using the same loop in code Total lines of code=15
requires re-setting it Multiple elements
Total lines of code = 10 Requires understanding of various
Loop is simple to write parts of loop
All test-conditions allowed All test-conditions allowed
Not easy to understand the code Relatively easy to understand
written using while loop
Choice of loop heavily depends upon the programmer, despite the facts some may
Find for loop easy while other may feel comfortable with while loop
Example-1
Program :Print all numbers between two numbers A and B

#include<stdio.h>
void main(){
int a,b,i;

b=15;
b=25;

for(i=a+1; i<b; i++)


printf( %d ,i);

}
Example-2
Program :Print all even numbers between two numbers A
and B

#include<stdio.h>
void main(){
int a,b,i;

b=15;
b=25;

for(i=a+1; i<b; i++)


if(a%2==0)
printf( %d ,i);

}
Nested Loops

Using a loop inside another loop


Can use
For loop inside a for loop
For loop inside a while or do while loop
While loop inside a for loop
While loop inside a while or do while loop
Do while inside a while or for or do while loop
Nested for loop

A for loop inside a for loop

for ( init; condition; increment )


{
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}
The loops are usually termed as inner and outer loops
The inner loop is executed every time the body of outer for
loop is executed
Nested While Loops

A while loop inside another while loop

while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
How many times X is printed
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
printf(X);
}
The outer loop has 5 iterations (The body of outer loop is
executed 5 times)
So inner loop is executed 5 times
The inner loop has 3 iterations ( The body of inner loop is
executed 3 times )
In one iteration of outer loop x is printed 3 times
So, 5x3=15 the printf(X); statement will be executed 15 times

You might also like