For Loop Revisited: Muhammad Hamza Malik CSC-20S-124 Discrete Math Structure Computer Science - Sec - D Sir Waqas
For Loop Revisited: Muhammad Hamza Malik CSC-20S-124 Discrete Math Structure Computer Science - Sec - D Sir Waqas
31 – 12 - 2021
Definition
In computer science a for loop is
a programming language statement which
allows code to be repeatedly executed. A for
loop is classified as an iteration statement.
Unlike many other kinds of loops, such as
the while loop, the for loop is often
distinguished by an explicit loop counter or loop
variable. This allows the body of the for loop
(the code that is being repeatedly executed) to
know about the sequencing of each iteration.
For loops are also typically used when the
number of iterations is known before entering
the loop.
31 – 12 - 2021
Three-expression for loops
This type of for loop is found in nearly all
languages which share a common heritage with
the C programming language. It is characterized
by a three-parameter loop control expression;
consisting of an initializer, a loop-test, and a
counting expression. A representative example
in C is:
for (i = 0; i < 10; i++)
{
/* loop body */
}
31 – 12 - 2021
Three-expression for loops
The three control expressions, separated by
semicolons here, are from left to right the
initializer expression, the loop test expression,
and the counting expression. The initializer is
evaluated exactly once right at the start of the
loop. The loop test expression is evaluated at
the beginning of each iteration through the
loop, and determines when the loop should
exit. Finally, the counting expression is
evaluated at the end of each loop iteration -
even ifcontinue is called - and is usually
responsible for altering the loop variable.
31 – 12 - 2021
Flow Chart
of For Loop
31 – 12 - 2021
Syntax
31 – 12 - 2021
For Loop Example 1
int i,j;
for(i=1; i<=10; i++)
{
printf(“\nA”);
}
getch()
}
31 – 12 - 2021
For Loop Example 2
31 – 12 - 2021
Nested For Loops
31 – 12 - 2021
For Loop Examples 3
31 – 12 - 2021
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a,b,c;
Printf("Enter the Value of Table=“);
Scanf(“%d”,&a);
for (b=1; b<=10; b++)
{
c=a*b;
Printf(“\n%d * %d = %d”,a,b,c);
}
getch();
}
31 – 12 - 2021
For Loop Example 4
31 – 12 - 2021
void main()
{
clrscr();
int a,c,d;
c=0;
d=0;
for (a=10; a<=20; a++)
{
if(a%2==0)
{
c=c+a;
}
else
{
d=d+a;
}
}
printf("Sum of Even Numbers from 10-20= %d “, c);
printf("\nSum of Odd Numbers from 10-20= %d“ , d);
getch();
}
31 – 12 - 2021
For Loop Example 5
Make increasing stars arrangements as fallow?
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
31 – 12 - 2021
int i,j;
clrscr();
for(i=1; i<=15; i++)
{
for(j=1; j<=i; j++)
{
printf(" *");
}
printf("\n");
}
getch();
}
31 – 12 - 2021
For Loop Example 6
Make increasing stars arrangements as fallow?
* * * *******
* * * ******
* * * *****
* * * ****
* * * ***
* * * **
* * * *
* * *
* *
*
31 – 12 - 2021
int i,j;
clrscr();
for(i=1; i<=10; i++)
{
for(j=10; j>=i; j--)
{
printf(" *");
}
printf("\n");
}
getch();
}
31 – 12 - 2021
Assignment Questions 1
31 – 12 - 2021
Assignment Questions 2
31 – 12 - 2021