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

Programming Fundamentals

Lab
Lecture 07

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


For Loop

 for loop is a repetition control structure 


 executes a specific number of times

 Parts of a for loop


1. Initialization
2. Condition
3. Increment or decrement
4. Body of the loop

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Syntax

for ( initialization condition ; termination condition ; increment condition )


{
statement ( s ) ;
}

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Flow control

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Example

int counter ;
for( counter = 0 ; counter < 10 ; counter = counter + 1 )
cout << counter;

Output
0123456789

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Table for 2

2x1=2
2x2=4
2x3=6
:
:
2 x 10 = 20

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Example - Calculate Table for 2

main ( )
{
int counter ;
for ( counter = 1 ; counter <= 10 ; counter = counter + 1 )
{
cout << "2 x " << counter << " = " << 2* counter << "\n“ ;
}
}

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Output

2 x1 = 2
2x2=4
2x3=6
:
:
2 x 10 = 20

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Example: Calculate Table- Enhanced
main ( )
{
int number ;
int maxMultiplier ;
int counter ;
maxMultiplier = 10 ;
cout << " Please enter the number for which you wish to construct the table “ ;
cin >> number ;
for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 )
{
cout << number <<" x " << counter<< " = " << number * counter << "\n“ ;
}
}

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Task

 Using For loop write a program in C++ to find the sum of first 10 natural numbers.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Task

 Write a program in C++ to check whether a number is prime or not.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Task

 Write a program to print first ten odd numbers in descending order.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Task

 Write a program to print Fibonacci series using for loop

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Compound Assignment operators

e1 op= e2
 where e1 is a modifiable l-value not of const type and e2 can be :
 An arithmetic type
 A pointer, if op is + or -
 It works same as e1 = e1 op e2

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


 += counter += 3 ; same as counter = counter + 3 ;
 -= counter -= 5 ; same as counter = counter – 5 ;
 *= x*=2; same as x=x*2
 /= x /= 2; same as x=x/2
 %= x %= 2; same as x=x%2

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Nested For Loop

 Syntax

for ( init; condition; increment )


{
for ( init; condition; increment )
{
statement(s);
}
statement(s); // you can put more statements.
}

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Flow control

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Example

 Write a program to print Tables from 5 to 10 on screen using nested for loop

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


int main()
{
int i;
int j;
for(i = 5; i <= 10; i++)
{ /*outer loop*/
cout << "Table of " << i << endl;
for(j = 1; j <= 10; j++)
{ /*inner loop*/
cout << i << "*" << j << "=" << (i*j) << endl;
}
}
return 0;
}

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Task

 Write a program in C++ to display the pattern like right angle triangle with number.

1
12
123
1234
12345

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Task

 Write a program to print the following pattern using for loop.

*
**
***
****

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Task

 Write a program in C++ to make such a pattern like a pyramid with an asterisk.
 Hint: pyramid having odd number of asterisk.

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

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar

You might also like