chapter 2 basic element of programming
DESCRIPTION
TRANSCRIPT
Chapter 2 : Basic Element of Programming Language and
Sequential Structure
Lecture Outline
Basic Element of Computer Program- General Overview
Data Manipulation- Standard data type (int, float, double, char, char[ ])- Variable
Computer Statement - Statement (constant, identifier)- Input/output statement - Arithmetic expression- Assignment concept
Basic Element of Computer Program
Comp. Statements
Sequence Selection Iteration
Problem Solving using Computer
Instruction/Steps Data
2
2
3 4
5
6Comp. ProgramProcedural Programming vsObject-oriented Programming
Functions•Independent•Method
Variable
Input /output
•Data type•Predefine/user define•Identifier•Size•Range•Lifespan•Scope
Data Manipulation
Types of data :1. Input data – key in by the user (need to solve the problem)2. Output data – process by the program and display to the user3. Temporary data – used by the program in the process of obtaining
the output data.
Data is presented by a variable in a computer program.Variable is a location in the computer’s memory where a value can be stored for use by a program.
Elements of the variable :Data Type (Pre Defined / User Defined)Lifespan – How long will the variable exist?Scope –Where can the variable being used?Identifier – Rules in using identifier
Pre Define Data Type (Size, modifier and range) Example : int, float, char and double
Data Type Modifiers Size (bytes) Rangeint Short, unsigned 2 0 to 65,535
Short, signed 2 -32,768 to 32, 767
Long, unsigned 4 0 to 4,294,967,295
Long, signed 4 -2,147,483,683 to 2,147,483,687
char 1 256 valuesfloat 4 1.2e-38 to 3.4e38
double 8 2.2e-308 to 1.8e308
Data Manipulation (cont.)
User Defined Data type – defined by user Example: object and array
Computer Statement
Program is a list of instruction that represented by the computer statement.
Language element than can be used in constructing high-level language programming form computer statement is called as Token.
6 types of token in C++ programming language :Reserved word IdentifierConstantString LiteralPunctuationOperator
Type of Token Description Example
Computer Statement (cont.)
Word that has a special meaning to compilerMust be typed in the correct locationMust be spell correctly (lower case letter)Must be used in the right context
Name which is used in a computer programOther than reserved wordMainly to name variables and functionRules :
consists of letters A..Z, a..z, 0..9, and _1st character must be a letterCase sensitivelength : < 32 character, recommended3 – 8 charactersMeaningful name
Reserved word
main( ), int, strcpy
Identifier Salary, SUM, PRO_10
Type of Token Description Example
Computer Statement (cont.)
Item with a fixed value (not be changed in any statement)Literal Constant – value typed directly,
wherever it is neededSymbolic Constant – represented by a name using a preprocessor directive of keyword const
Sequence of character surrounded by double quotation marksString literals may contain printable characters as \a, \n.
Constants
String Literals “ \n The minimum value is :”
int pie = 3.142
#define max 10.9const int size = 5
Type of Token Description Example
Computer Statement (cont.)
Separators in C++To limit the various syntactical units in
programming language
Result in computation or action when applied to variables or other elements in an expressionOperator act on operandUnary operator – operator that require one operandBinary operator – operator that require two operandTrenary operator – operator that required 3 operand (conditional operator (?:)
Punctuator
Operator
[ ], ( ), ;, *, #
+, -, *, <>, !=
A++, C—Total = sum + salarycout << (grade > 60 ? “ Passed” : “Failed”)
Several token will form a Statement.
Statement is an instructions to the computer.
Computer statement is a specification of an action to be executed by the computer. It is cause the processor to do something.
Example :
An input statement will input value and placed it to a variable
An output statement will print message or result to the user on the computer screen
Compound statement is a list of statement enclosed in braces { } that can contain declaration and any type of computer statement.
Computer Statement (cont.)
Types of computer statement
Input statementProcess statementOutput statement
Input statement – read the value for one or more variable from the input/user.
Format : Pre Defined data type variable
cin >> variable1 >> variable2 …. >>variablen;
Example :
cin>> first >> middle >> last;
Computer Statement (cont.)
Input Statement
Input statement for user defined data type variable
Example: array – use getline command
string name;
char name[20];
Format :
// I) use the string data type
getline (cin, variable_name)
or
// II) user know the maximum number of characters will be used
cin .getline (variable_name, number_of_characters);
Example
getline(cin, name)
cin.getline(name,20)
Output Statement
Output statement - print the value of one or more expressions.
Format : cout << expression1 << expression2 << … << expressionn;
Example :cout << ringgit;cout << “ Total value : “ << ringgit<<endl;
Print format : endl – cursor go to next line\n - cursor go to next line\t - the print out will tab the printout etc ..
Process Statement
Process statement – to ask processor to an actionTypes of process statement :
Assignment statementFunction call statement
Assignment statement – to store the given value or value of an expression in a variable
Format :variable = expression;
Example :sum = ringgit * 0.01 ;ringgit = 100;
Assignment statement
Assignment statement also can use to the user defined data type such as array of character (string copy – strcpy)
Purpose – to copy the content from one string variable to other string variable (and make sure the size of the variable are same)
Format:
strcpy (new_variable_name,old_variable_name)
Example:
char old[20], new[20];
strcpy (new, old)
Function Call statement
Function call statement- used to execute the statement in a particular function.
Purpose : The result of calling a function and supplying the values for the function parameters.
Format :function_name( expression1, expression2,…
expressionn);Example:
cout << calculatepower( ); // with output statement
cout<<pow ( z+y, n); / /with parameter
value =sqrt(x);total = calculate ( x, y ); // with assignment statement
Expression Concepts
Programmers should know how to construct the expression and how to get the value.
The expression should syntactically correct and meaningful combination of operators and operand.
Type of expression
Regular expressionArithmetic expressionLogical Expression (discuss in chapter 3)
Regular expression – describing numbers and denotes any digit
between 0 – 9.
Example : x = 20;
Arithmetic Expression
Arithmetic expression – using arithmetic operator such as +, -, / etc
Example :
sum = no1 + no2
total = (no1 – 10) + ( no3 – 20)
C++ Operation
Arithmetic Operator
Algebraic expression C++ expression
Addition + F + 5 F + 5
Subtraction - P - C P - C
Multiplication x B x M P * M
Division / X / Y or X ÷ Y X / Y
Modulus % R mod S R % S
Modulus - operator must applied to integer only give the remainder of the integer division
Arithmetic Expression (cont.)
Operator Operation Order of evaluation
( ) Parentheses Evaluated first, left to right
*, /, or % MultiplicationDivisionModulus
Evaluated last, if there are several, left to right.
+ or - AdditionSubtraction
Evaluated last, if there are several, left to right.
Precedence of arithmetic operators
Sequential Structure
Sequence structure – the computer executes C++ statements one after the other in order in which they are written.
Sequential Structure
Output StatementInput Statement Process Statement
Assignmentstatement
Function Call Statement