SlideShare una empresa de Scribd logo
1 de 67
Lecture 4
        Procedural Abstraction
                 and
         Predefined Functions




Computer Programming I           1
Outline
   for loop
   switch
   casting
   predefined functions




    Computer Programming I     2
for loop
 A for-loop is another loop mechanism in C++
   Designed for common tasks such as adding
     numbers in a given range
   Is sometimes more convenient to use than a
     while loop
   Does not do anything a while loop cannot do




   Computer Programming I                         3
for/while Loop Comparison
 sum = 0;
  n = 1;
  while(n <= 10) // add the numbers 1 - 10
   {
      sum = sum + n;
      n++;
    }
 sum = 0;
  for (n = 1; n <= 10; n++) //add the numbers 1 - 10
    sum = sum + n;


   Computer Programming I                         4
For Loop Dissection
 The for loop uses the same components as the
  while loop in a more compact form
    for (n = 1; n <= 10; n++)



Initialization Action       Update Action

           Boolean Expression




   Computer Programming I                        5
for loop (tracing)
  #include <iostream>
  #include <conio.h>
  using namespace std;
                                 i   output
  int main()
  {
    int i;

      for (i=1; i<=3; i++)
      { cout<<i<<“ “;
      }
      system(“pause”);
      return 0;
  }

        Computer Programming I                6
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                  i   output
   int main()
   {                              1   1
     int i;

       for (i=1; i<=3; i++)
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                7
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                  i   output
   int main()
   {                              1   1
     int i;                       2

       for (i=1; i<=3; i++)
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                8
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                                      i   output
   int main()
   {                                                  1   1
                          If true then execute body
     int i;                                           2

       for (i=1; i<=3; i++)
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                                    9
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                  i   output
   int main()
   {                              1   12
     int i;                       2

       for (i=1; i<=3; i++)
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                10
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                  i   output
   int main()
   {                              1   12
     int i;                       2
                                  3
       for (i=1; i<=3; i++)
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                11
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                                      i   output
   int main()
   {                                                  1   12
                          If true then execute body
     int i;                                           2
                                                      3
       for (i=1; i<=3; i++)
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                                    12
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                  i   output
   int main()
   {                              1   123
     int i;                       2
                                  3
       for (i=1; i<=3; i++)
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                13
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                  i   output
   int main()
   {                              1   123
     int i;                       2
                                  3
       for (i=1; i<=3; i++)       4
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                14
for loop (tracing)
   #include <iostream>
   #include <conio.h>
   using namespace std;
                                                      i   output
   int main()
   {                                                  1   123
                          If true then execute body
     int i;                                           2
                                                      3
       for (i=1; i<=3; i++)                           4
       { cout<<i<<“ “;
       }
       system(“pause”);
       return 0;
   }

         Computer Programming I                                    15
for loop
  #include <iostream>
  #include <conio.h>
  using namespace std;
                                                     i   output
  int main()
  {                                                  1   123
                         If true then execute body
    int i;                                           2
                                                     3
      for (i=1; i<=3; i++)                           4
      { cout<<i<<“ “;
      }
      system(“pause”);
                              Not True
      return 0;
  }

        Computer Programming I                                    16
for loop                                       i     j
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

       for (i=1; i<=3; i++)
       {
                                                   output
           for (j=1; j<=3; j++)
                 cout << "i" << "j" << "t";
           cout << endl;
       }
      system(“pause”);
      return 0;
  }
          Computer Programming I                            17
for loop                                       i     j
                                               1
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

       for (i=1; i<=3; i++)
       {
                                                   output
           for (j=1; j<=3; j++)
                 cout << "i" << "j" << "t";
           cout << endl;
       }
      system(“pause”);
      return 0;
  }
          Computer Programming I                            18
for loop                                       i     j
                                               1     1
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

       for (i=1; i<=3; i++)
       {
                                                   output
           for (j=1; j<=3; j++)
                 cout << "i" << "j" << "t";
           cout << endl;
       }
      system(“pause”);
      return 0;
  }
          Computer Programming I                            19
for loop                                    i          j
                                            1          1
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                     output
        for (j=1; j<=3; j++)
              cout << "i" << "j" << "t";       ij
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                                 20
for loop                                    i          j
                                            1          1
  #include <iostream>
  #include <conio.h>
                                                       2

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                     output
        for (j=1; j<=3; j++)
              cout << "i" << "j" << "t";       ij
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                                 21
for loop                                     i          j
                                             1          1
  #include <iostream>
  #include <conio.h>
                                                        2

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                      output
         for (j=1; j<=3; j++)
               cout << "i" << "j" << "t";       ij     ij
         cout << endl;
     }
   system(“pause”);
    return 0;
  }
       Computer Programming I                                  22
for loop                                       i          j
                                               1          1
  #include <iostream>
  #include <conio.h>
                                                          2
                                                          3
  using namespace std;

  int main()
  {
     int i,j;

       for (i=1; i<=3; i++)
       {
                                                        output
           for (j=1; j<=3; j++)
                 cout << "i" << "j" << "t";       ij     ij
           cout << endl;
       }
      system(“pause”);
      return 0;
  }
          Computer Programming I                                 23
for loop                                       i          j
                                               1          1
  #include <iostream>
  #include <conio.h>
                                                          2
                                                          3
  using namespace std;

  int main()
  {
     int i,j;

       for (i=1; i<=3; i++)
       {
                                                        output
           for (j=1; j<=3; j++)
                 cout << "i" << "j" << "t";       ij     ij     ij
           cout << endl;
       }
      system(“pause”);
      return 0;
  }
          Computer Programming I                                      24
for loop                                     i          j
                                             1          1
  #include <iostream>
  #include <conio.h>
                                                        2
                                                        3
  using namespace std;                       2

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                      output
         for (j=1; j<=3; j++)
               cout << "i" << "j" << "t";       ij     ij     ij
         cout << endl;
     }
   system(“pause”);
    return 0;
  }
       Computer Programming I                                       25
for loop                                       i          j
                                               1          1
  #include <iostream>
  #include <conio.h>
                                                          2
                                                          3
  using namespace std;                         2          1

  int main()
  {
     int i,j;

       for (i=1; i<=3; i++)
       {
                                                        output
           for (j=1; j<=3; j++)
                 cout << "i" << "j" << "t";       ij     ij     ij
           cout << endl;
       }
      system(“pause”);
      return 0;
  }
          Computer Programming I                                      26
for loop                                    i          j
                                            1          1
  #include <iostream>
  #include <conio.h>
                                                       2
                                                       3
  using namespace std;                      2          1

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                     output
        for (j=1; j<=3; j++)
              cout << "i" << "j" << "t";       ij     ij     ij
        cout << endl;
                                                ij
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                                      27
for loop                                    i          j
                                            1          1
  #include <iostream>
  #include <conio.h>
                                                       2
                                                       3
  using namespace std;                      2          1
                                                       2
  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                     output
        for (j=1; j<=3; j++)
              cout << "i" << "j" << "t";       ij     ij     ij
        cout << endl;
                                                ij
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                                      28
for loop                                    i          j
                                            1          1
  #include <iostream>
  #include <conio.h>
                                                       2
                                                       3
  using namespace std;                      2          1
                                                       2
  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                     output
        for (j=1; j<=3; j++)
              cout << "i" << "j" << "t";       ij     ij     ij
        cout << endl;
                                                ij     ij
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                                      29
for loop                                    i          j
                                            1          1
  #include <iostream>
  #include <conio.h>
                                                       2
                                                       3
  using namespace std;                      2          1
                                                       2
  int main()                                           3
  {
     int i,j;
                                            3          1
                                                       2
     for (i=1; i<=3; i++)                              3
     {
                                                     output
        for (j=1; j<=3; j++)
              cout << "i" << "j" << "t";       ij     ij     ij
        cout << endl;
                                                ij     ij     ij
     }
   system(“pause”);                             ij     ij     ij
   return 0;
  }
       Computer Programming I                                      30
for loop
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                        output
        for (j=1; j<=3; j++)
              cout << i << j << "t";
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                    31
for loop                                i     j
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                            output
        for (j=1; j<=3; j++)
              cout << i << j << "t";
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                        32
for loop                                i     j
                                        1
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                            output
        for (j=1; j<=3; j++)
              cout << i << j << "t";
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                        33
for loop                                i     j
                                        1     1
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                            output
        for (j=1; j<=3; j++)
              cout << i << j << "t";
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                        34
for loop                                i          j
                                        1          1
  #include <iostream>
  #include <conio.h>

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                 output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                             35
for loop                                i          j
                                        1          1
  #include <iostream>
  #include <conio.h>
                                                   2

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                                 output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                             36
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2

  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                          37
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2
                                                3
  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                          38
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2
                                                3
  using namespace std;

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12      13
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                               39
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2
                                                3
  using namespace std;                  2

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12      13
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                               40
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2
                                                3
  using namespace std;                  2       1

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12      13
        cout << endl;
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                               41
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2
                                                3
  using namespace std;                  2       1

  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12      13
        cout << endl;
                                            21
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                               42
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2
                                                3
  using namespace std;                  2       1
                                                2
  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12      13
        cout << endl;
                                            21
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                               43
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2
                                                3
  using namespace std;                  2       1
                                                2
  int main()
  {
     int i,j;

     for (i=1; i<=3; i++)
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12 13
        cout << endl;
                                            21 22
     }
   system(“pause”);
   return 0;
  }
       Computer Programming I                          44
