|< C Formatted I/O 2 | Main | C & C++ Program Controls 2 >| Site Index | Download |


 

 

 

 

MODULE 6

THE C/C++ CONTROL/LOOP PROGRAMMING 1

 

 

 

 

 

 

 

 

My Training Period:  xx hours

 

The source code for this Module is: C/C++ loops source codes. The lab worksheets for your practice are: C/C++ program control repetition 1, C/C++ program control repetition 2, C/C++ program control selection 1, C/C++ program control selection 2 and C/C++ program control selection 3.

 

The C and C++ programming abilities:

 

Able to understand and use:

  • The basic of the flow chart usages to describe C/C++ program control.

  • The if, if–else, if-else-if and their variation.

  • The switch-case-break statement.

  • The for statement.

 

6.1   Basic Flowchart

  • A flowchart is a graphical representation of an algorithm or a portion of an algorithm.

  • It is drawn using certain special-purpose symbols such as rectangles, diamonds, ovals, and small circles.

  • These symbols are connected by arrows called flow lines.

  • Flowcharts can clearly show how control structures operate.

  • The partial list some of the symbols used in this Module is shown in Table 6.1.  We will use flow charts to assist our study of the program controls.

C C++ program control flow chart

 

6.2    Program Execution

6.3       Program Control Types

  1. Sequence control structure.

  2. Selection structures such as if, if-else, nested if, if-if-else, if-else-if and switch-case-break.

  3. Repetition such as for, while and do-while.

6.3.1           Sequence Control Structure

#include <stdio.h>

 

int main()

{

  float rate = 5.0;

  int   hours = 25;

  float pay = (float) hours * rate;

  printf("\nPay = $%.2f \n", pay);

  return 0;

}

 

Output:

 

C C++ program control single entry single exit sequence flow

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

rate = 5.0                                         // S1

hours = 25                                       // S2

pay = (float) hours * rate                // S3

printf("\n Pay = $%.2f ", pay);        // S4

C C++ program control sequence flow

6.3.2           Selection Control Structure

C C++ program control if statement

  1. (expression) is evaluated.

  2. If TRUE (non-zero) the statement is executed.

  3. If FALSE (zero) the next_statement following the if statement block is executed.

  4. So, during the execution based on some condition, some codes not executed (skipped).

if (hours > 70)

    hours = hours + (hours - 70);

printf("...");

if(job_code == '1')

{

car_allowance = 200.00;

housing_allowance = 800.00;

entertainment_allowance = 500.00;

}

printf("...");

C C++ program control if-else statement

  1. The (expression) is evaluated.

  2. If it evaluates to non-zero (TRUE), statement_1 is executed, otherwise, if it evaluates to zero (FALSE), statement_2 is executed.

  3. They are mutually exclusive, meaning, either statement_1 is executed or statement_2, but not both.

  4. As usual the statements_1 and statements_2 can take the form of block and must be put in curly braces.

if(job_code == '1')

rate = 7.00;

else

rate = 10.00;

printf("...");

#include <iostream>

using namespace std;

 

int main()

{

       int  job_code;

       double housing_allowance, entertainment_allowance, car_allowance;

       cout<<"Available job codes: 1 or non 1:\n"<<endl;

       cout<<"Enter job code: ";

       cin>>job_code;

       // if 1 is selected

       if(job_code==1)

       {

              car_allowance = 200.00;

              housing_allowance = 800.00;

              entertainment_allowance = 250.00;

              cout<<"--THE BENEFITS--\n";

              cout<<"Car allowance: "<<car_allowance<<endl;

              cout<<"Housing allowance: "<<housing_allowance<<endl;

              cout<<"Entertainment allowance: "<<entertainment_allowance<<endl;

       }

       // other than 1

       else

       {

              car_allowance = 100.00;

              housing_allowance = 400.00;

              entertainment_allowance = 150.00;

              cout<<"--THE BENEFITS--\n";

              cout<<"Car allowance: "<<car_allowance<<endl;

              cout<<"Housing allowance: "<<housing_allowance<<endl;

              cout<<"Entertainment allowance: "<<entertainment_allowance<<endl;

       }

       return 0;

 }

 

Output:

 

C C++ program control if-else statement selection

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-------------------------------------------------------------------

C C++ loops program control if-else statement digestion

#include <iostream>

using namespace std;

 

int main()

