Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

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

 The syntax for a three-expression for loop is nearly


identical in all languages that have it, after accounting
for different styles of block termination and so on.
 In C and C++ similar languages:

 for (counter = 1; counter <= 5; counter++) //statement;

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

 Find product of number. The program must


perform it at least 10 times.
 int i,j,a;
 for(i=1; i<=10; i++)
 {
 printf(“Enter value here = ”);
 scanf(%d,&a);
 a=a*a;
 printf(“The product is %d”,a);
 }
 getch()
 }

31 – 12 - 2021
Nested For Loops

 The nested for loop is called the loop inside the


loop. e.g
 int i,j,a;
 for(i=1; i<=10; i++)
 {
 printf(“ \n ”);
 for(j=1; j<=10; j++)
 printf(“A”);
 }
 getch()
 }

31 – 12 - 2021
For Loop Examples 3

Make any Table (Take


the Input from User)
using (for loop)Print

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

 Print Sum of Even and Odd Numbers from 10-20 (for


loop)

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

 Make mirrored shape of Example 4.


*
**
***
****
*****
******
*******
********
*********
**********

31 – 12 - 2021
Assignment Questions 2

 Make mirrored shape of Example 5


**********
*********
********
*******
******
*****
****
***
**
*

31 – 12 - 2021

You might also like