for loop                                i       j
                                        1       1
  #include <iostream>
  #include <conio.h>
                                                2
                                                3
  using namespace std;                  2       1
                                                2
  int main()                                    3
  {
     int i,j;
                                        3       1
                                                2
     for (i=1; i<=3; i++)                       3
     {
                                              output
        for (j=1; j<=3; j++)
              cout << i << j << "t";       11 12      13
        cout << endl;
                                            21 22      23
     }
   system(“pause”);                         31 32      33
   return 0;
  }
       Computer Programming I                               45
for loop
 #include <iostream>
 #include <conio.h>

 using namespace std;

 int main()
 {
    int i,j;
    for (i=0; i<=4; i++)
    {
        for (j=0; j<=4; j++)                          output
             cout << "(" << i << "," << j << ")t";
        cout << endl;                                 (0,0)    (0,1)   (0,2)   (0,3)   (0,4)
    }                                                 (1,0)    (1,1)   (1,2)   (1,3)   (1,4)
  system(“pause”);                                    (2,0)    (2,1)   (2,2)   (2,3)   (2,4)
  return 0;                                           (3,0)    (3,1)   (3,2)   (3,3)   (3,4)
 }                                                    (4,0)    (4,1)   (4,2)   (4,3)   (4,4)

         Computer Programming I                                                        46
