BASIC ELEMENTS OF A COMPUTER PROGRAM
Contents 1. Statements 2. C++ Program Structure 3. Programming process 4. Control Structure
statements
Assignment statements
Assigns a value to respective memory space allocated to a variable. Assignment statement Assigns a value to respective memory space allocated to a variable. May involve arithmetic expressions. Syntax: Examples variable = value/ variable; a = 3; x = 5.6; y = ‘z’; pi = 3.14; 5 = z; //invalid!!
Assignment statement Syntax: The expressions may contains variables, constant and arithmetic operators, Example: variable = expression; float radius, pi, area; radius = 3; pi = 3.14; area = pi * radius * radius;
A sequence of character: Assignment statement A sequence of character: Using assignment statement: Using strcpy function: char a [20] = “sample string”; char a [20]; strcpy(a ,“sample string”);
Exercises Write a C++ assignment statement that assigns the letter T to a char variable named insured. Write a C++ assignment statement that multiplies the number 10.75 by the content of a double variable named hours, and assigns the result to a double variable named grossPay. Assuming the answer variable is an int variable, the assignment statement answer=8/4*3-5%2; assign the value ______ to the answer variable. What will be assigned to the answer variable in question 3 if the variable is a double variable?
Converting algebraic expression into C++ statements
Writing algebraic expression using C++ statement. Algebraic statement pow(a,b) ab sqrt(a - b) √ (a – b) abs(x - y) | x – y |
Arithmetic Expression Examples Algebraic Expression Arithmetic Expression 17– 12 + 74 23 – 7 5 23 – 7 / 5 12 + 1 + 76 4 (12 + 1 + 76) / 4 42 pow ( 4 , 2 ) ( a – b) sqrt (a – b ) x – y abs ( x – y )
Examples: -b ± √b2 – 4ac 2a Solution : Or -b + sqrt ((pow (b,2) – (4 * a * c))/(2 * a) Or -b - sqrt (b * b – 4 * a * c) / (2 * a)
Exercises s = t3 – uv3 g = 4∏2 a3 p2(m1+m2) 3) a = 2x + y 3(x – y)+ 2 4) a = 2(x2 + y) + 1 x – y x + y
Exercises a = √ x + y a = 2 √ x + y2 7 a = p + 5√x – y √ x + y
Input output statements
Used to display information on screen. Output statements Used to display information on screen. The header file iostream.h must be included in the preprocessor directives. Syntax: The symbol << is called insertion operator cout<<variable<<variable<<…<<variable;
Output statements Sample output: … year = 1999; cout << “my year of birth is”<<year; my year of birth is 1999
cout << (2003 – year); Output statements The operand after the symbol << can be a constant, variable or an expression. The following are escape sequence which can be used in a string literal in a cout statement. cout << (2003 – year); \n – new line \’ – single quote \t - tab \” - double quote \b - backspace \\ - backslash
Used to read in information from a keyboard. Syntax: Input statement Used to read in information from a keyboard. Syntax: The symbol >> is called an extraction operator. The operand after the symbol >> must be a variable. cin>>variable>>variable>>…>>variable;
Input statement: example … cout << “enter your age and matric number:”; cin>>age>>matric;
When reading a sequence of character (including whitespace), used: Input statement cin only takes input up to the first blank (ignores whitespace such as blanks, tabs). When reading a sequence of character (including whitespace), used: cin.getline(name, 20);
Example Using cin statement: Sample output cout<< “Enter your name:”; cin>>name; cout<< “Welcome, ”<<name<<“!”; Enter your name: Adam Ahmad Welcome, Adam!
Using cin.getline statement: Example Using cin.getline statement: Sample output cout<< “Enter your name:”; cin.getline(name,20); cout<< “Welcome, ”<<name<<“!”; Enter your name: Adam Ahmad Welcome, Adam Ahmad!
C++ Program structure
Components of a program Preprocessor directives Constant and type definition section Main program heading Begin block Main block Declaration section Statement section End block
Syntax #include <headerFileName> const <dataType><identifier> = <value> dataType <identifier> <functionType> <functionName> (formal parameter list) { cout << statement; cin >> variables; }
A C++ program is a collection of one or more subprograms, called functions A subprogram or a function is a collection of statements that, when activated (executed), accomplishes something Every C++ program has a function called main
Preprocessor directives Examples: #include <iostream.h> To declares the basic C++ streams (I/O) routines. #include <math.h> Declares prototypes for the math functions and math error handlers. #include <conio.h> Declares various functions used in calling the operating system console I/O routines.
Example of program Preprocessor Directives main function #include <iostream.h> #include <conio.h> #include <string> void main() { string name; int age; cout<< "Please enter your name:\n"; cin>>name; cout<< "Please enter your age:\n"; cin>>age; cout<<endl; cout<< "Your name is:"<<name<<endl; cout<< "Your age is:"<<age<<endl; getch(); } Preprocessor Directives main function
Programming process
Compilation process
Running a program
Control structures
Sequential Control Structure The simplest of all the structures The program instruction has one starting point and one ending point. Each step is carried out in order of their position and is only done once.
Sequential Control Structure Flowchart Begin Fill a kettle with water Boil the water in the kettle Put tea leaves in the pot Pour boiling water into the pot End
Selection Control Structure The selection structure allows comparison of expression, and based on the comparison, to select certain course of action
Selection Control Structure Telephone rings? F T Continue reading Answer phone
Repetition control structure The repetition structure allow a sequence of instructions to be executed repeatedly until a certain condition is achieved.
Repetition control structure beg has items? false true Take item out
Modular Is a method of dividing a problem into separate tasks, each with a single purpose. Separate task with a single purpose = FUNCTION
Modular Company IT Accountancy Human resource
Thank You !