Presentation is loading. Please wait.

Presentation is loading. Please wait.

BASIC ELEMENTS OF A COMPUTER PROGRAM

Similar presentations


Presentation on theme: "BASIC ELEMENTS OF A COMPUTER PROGRAM"— Presentation transcript:

1 BASIC ELEMENTS OF A COMPUTER PROGRAM

2 Contents 1. Statements 2. C++ Program Structure 3. Programming process
4. Control Structure

3 statements

4 Assignment statements

5 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!!

6 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;

7 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”);

8 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 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?

9 Converting algebraic expression into C++ statements

10 Writing algebraic expression using C++ statement.
Algebraic statement pow(a,b) ab sqrt(a - b) √ (a – b) abs(x - y) | x – y |

11 Arithmetic Expression
Examples Algebraic Expression Arithmetic Expression 17– 23 – 7 5 23 – 7 / 5 4 ( ) / 4 42 pow ( 4 , 2 )  ( a – b) sqrt (a – b )  x – y  abs ( x – y )

12 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)

13 Exercises s = t3 – uv3 g = 4∏2 a3 p2(m1+m2) 3) a = 2x + y 3(x – y)+ 2 4) a = 2(x2 + y) x – y x + y

14 Exercises a = √ x + y a = 2 √ x + y2 7 a = p √x – y √ x + y

15 Input output statements

16 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;

17 Output statements Sample output: … year = 1999;
cout << “my year of birth is”<<year; my year of birth is 1999

18 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

19 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;

20 Input statement: example
cout << “enter your age and matric number:”; cin>>age>>matric;

21 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);

22 Example Using cin statement: Sample output
cout<< “Enter your name:”; cin>>name; cout<< “Welcome, ”<<name<<“!”; Enter your name: Adam Ahmad Welcome, Adam!

23 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!

24 C++ Program structure

25 Components of a program
Preprocessor directives Constant and type definition section Main program heading Begin block Main block Declaration section Statement section End block

26 Syntax #include <headerFileName>
const <dataType><identifier> = <value> dataType <identifier> <functionType> <functionName> (formal parameter list) { cout << statement; cin >> variables; }

27 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

28 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.

29 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

30 Programming process

31 Compilation process

32 Running a program

33 Control structures

34 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.

35 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

36 Selection Control Structure
The selection structure allows comparison of expression, and based on the comparison, to select certain course of action

37 Selection Control Structure
Telephone rings? F T Continue reading Answer phone

38 Repetition control structure
The repetition structure allow a sequence of instructions to be executed repeatedly until a certain condition is achieved.

39 Repetition control structure
beg has items? false true Take item out

40 Modular Is a method of dividing a problem into separate tasks, each with a single purpose. Separate task with a single purpose = FUNCTION

41 Modular Company IT Accountancy Human resource

42 Thank You !


Download ppt "BASIC ELEMENTS OF A COMPUTER PROGRAM"

Similar presentations


Ads by Google