for loop
 #include <iostream>
 #include <conio.h>

 using namespace std;

 int main()
 {
    int i,j;
    for (i=0; i<=4; i++)
    {
        for (j=0; j<=4; j++)                            i=j      output           i<j
             cout << "(" << i << "," << j << ")t";
        cout << endl;                                   (0,0)   (0,1)   (0,2)   (0,3)   (0,4)
    }                                                   (1,0)   (1,1)   (1,2)   (1,3)   (1,4)
  system(“pause”);                                      (2,0)   (2,1)   (2,2)   (2,3)   (2,4)
  return 0;                                             (3,0)   (3,1)   (3,2)   (3,3)   (3,4)
 }                                                i>j   (4,0)   (4,1)   (4,2)   (4,3)   (4,4)

         Computer Programming I                                                         47
for loop
 #include <iostream>
 #include <conio.h>

 using namespace std;

 int main()
 {
                                                      How to produce
                                       ?
    int i,j;
    for (i=0; i<=4; i++)
    {                                                  this output?
        for (j=0; j<=4; j++)
             cout << "(" << i << "," << j << ")t";
        cout << endl;
    }                                                 (1,0)
  system(“pause”);                                    (2,0) (2,1)
  return 0;                                           (3,0) (3,1) (3,2)
 }                                                    (4,0) (4,1) (4,2) (4,3)

         Computer Programming I                                                 48
