What we have learned ?
if
if-else
1
Train your mind……….!!!
2
for Loop
For loop
for ( initialization condition ; termination condition ; increment condition )
{
statement ( s ) ;
}
Example
int counter ;
for( counter = 0 ; counter < 10 ; counter = counter + 1 )
cout << counter;
Output
0123456789
Table for 2
2x1=2
2x2=4
2x3=6
:
:
2 x 10 = 20
Example - Calculate Table for 2
#include <iostream.h>
main ( )
{
int counter ;
for ( counter = 1 ; counter <= 10 ; counter = counter + 1 )
{
cout << "2 x " << counter << " = " << 2* counter << "\n“ ;
}
}
Output
2 x1 = 2
2x2=4
2x3=6
:
:
2 x 10 = 20
Flow chart for the ‘Table’ example
Start
counter=1
While
No Exit
counter <=10?
yes
Print 2*counter
Counter =
counter + 1
Stop
#include <iostream>
using namespace std;
int main() {
// The counter variable can be declared in the init-expression.
for (int i = 0; i < 2; i++ ){
cout << i;
}
// Output: 01
// The counter variable can be declared outside the for loop.
int i;
for (i = 0; i < 2; i++){
cout << i;
}
// Output: 01
// These for loops are the equivalent of a while loop.
i = 0;
while (i < 2){
cout << i++;
}
}
// Output: 012 10
Example: Calculate Table- Enhanced
#include <iostream.h>
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“ ;
}
}
#include <iostream>
using namespace std;
int main(){
for (int i = 10; i > 0; i--) {
cout << i << ' ';
}
// Output: 10 9 8 7 6 5 4 3 2 1
for (int i = 10; i < 20; i = i+2) {
cout << i << ' ';
}
// Output: 10 12 14 16 18
12
• Always think re-use
• Don’t use explicit constants
Pre-increment vs Post-increment
• Pre-increment and pre-decrement operators increments or
decrements the value of the object and returns a reference to the
result.
• Post-increment and post-decrement creates a copy of the object,
increments or decrements the value of the object and returns the
copy from before the increment or decrement.
14
Example
• #include <iostream>
•
• int main()
•{
• int n1 = 1;
• int n2 = ++n1;
• int n3 = ++ ++n1;
• int n4 = n1++;
• // int n5 = n1++ ++; // error
• // int n6 = n1 + ++n1; // undefined behavior
• std::cout << "n1 = " << n1 << '\n'
• << "n2 = " << n2 << '\n'
• << "n3 = " << n3 << '\n'
• << "n4 = " << n4 << '\n';
•}
15
Increment operator
++
• counter ++ ;
same as
• counter = counter + 1;
Decrement operator
--
• counter -- ;
same as
• counter = counter - 1
+=
• counter += 3 ;
same as
• counter = counter + 3 ;
-=
• counter -= 5 ;
same as
• counter = counter – 5 ;
*=
x*=2
x=x*2
/=
x /= 2
x=x/2
Compound Assignment Operators
operator=
%=
• x %= 2 ;
same as
• x=x%2;
Comments
• Write comment at the top program to show
what it does
• Write comments that mean some thing
continue ;
continue
while trynum <= 5 ;
{
….
….
continue ;
}
continue in ‘for’ loop
for ( counter = 0 ;counter <= 10 ; counter ++ )
{
…….
continue ;
}
Nested For Loop
• A loop inside another loop is called a nested loop.
• The number of loops depend on the complexity of a problem.
• Suppose, a loop, outer loop, running n number of times consists of
another loop inside it, inner loop, running m number of times.
• Then, for each execution of the outer loop from 1...n, the inner loop
runs maximum of m times.
28
Syntax
• The syntax for a nested for loop statement in C++ is as follows −
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
}
statement(s); // you can put more statements.
}
29
Prime Number Logic
• A positive integer which is only divisible by 1 and itself is known as
prime number.
• For example: 13 is a prime number because it is only divisible by 1
and 13 but, 15 is not prime number because it is divisible by 1, 3, 5
and 15.
30
Example
The following program uses a nested for loop to find the prime numbers from 2 to 100 −
• #include <iostream>
• using namespace std;
•
• int main () {
• int i, j;
•
• for(i = 2; i<100; i++) {
• for(j = 2; j <= (i/j); j++)
• if(!(i%j)) break; // if factor found, not prime
• if(j > (i/j)) cout << i << " is prime\n";
• }
•
• return 0;
•}
31
C++ Program To display the half pyramid
of * using nested-for loop
• This program is like the number triangle.
• In number triangle we were displaying numbers in this program we
are displaying character “*”.
• Here we are asking the user to enter the number of rows for
displaying the star pattern.
• You can replace character “*” with the character you like for eg : $ , %
, etc.
32
Sample Output
• Enter number of rows: 4
*
**
***
****
*/
33
Code
• #include<iostream>
• using namespace std;
• int main(){
• ///clrscr();
• int rows,i,j;
• cout<<"Enter number of rows:";
• cin>>rows;
• for(i=1;i<=rows;i++){
• for(j=1;j<=i;j++){
• cout<<"* ";
•}
• cout<<endl;
•}
• return 0;
•}
34
C++ program to find the sum of 2
matrices.
• Nested for loop is used to calculate the sum of two 2-dimensional
matrices.
• The program consists of three for nested loops where the outer loop
runs equal to size of row and inner loop runs equal to size of column.
• The first and second are used for entering the values of elements for
Matrix A and B, while the third is used for displaying the sum of the
elements of the two matrices.
• Matrix A and B are stored in 2-dimensional arrays a and b respectively.
• In the final nested loop, each element of a and b is traversed and the
sum is printed.
35
Output
• Enter size of row:2
• Enter size of column:3
• Enter elements of matrix A
• 270
• 3 -1 7
• Enter elements of matrix B
• 492
• 0 1 -8
• Sum of A and B
• 6 16 2
• 3 0 -1
36
Code
• #include <iostream> • for(i=0;i<row;i++)
• #include <conio.h> • cout <<"Enter elements of matrix B" << endl;
• using namespace std; • for(i=0;i<row;i++)
• {
• int main() • for(j=0;j<column;j++)
• { • {
• int a[10][10], b[10][10], s[10][10]; • cin >> b[i][j];
• int i,j,row, column; • }
• cout <<"Enter size of row:"; • }
• cout << "Sum of A and B" << endl;
• cin >> row;
• for(i=0;i<row;i++)
• cout <<"Enter size of column:";
• {
• cin >> column;
• for(j=0;j<column;j++)
• cout <<"Enter elements of matrix A" << endl;
• {
• {
• cout << a[i][j] + b[i][j] << " ";
• for(j=0;j<column;j++)
• }
• {
• cout << endl;
• cin >> a[i][j];
• }
• }
• getch();
• }
• return 0; 37
• }