{

    char  job_title;

    int   years_served, no_of_pub;

    cout<<"Enter data \n";

    cout<<"Current job (Tutor-T, lecturer-L, Assoc prof-A): ";

    cin>>job_title;

    cout<<"Years served: ";

    cin>>years_served;

    cout<<"No of publication: ";

    cin>>no_of_pub;

    if(job_title == 'T')

    {

       if(years_served > 15)

              if(no_of_pub > 10)

                     cout<<"\nPromote to lecturer";

              else

                      cout<<"\nMore publications required";

       else

               cout<<"\nMore service required";

    }

    else if(job_title == 'L')

    {

       if(years_served > 10)

              if(no_of_pub > 5)

                     cout<<"\nPromote to Assoc professor";

              else

                      cout<<"\nMore publications required";

       else

               cout<<"\nMore service required";

    }

    else if(job_title == 'A')

    {

           if(years_served > 5)

              if(no_of_pub > 5)

                     cout<<"\nPromote to professor";

              else

                  cout<<"\nMore publications required";

       else

           cout<<"\nMore service required";

    }

    

    cout<<"\n";

    return 0;

}

 

Output:

 

C C++ loop program control if-else if statement selection

 

C C++ program control if-else if statement multi selection

#include <iostream>

       using namespace std;

 

int main()

{

       int mark;

      

       cout<<"Enter student's mark:  ";

       cin>>mark;

       if (mark < 40)

          cout<<"Student grade = F";

       else if (mark < 50)

          cout<<"Student grade = E";

       else if (mark < 60)

          cout<<"Student grade = D";

       else if (mark < 70)

          cout<<"Student grade = C";

       else if (mark < 80)

          cout<<"Student grade = B";

       else

          cout<<"Student grade = A";

       cout<<"\n";

    return 0;

}

 

Output:

 

C C++ program control if-else if statement selection

// a program example of if-else statement.  This program

// is to test whether a banking transaction is a deposit,

// withdrawal, transfer or an invalid transaction, and to take the necessary action.

#include <iostream>

using namespace std;

 

int main()

{

          float   amount;

          char    transaction_code;

          cout<<"D - Cash Deposit, W - Cash Withdrawal, T - Cash Transfer\n";

          cout<<"\nEnter the transaction code(D, W, T); ";

          cin>>transaction_code;

          if (transaction_code == 'D')

         {

              cout<<"\nDeposit transaction";

              cout<<"\nEnter amount:   ";

              cin>>amount;

              cout<<"\nPROCESSING....Please Wait";

              cout<<"\nAmount deposited: "<<amount;

              cout<<"\n---THANK YOU!/TERIMA KASIH!---";

          }

        else if (transaction_code == 'W')

          {

              cout<<"\nWithdrawal transaction";

              cout<<"\nEnter amount: ";

              cin>>amount;

              cout<<"\nPROCESSING....Please Wait";

              cout<<"\nAmount withdrawn: "<<amount;

              cout<<"\n---THANK YOU!/TERIMA KASIH!---";

          }

        else if (transaction_code == 'T')

          {

                cout<<"\nTransfer transaction";

                cout<<"\nEnter amount: ";

                cin>>amount;

                cout<<"\nPROCESSING....Please Wait";

                cout<<"\nAmount transferred: "<<amount;

                cout<<"\n---THANK YOU!/TERIMA KASIH!---";

            }

          else  {

                         cout<<"\nInvalid transaction!!";

                         cout<<"D = Deposit, W = Withdrawal, T = Transfer";

                         cout<<"\nPlease enters the correct transaction code: ";

              }

        cout<<"\n";

        return  0;

}

 

Output:

 

C C++ program control if-else if statement selection example

 

 

C & C++ programming tutorials

 

 

 

 

 

 

 

 

 

 

 

 

Further C & C++ reading and digging:

 

  1. Check the best selling C / C++ books at Amazon.com.
  2. The source code for this Module is: C/C++ loops source codes.
  3. The lab worksheets for your practice are: C/C++ program control repetition 1, C/C++ program control repetition 2, C/C++ program control selection 1, C/C++ program control selection 2 and C/C++ program control selection 3.

 

 

 

 

 

 

|< C Formatted I/O 2 | Main | C & C++ Program Controls 2 >| Site Index | Download |


C Formatted Input/Output:  Part 1 | Part 2 | Part 3 | Part 4 |