for loop
 #include <iostream>
 #include <conio.h>

 using namespace std;

 int main()
 {
    int i,j;
    for (i=0; i<=4; i++)
    { for (j=0; j<=4; j++)
             if (i>j)
                                                          output      
                  cout << "(" << i << "," << j << ")t";
        cout << endl;
    }                                                      (1,0)
  system(“pause”);                                         (2,0) (2,1)
  return 0;                                                (3,0) (3,1) (3,2)
 }                                                         (4,0) (4,1) (4,2) (4,3)

         Computer Programming I                                                      49
Multi-way if-else-statements
 An if-else-statement is a two-way branch
 Three or four (or more) way branches can be
  designed using nested if-else-statements




   Computer Programming I                       50
Multi-way if-else
 #include <iostream>                        if (no==1)
 #include <conio.h>                            price=10;
                                            else if (no==2)
 using namespace std;                          price=9;
                                            else if (no==3)
 int main()                                    price=7.5;
 {                                          else if (no==4)
    int no;                                    price=6.5;
    double price;                           else if (no==5)
    double total;                              price=5.5;
                                            else
   cout << “Enter no. of students ";           price=4.5;
   cin >> no;                               total=no*price;
                                            cout << "the total price is " << total;
                                           system(“pause”);
                                           return 0;
                                       }
         Computer Programming I                                                       51
The switch-statement
 The switch-statement is an alternative for
  constructing multi-way branches




   Computer Programming I                      52
switch-statement Syntax
 switch (controlling expression)
  {
      case Constant_1:
                           statement_Sequence_1
                           break;
       case Constant_2:
                           Statement_Sequence_2
                           break;
               ...
        case Constant_n:
                           Statement_Sequence_n
                           break;
        default:
                           Default_Statement_Sequence
  }


   Computer Programming I                               53
The break Statement
 The break statement ends the switch-statement
    Omitting the break statement will cause the
     code for the next case to be executed!
    Omitting a break statement allows the use of
     multiple case labels for a section of code
      case 'A':
       case 'a':
                   cout << "Excellent.";
                   break;

      Runs the same code for either 'A' or 'a'


   Computer Programming I                           54
The default Statement
 If no case label has a constant that matches the
  controlling expression, the statements following
  the default label are executed
    If there is no default label, nothing happens
      when the switch statement is executed
    It is a good idea to include a default section




   Computer Programming I                         55
Output:
switch (if-else to4switch)
Enter no. of students
the total<iostream> 26
 #include price is        //break will exit switch()
 #include <conio.h>                        switch (no)
Note: 26 = 4 * 6.5                         {
 using namespace std;                         case 1: price=10.0; break;
                                              case 2: price=9.0; break;
 int main()                                   case 3: price=7.5; break;
 {                                            case 4: price=6.5; break;
    int no;                                   case 5: price=5.5; break;
    double price;                             default: price=4.5;
    double total;                          }
                                           total=no*price;
   cout << “Enter no. of students ";       cout << "the total price is " << total;
   cin >> no;                              return 0;
                                       }




         Computer Programming I                                                      56
Output:
switch (if-else to 4
Enter no. of students
                      switch)
the total price is 18
 #include <iostream>         //break will exit switch()
 #include <conio.h>                        switch (no)
                                           {
Note: 18 = 4 * 4.5std;
using namespace                               case 1: price=10.0;
                                              case 2: price=9.0;
 int main()                                   case 3: price=7.5;
 {                                            case 4: price=6.5;
    int no;                                   case 5: price=5.5;
    double price;                             default: price=4.5;
    double total;                          }
                                           total=no*price;
   cout << “Enter no. of students ";       cout << "the total price is " << total;
   cin >> no;                              return 0;
                                       }




         Computer Programming I                                                      57
#include <iostream>
switch   #include <conio.h>
         using namespace std;
         int main() {
             int no;
             cout << "Please enter a number ";
             cin >> no;

              switch (no)                //no break is used
             {
                case 1: cout << "1" << " your number is " << no << endl;
                case 2: cout << "2" << " your number is " << no << endl;
                case 3: cout << "3" << " your number is " << no << endl;
                case 4: cout << "4" << " your number is " << no << endl;
                case 5: cout << "5" << " your number is " << no << endl;
                default:
                   cout << "No such number" << endl;
              }

             return 0;
         }
    Computer Programming I                                                 58
