Control Structures (B) • Topics to cover here: • Sequencing in C++ language Coding (Writing a Program) • After testing your algorithm, you can code it in any programming language. • In our lab, we are going to use C++ language. • First, we have to see the C++ language elements and syntax that correspond to what we have studied in the algorithmic language. Note that most of the syntax used in the pseudo code is similar to that of C++, with miner differences. • Then, we convert all previous given algorithms into C++ programs. C++ Language Elements • The general form of a C++ program // File: filename // Program description # include compiler directives void main ( ) //main function of the program { //declarations section //executable statement section } 1- Comments in Programs • In C++, the two symbols // are used to denote a program comment. • If comments need more than one line, then you can use the symbols /* to begin comment and */ to end it. • Usually, the first line of comment is the one that contains the file name. 2- The include compiler directive • The line begins with #include represents a compiler directive. • A compiler directive is processed at compilation time. • C++ syntax: # include <filename> • The #include statement instructs the compiler to insert the indicated C++ instructions (of the filename) into the program in place of the directive. • e.g. #include <iostream.h> • iostream.h is the name of a C++ library header file whose contents are inserted in place of the #include line during compilation. • iostream.h is used to manipulate input/output operations on objects consisting of streams of characters that are external to the program. • The standard I/O stream objects, cin and cout are already defined in iostream.h 3- Declaration Section • The declaration section tells the compiler what data are needed in the program. • Declarations are based on the problem data requirements identified during the problem analysis. • An identifier is associated with each data element. The identifiers in C++ follow the same rules that build identifiers in the algorithmic language. • All identifiers must be declared before they are used. • Every identifier associated with a problem data element must be declared only once in the declaration section. Syntax: <type> List-of-identifiers where, type is any predefined type in C++, and List-of-identifiers is a list that contains one or more identifiers. 1- Declaring identifiers of integer type : int x ; int x, y; 2- Declaring identifiers of character type: char c1, c2 ; 3- Declaring identifiers to hold real numbers: float sum ; double total ; 4- Executable Statements Section • Execution statements cause some kind of action to occur when a program is executed. • We have seen three executable statements in the algorithmic language: INPUT, OUTPUT, and Assignment Statements. A) The Input statement in C++ Syntax: cin >> identifier >> identifier ; This will cause any data value, typed by the user from the standard input devise (usually the keyboard), to be read into the identifiers mentioned in the statement. e.g. cin >> length ; cin >> x >> y ; B) The Output Statement in C++ 1- Syntax: cout << identifier << identifier ; This statement causes the values of the identifiers to be displayed to the standard output device (e.g. a screen). e.g. cout << length ; 2- Syntax: cout << “any message” ; This statement causes the message between quotation to be displayed to the standard output device. e.g. cout << “ Enter three integer values “ ; 3- Syntax: cout << endl ; This statement causes a new line to be displayed in the output. Use this for output clarity. Note: You can mix between all type of cout. C) The Assignment Statement in C++ Syntax: variable = Expression ; This statement causes the value of Expression to be assigned into the variable on the LHS of the assignment statement. e.g. x=y; z=t*2; w=6; w=w+1; NOTE: Expression has the same operations with the same precedence rules as with the algorithmic language with some different symbols. Operator Precedence Operator Description () brackets !, + , not, unary plus, unary minus *, / , % multiplication, division, mod +.binary plus, binary minus <, <= ,>, >= less than, less or equal, greater than, greater or equal == , != equal, not equal && and || or = assignment Examples on C++ Programs • Now, we will code the previous examples given in the algorithmic language into C++ language. • Try to run the C++ programs in your next lab hour. • In the lab, we will use Visual C++ environment. Examples on C++ Programs Example 1: (refer to the algorithm of Example 1 on slide number 17) // File: apples.cpp /*This program calculates the price of purchasing some kilos of apples */ #include <iostream.h> void main ( ) { int quantity ; float cost , total_cost ; cin >> quantity >> cost ; total_cost = quantity * cost ; cout << “Total cost = “ << total_cost << endl ; } Examples on C++ Programs Example 2: (refer to the algorithm of Example 2 on slide number 20) //File: convert.cpp /*This program converts a given length from feet and inche into centimeters */ #include <iostream.h> void main ( ) { float length , centimeters ; cin >> length ; centimeters = 2.54 * length ; cout << “Length in centimeters= “ << centimeters << endl; } Examples on C++ Programs Example 3: (refer to the algorithm of Example 3 on slide number 23) //File: Pays.cpp /*This program calculates the wage that an employee will receive */ #include <iostream.h> void main ( ) { int hours ; //number of worked hours float rate, //rate per hour given to the employee wage ; //wage that the employee will earn cin >> hours >> rate ; wage = hours * rate ; cout << “Employee wage = “ << wage << endl; } Examples on C++ Programs Example 4: (refer to the algorithm of Example 4 on slide number 27) //File: Purchases.cpp /*A program calculates the price to pay for purchasing an item including the tax after some discount. */ #include <iostream.h> void main ( ) { float price, discount_rate, //original price, discount rate for an item tax_rate , discount, //tax rate on the purchased items and the // amount of discount on purchasing an item tax ; //the calculated tax that a customer will pay cin >> price >> discount_rate >> tax_rate ; discount = price * discount_rate ; price = price - discount ; tax = price * tax_rate ; price = price + tax ; cout << “Price = “ << price << endl ; }