LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM
Contents 1. Statements 2. C++ Program Structure 3. Programming process 4. Control Structure
STATEMENTS
ASSIGNMENT STATEMENTS
Assignment statement Assigns a value to respective memory space allocated to a variable. May involve arithmetic expressions. Syntax: variable = value/ variable; Examples 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;
Assignment statement A sequence of character: Using assignment statement: char a [20] = sample string ; Using strcpy function: char a [20]; strcpy(a, sample string );
CONVERTING ALGEBRAIC EXPRESSION INTO C++ STATEMENTS
Writing algebraic expression using C++ statement. C++ Statement pow(a,b) Algebraic statement a b sqrt(a - b) (a b) abs(x - y) x y
Examples Algebraic Expression Arithmetic Expression 17 12 + 74 17 12 + 74 23 7 23 7 / 5 5 12 + 1 + 76 (12 + 1 + 76) / 4 4 4 2 pow ( 4, 2 ) ( a b) sqrt (a b ) x y abs ( x y )
Examples: -b ± b 2 4ac 2a Solution : -b + sqrt ((pow (b,2) (4 * a * c))/(2 * a) Or -b - sqrt (b * b 4 * a * c) / (2 * a)
INPUT OUTPUT STATEMENTS
Output statements Used to display information on screen. The header file iostream.h must be included in the preprocessor directives. Syntax: cout<<variable<<variable<< <<variable; The symbol << is called insertion operator
Output statements year = 1999; cout << my year of birth is <<year; Sample output: my year of birth is 1999
Output statements The operand after the symbol << can be a constant, variable or an expression. cout << (2003 year); The following are escape sequence which can be used in a string literal in a cout statement. \n new line \ single quote \t - tab \ - double quote \b - backspace \\ - backslash
Input statement Used to read in information from a keyboard. Syntax: cin>>variable>>variable>> >>variable; The symbol >> is called an extraction operator. The operand after the symbol >> must be a variable.
Input statement: example cout << enter your age and matric number: ; cin>>age>>matric;
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: cout<< Enter your name: ; cin>>name; cout<< Welcome, <<name<<! ; Sample output Enter your name: Adam Ahmad Welcome, Adam!
Example Using cin.getline statement: cout<< Enter your name: ; cin.getline(name,20); cout<< Welcome, <<name<<! ; Sample output 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) { datatype <identifier> } 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
Example of program #include <iostream> #include <string> Preprocessor Directives using namespace std; int main() { string nickname; char fullname[50]; int age; main function Variable declaration cout<< "Please enter your nickname:\n"; cin.getline(fullname,50); cout<< "Please enter your fullname:\n"; cin>>nickname; cout<< "Please enter your age:\n"; cin>>age; cout<<endl; cout<< "Your nickname is:"<<nickname<<endl; cout<< "Your fullname is:"<<fullname<<endl; cout<< "Your age is:"<<age<<endl; Input Output } return 0;
Preprocessor directives A program that sets up your source code for the compiler The #include directive causes the preprocessor to include contents of another file in the program Examples: #include <iostream.h> / <iostream> To declares the basic C++ streams (I/O) routines. #include <math.h> / <cmath> 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.
main function Marks the beginning of a function function a block of code that performs a task main name of a function (every program must have only one main function) int stands for integer, indicates the function send an integer value back to the operating system when it finished executing () must ended with this no semicolon ; added at the end of a function return 0; - This sends the integer value 0 back to OS upon the program s completion 0 The value 0 indicates that a program executed successfully.
{ - beginning brace / open brace marks the beginning of the code block that comprises the function. } closing brace marks the end of the code block Everything between the opening and closing set of braces ({ }) belongs to this main function and is referred to as the function body. ; - semicolon marks the end of a complete programming statement
Predefined function - strcmp The strcmp are in the library with the header file string. #include<string> int y = strcmp(s1, s2); char a [20]; strcpy(a, sample string );
Predefined function - strcmp Problem statement: Write a C++ program to display the program of the student based on the code the he or she inputs in the computer. Display Invalid code if the student enter wrong code. Code C A E Program Computer Science Accounting Engineering C++ Program segment: if( code == C ) cout<<. ; else if ( code == E ) cout<<. ;...
Predefined function - strcmp Write a C++ program to display the code of the student based on the program the he or she inputs in the computer. Display Invalid program if the student enter wrong program. Code C A E Program Computer Science Accounting Engineering
Characters ASCII codes
Predefined function - strcmp cout<< "please enter program:"; cin.getline(program, 20); if(program == computer science ) cout<< C ; else if (program == Accounting ) cout<< A ; else if (program == Engineering ) cout<< E ; else cout<< invalid ; Is this the correct way???
Predefined function - strcmp int y = strcmp(s1, s2); strcmp will accept two sequence of characters. It will return an integer. The integer will be: Negative if s1 is less than s2 Zero is s1 and s2 are equal Positive if s1 is greater than s2
Predefined function - strcmp How to use it??? cout<< "please enter program:"; cin.getline(program, 20); int y = strcmp(program, computer science ); if(y == 0) cout<< yes ; else cout<< no ; The output:
Predefined function - strcmp How to use it?? cout<< "please enter program:"; cin.getline(program, 20); if(strcmp(program, computer science ) == 0) cout<< yes ; else cout<< no ;
Predefined function - strcmp cout<< "please enter program:"; cin.getline(program, 20); if(strcmp(program, computer science ) == 0) cout<< C ; else if (strcmp(program, Accounting ) == 0) cout<< A ; else if (strcmp(program, Engineering ) == 0) cout<< E ; else cout<< invalid ;
Characters ASCII codes
4 2 Problem statement: Write a C++ program that compare the brand of two handphone. This program should ask for the brand of handphone that user currently use and then compare the brand with the given brand, which is SAMSUNG. If the brand is same, then display Same like mine!. Otherwise display Not the same!
This is not the correct way to write the program!! 4 3
Characters ASCII codes
int y = strcmp(s1, s2); strcmp will accept two sequence of characters. In the library with the header file string. #include <string> It will return an integer. The integer will be: Negative if s1 is less than s2 Zero is s1 and s2 are equal Positive if s1 is greater than s2
This is the correct way to write the program!! 4 6
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 F Telephone rings? 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 false beg has items? 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 Accountancy IT Human resource
LOGO