#include <iostream>
switch   #include <conio.h>
         using namespace std;
         int main() {
              int no;
              cout << "Please enter a number ";
              cin >> no;

              switch (no)        //break will cause program to exit switch()
              {
                case 10: cout << "your number is 10" ; break;
                case 20:
                case 30: cout << "your number is 20 or 30" ; break;
                case 40: cout << "your number is 40" ; break;
                default: cout << "No such number" << endl;
             }

             return 0;
         }


    Computer Programming I                                                     59
Static cast
   Type casts are data conversions specified by the
    programmer. A Type Cast produces a value of one type
    from another type

   var1 = static_cast <var1_type> (var2);

   Example:       int a = 5;
                   double b;
                   b = static_cast <double> (a);
   b is now have a value 5.0 (note the decimal to indicate
    floating point value)


     Computer Programming I                                   60
Static cast
 A problem with integer division:
   int sugarInKg = 10, noOfPeople = 4;
   double kgPerPerson;
   kgPerPerson = sugarInKg / noOfPeople;
        (2.0)          (10)      (4)

 kgPerPerson should be 2.5, not 2.0!
 Solutions:
       1) declare sugarInKg as double OR
       2) static_cast sugarInKg to double.
   int sugarInKg = 10, noOfPeople = 4;
   double kgPerPerson;
   kgPerPerson = static_cast <double> (sugarInKg) / noOfPeople;
        (2.5)                 (10.0)                   (4)
      Computer Programming I                                      61
Predefined functions
  C++ comes with libraries of predefined functions.

  To make the functions available, the library must be
   “included” in a program using include directive.

  An include directive tells the compiler which
   library header file to include.

  Example: To use the sqrt function from the math library,
   cmath, we need to define:

                      #include <cmath>



     Computer Programming I                               62
Predefined functions




     Computer Programming I   63
Predefined functions

  Function call syntax:
     functionName (argument1, argument2, …);

  Example: sqrt function returns, or computes, the square root
  of a number
                      answer = sqrt (9.0);

              return value   function name   argument

                answer now have a value of 3.0



     Computer Programming I                                      64
Predefined functions

   Function arguments can be

      1) Constants:            sqrt ( 4.0 );
                                                      from double
      2) Variables:            sqrt ( x );              data type

      3) Expressions:          sqrt ( sqrt ( x ) );

                               sqrt ( 3 – 6*x );




     Computer Programming I                                   65
Predefined functions

   pow (x, y)
      Returns value of x to the power of y
      Return value is of type double
      Both arguments are of type double
      Found in the library cmath


                      value = pow (2.0, 3.0);
       return value    function name   argument 1   argument 2



     Computer Programming I                                      66
End of slides




     Computer Programming I   67

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
The pseudocode
The pseudocodeThe pseudocode
The pseudocode
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Basic structure of computers
Basic structure of computersBasic structure of computers
Basic structure of computers
 
08 Operating System Support
08  Operating  System  Support08  Operating  System  Support
08 Operating System Support
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Graphics User Interface Lab Manual
Graphics User Interface Lab ManualGraphics User Interface Lab Manual
Graphics User Interface Lab Manual
 
Operating Systems Process Scheduling Algorithms
Operating Systems   Process Scheduling AlgorithmsOperating Systems   Process Scheduling Algorithms
Operating Systems Process Scheduling Algorithms
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
Program logic and design
Program logic and designProgram logic and design
Program logic and design
 
Introduction to computer programming
Introduction to computer programmingIntroduction to computer programming
Introduction to computer programming
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Introduction to toc and compiler
Introduction to toc and compilerIntroduction to toc and compiler
Introduction to toc and compiler
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
C if else
C if elseC if else
C if else
 

Destacado (7)

Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
 
Computer Programming- Lecture 11
Computer Programming- Lecture 11Computer Programming- Lecture 11
Computer Programming- Lecture 11
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
 

Similar a Computer Programming- Lecture 4

Programa.eje
Programa.ejePrograma.eje
Programa.ejeguapi387
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 
Assignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zAssignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zSyed Umair
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control StructureMohammad Shaker
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacionJeff Tu Pechito
 
Array programs
Array programsArray programs
Array programsALI RAZA
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Deepak Singh
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends? ?
 
Data structures lab
Data structures labData structures lab
Data structures labRagu Ram
 

Similar a Computer Programming- Lecture 4 (20)

Programa.eje
Programa.ejePrograma.eje
Programa.eje
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Problemas de Arreglos en c++
Problemas de Arreglos en c++Problemas de Arreglos en c++
Problemas de Arreglos en c++
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
Assignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zAssignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.z
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
Array programs
Array programsArray programs
Array programs
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends
 
Data structures lab
Data structures labData structures lab
Data structures lab
 

Último

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 

Último (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 

Computer Programming- Lecture 4

  • 1. Lecture 4 Procedural Abstraction and Predefined Functions Computer Programming I 1
  • 2. Outline  for loop  switch  casting  predefined functions Computer Programming I 2
  • 3. for loop  A for-loop is another loop mechanism in C++  Designed for common tasks such as adding numbers in a given range  Is sometimes more convenient to use than a while loop  Does not do anything a while loop cannot do Computer Programming I 3
  • 4. for/while Loop Comparison  sum = 0; n = 1; while(n <= 10) // add the numbers 1 - 10 { sum = sum + n; n++; }  sum = 0; for (n = 1; n <= 10; n++) //add the numbers 1 - 10 sum = sum + n; Computer Programming I 4
  • 5. For Loop Dissection  The for loop uses the same components as the while loop in a more compact form  for (n = 1; n <= 10; n++) Initialization Action Update Action Boolean Expression Computer Programming I 5
  • 6. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 6
  • 7. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 1 int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 7
  • 8. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 1 int i; 2 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 8
  • 9. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 1 If true then execute body int i; 2 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 9
  • 10. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 12 int i; 2 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 10
  • 11. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 12 int i; 2 3 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 11
  • 12. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 12 If true then execute body int i; 2 3 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 12
  • 13. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 123 int i; 2 3 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 13
  • 14. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 123 int i; 2 3 for (i=1; i<=3; i++) 4 { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 14
  • 15. for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 123 If true then execute body int i; 2 3 for (i=1; i<=3; i++) 4 { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 15
  • 16. for loop #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 123 If true then execute body int i; 2 3 for (i=1; i<=3; i++) 4 { cout<<i<<“ “; } system(“pause”); Not True return 0; } Computer Programming I 16
  • 17. for loop i j #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 17
  • 18. for loop i j 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 18
  • 19. for loop i j 1 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 19
  • 20. for loop i j 1 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij cout << endl; } system(“pause”); return 0; } Computer Programming I 20
  • 21. for loop i j 1 1 #include <iostream> #include <conio.h> 2 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij cout << endl; } system(“pause”); return 0; } Computer Programming I 21
  • 22. for loop i j 1 1 #include <iostream> #include <conio.h> 2 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 22
  • 23. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 23
  • 24. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 24
  • 25. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 25
  • 26. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 26
  • 27. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; ij } system(“pause”); return 0; } Computer Programming I 27
  • 28. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; ij } system(“pause”); return 0; } Computer Programming I 28
  • 29. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; ij ij } system(“pause”); return 0; } Computer Programming I 29
  • 30. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() 3 { int i,j; 3 1 2 for (i=1; i<=3; i++) 3 { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; ij ij ij } system(“pause”); ij ij ij return 0; } Computer Programming I 30
  • 31. for loop #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 31
  • 32. for loop i j #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 32
  • 33. for loop i j 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 33
  • 34. for loop i j 1 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 34
  • 35. for loop i j 1 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 cout << endl; } system(“pause”); return 0; } Computer Programming I 35
  • 36. for loop i j 1 1 #include <iostream> #include <conio.h> 2 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 cout << endl; } system(“pause”); return 0; } Computer Programming I 36
  • 37. for loop i j 1 1 #include <iostream> #include <conio.h> 2 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 cout << endl; } system(“pause”); return 0; } Computer Programming I 37
  • 38. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 cout << endl; } system(“pause”); return 0; } Computer Programming I 38
  • 39. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; } system(“pause”); return 0; } Computer Programming I 39
  • 40. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; } system(“pause”); return 0; } Computer Programming I 40
  • 41. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; } system(“pause”); return 0; } Computer Programming I 41
  • 42. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; 21 } system(“pause”); return 0; } Computer Programming I 42
  • 43. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; 21 } system(“pause”); return 0; } Computer Programming I 43
  • 44. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; 21 22 } system(“pause”); return 0; } Computer Programming I 44
  • 45. for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() 3 { int i,j; 3 1 2 for (i=1; i<=3; i++) 3 { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; 21 22 23 } system(“pause”); 31 32 33 return 0; } Computer Programming I 45
  • 46. for loop #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) output cout << "(" << i << "," << j << ")t"; cout << endl; (0,0) (0,1) (0,2) (0,3) (0,4) } (1,0) (1,1) (1,2) (1,3) (1,4) system(“pause”); (2,0) (2,1) (2,2) (2,3) (2,4) return 0; (3,0) (3,1) (3,2) (3,3) (3,4) } (4,0) (4,1) (4,2) (4,3) (4,4) Computer Programming I 46
  • 47. for loop #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) i=j output i<j cout << "(" << i << "," << j << ")t"; cout << endl; (0,0) (0,1) (0,2) (0,3) (0,4) } (1,0) (1,1) (1,2) (1,3) (1,4) system(“pause”); (2,0) (2,1) (2,2) (2,3) (2,4) return 0; (3,0) (3,1) (3,2) (3,3) (3,4) } i>j (4,0) (4,1) (4,2) (4,3) (4,4) Computer Programming I 47
  • 48. for loop #include <iostream> #include <conio.h> using namespace std; int main() { How to produce ? int i,j; for (i=0; i<=4; i++) { this output? for (j=0; j<=4; j++) cout << "(" << i << "," << j << ")t"; cout << endl; } (1,0) system(“pause”); (2,0) (2,1) return 0; (3,0) (3,1) (3,2) } (4,0) (4,1) (4,2) (4,3) Computer Programming I 48
  • 49. for loop #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) if (i>j)  output  cout << "(" << i << "," << j << ")t"; cout << endl; } (1,0) system(“pause”); (2,0) (2,1) return 0; (3,0) (3,1) (3,2) } (4,0) (4,1) (4,2) (4,3) Computer Programming I 49
  • 50. Multi-way if-else-statements  An if-else-statement is a two-way branch  Three or four (or more) way branches can be designed using nested if-else-statements Computer Programming I 50
  • 51. Multi-way if-else #include <iostream> if (no==1) #include <conio.h> price=10; else if (no==2) using namespace std; price=9; else if (no==3) int main() price=7.5; { else if (no==4) int no; price=6.5; double price; else if (no==5) double total; price=5.5; else cout << “Enter no. of students "; price=4.5; cin >> no; total=no*price; cout << "the total price is " << total; system(“pause”); return 0; } Computer Programming I 51
  • 52. The switch-statement  The switch-statement is an alternative for constructing multi-way branches Computer Programming I 52
  • 53. switch-statement Syntax  switch (controlling expression) { case Constant_1: statement_Sequence_1 break; case Constant_2: Statement_Sequence_2 break; ... case Constant_n: Statement_Sequence_n break; default: Default_Statement_Sequence } Computer Programming I 53
  • 54. The break Statement  The break statement ends the switch-statement  Omitting the break statement will cause the code for the next case to be executed!  Omitting a break statement allows the use of multiple case labels for a section of code  case 'A': case 'a': cout << "Excellent."; break;  Runs the same code for either 'A' or 'a' Computer Programming I 54
  • 55. The default Statement  If no case label has a constant that matches the controlling expression, the statements following the default label are executed  If there is no default label, nothing happens when the switch statement is executed  It is a good idea to include a default section Computer Programming I 55
  • 56. Output: switch (if-else to4switch) Enter no. of students the total<iostream> 26 #include price is //break will exit switch() #include <conio.h> switch (no) Note: 26 = 4 * 6.5 { using namespace std; case 1: price=10.0; break; case 2: price=9.0; break; int main() case 3: price=7.5; break; { case 4: price=6.5; break; int no; case 5: price=5.5; break; double price; default: price=4.5; double total; } total=no*price; cout << “Enter no. of students "; cout << "the total price is " << total; cin >> no; return 0; } Computer Programming I 56
  • 57. Output: switch (if-else to 4 Enter no. of students switch) the total price is 18 #include <iostream> //break will exit switch() #include <conio.h> switch (no) { Note: 18 = 4 * 4.5std; using namespace case 1: price=10.0; case 2: price=9.0; int main() case 3: price=7.5; { case 4: price=6.5; int no; case 5: price=5.5; double price; default: price=4.5; double total; } total=no*price; cout << “Enter no. of students "; cout << "the total price is " << total; cin >> no; return 0; } Computer Programming I 57
  • 58. #include <iostream> switch #include <conio.h> using namespace std; int main() { int no; cout << "Please enter a number "; cin >> no; switch (no) //no break is used { case 1: cout << "1" << " your number is " << no << endl; case 2: cout << "2" << " your number is " << no << endl; case 3: cout << "3" << " your number is " << no << endl; case 4: cout << "4" << " your number is " << no << endl; case 5: cout << "5" << " your number is " << no << endl; default: cout << "No such number" << endl; } return 0; } Computer Programming I 58
  • 59. #include <iostream> switch #include <conio.h> using namespace std; int main() { int no; cout << "Please enter a number "; cin >> no; switch (no) //break will cause program to exit switch() { case 10: cout << "your number is 10" ; break; case 20: case 30: cout << "your number is 20 or 30" ; break; case 40: cout << "your number is 40" ; break; default: cout << "No such number" << endl; } return 0; } Computer Programming I 59
  • 60. Static cast  Type casts are data conversions specified by the programmer. A Type Cast produces a value of one type from another type  var1 = static_cast <var1_type> (var2);  Example: int a = 5; double b; b = static_cast <double> (a);  b is now have a value 5.0 (note the decimal to indicate floating point value) Computer Programming I 60
  • 61. Static cast A problem with integer division: int sugarInKg = 10, noOfPeople = 4; double kgPerPerson; kgPerPerson = sugarInKg / noOfPeople; (2.0) (10) (4) kgPerPerson should be 2.5, not 2.0! Solutions: 1) declare sugarInKg as double OR 2) static_cast sugarInKg to double. int sugarInKg = 10, noOfPeople = 4; double kgPerPerson; kgPerPerson = static_cast <double> (sugarInKg) / noOfPeople; (2.5) (10.0) (4) Computer Programming I 61
  • 62. Predefined functions  C++ comes with libraries of predefined functions.  To make the functions available, the library must be “included” in a program using include directive.  An include directive tells the compiler which library header file to include.  Example: To use the sqrt function from the math library, cmath, we need to define: #include <cmath> Computer Programming I 62
  • 63. Predefined functions Computer Programming I 63
  • 64. Predefined functions Function call syntax: functionName (argument1, argument2, …); Example: sqrt function returns, or computes, the square root of a number answer = sqrt (9.0); return value function name argument answer now have a value of 3.0 Computer Programming I 64
  • 65. Predefined functions Function arguments can be 1) Constants: sqrt ( 4.0 ); from double 2) Variables: sqrt ( x ); data type 3) Expressions: sqrt ( sqrt ( x ) ); sqrt ( 3 – 6*x ); Computer Programming I 65
  • 66. Predefined functions  pow (x, y)  Returns value of x to the power of y  Return value is of type double  Both arguments are of type double  Found in the library cmath value = pow (2.0, 3.0); return value function name argument 1 argument 2 Computer Programming I 66
  • 67. End of slides Computer Programming I 67