Please enable JavaScript.
Coggle requires JavaScript to display documents.
Language Design Principles - Coggle Diagram
Language Design Principles
Expression and Assignment Statement
Conversion
Types of conversion
Narrowing Conversion
Converts a value to a type that cannot store even approximations of all the values of the original type. Fractional part after the decimal point will be lost.
Example : Converting a real to int (from 3.5 to 3)
Disadvantage : potentially losing information
Widening Conversion
Example : Converting an int to real (from 3 to 3.0)
Advantage : it is safe, as there is no value change (no information loss)
Converts a value to a type that can include at least approximations of all the values of the original type
Implicit
Takes place when operands are automatically converted into values of the correct data type for an expression and performed automatically by the compiler without the programmer's intervention
Advantages
: Improve flexibility and automatically converting between operands
Explicit
Advantage :
Improve flexibility and limit type checking & readability.
Example :
float b,x:
x=(float)a+b;
int a;
Programmer can explicitly convert values of one data type into values of another data type.
Relational & Boolean Expressions
Boolean expressions
Constants (TRUE, FALSE)
Expressions (<, <=, >, <>, =)
Variables (a= true or false)
Operators (AND, OR, NOT)
Relational expressions
An operator that compares the values of its 2 operands
Has 2 operands & one relational operator
Returns a Boolean Value based on the comparison
Assignment Statement
Provides the mechanism by which the user can dynamically change the bindings of values to variables.
Common format :
<variable> <assignment operator> <expression>
Type of assignments
Compound assignment
•A compound assignment operator is a shorthand method of specifying a commonly needed form of assignment
• Example: x += y
equivalent
to x = x + y
Unary assignment
The value of z is assigned to y, and then value of y is assigned
to x. Finally the value of x, y and z are similar to each other.
Example: x-y-z
Assignment as expressions
Unary assignment operators
++ for increment
-- for decrement
Example: ++x
equivalent
to x=x+1
Mixed-mode Assignment
The data type which appears furthest down in the table will be the
data type of the combined expression.
If the two input expressions are of different data types, the
expression is said to be a mixed mode expression.
For example, an int plus a real produces a real (int + real = real)
Binary operators combine two expressions into a single expression.
Expression
The fundamental means of specifying
computations in a programming languages
Things to be addressed when writing expressions
Operators & operands
The order of evaluation
The type checking and
conversion
Arithmetic
Expressions
Arithmetic expressions consists of :
Operands
Parentheses
Used by programmers to alter the precedence & associativity rules by placing parentheses in expressions
A parenthesized part of an expression has precedence over its adjacent un-parenthesized parts
Function calls
Operators
Unary
Binary
Ternary
The purpose of an arithmetic expression is to specify an arithmetic computation
Order of evaluation
Precedence
Define the order in which the operators of different precedence levels are evaluated based on the hierarchy of operator priorities
Typical precedence levels
** (if the language supports it)
*,/
Unary operators
*,-
Parentheses
Associativity
When an expression contains 2 adjacent occurrences of operators with the same level of precedence, the question which operator is evaluated first is answered the associativity rules of the language
Will be used if the operators have equal precedence
Left-to-right or right-to-left
Left associativity
- Leftmost occurrences is evaluated first
Right associativity
- Rightmost occurrence is evaluated first
Object Oriented Programming
Polymorphism
Perform a single action in different ways
Inheritance
Possible to inherit attributes and methods from one class to another
Abstraction
A method that is declared without an implementation
Class
Represents the set of properties or methods that are common to all objects of one type
Encapsulation
Used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them
Object
Subprogram
Fundamental of Subprogram
A collection of statements that can be executed from multiple
points within aprogram.
Categories
Procedures :
Collections of statements that define parameterized computations
Example:
Void methods
Functions:
Functions structurally resemble procedures but are semantically modeled on mathematical functions
Return value
Vocabulary
Protocol is a subprogram’s parameter profile and, if it is a function, its return type
Subprogram declaration provides the protocol, but not the body, of the subprogram
Parameter profile aka Signature of a subprogram is the
number, order, and types of its parameters
Function declarations in C and C++ are often called prototypes
Subprogram header is the first part of the definition, including the
name, the kind of subprogram, and the formal parameters
Subprogram Call is an explicit request that the subprogram be
executed
Formal parameters is a dummy variable listed in the subprogram header and used in the subprogram
Subprogram definition Pass-by-name - the expression in the call is not evaluated until it is used by the function
Actual parameter represents a value or address used in the
subprogram call statement
Parameter Passing Methods
Pass-by-result - when an argument provides an output variable to which the called function can assign a result
Pass-by-value-result - when an argument provides both input to, and output from the called function
Pass-by-value - when an argument provides an input value to the called function.
Pass-by-reference - same as pass-by-value- result
Pass-by-name - the expression in the call is not evaluated until it is used by the function
Direction of
data passing
Return :
data goes from subprogram back to caller at the end of execution
Call-return :
data passes in both directions : into subroutine at call and back to caller at en of execution
Call :
data goes into subprogram only at beginning of execution
Statement=level Control Structure
Compound Statement/Sequential Structure
A collection of statements & executed in sequential order that is
from the beginning (first statement) to the end (last statement).
Always written within delimiters such as begin/end pair or brackets.
Control Structure/Control Statement
Used to modify the order of execution.
4 types of control statements:
Selection statements
Iterative statements
Compound statements/ Sequential Structure
Selection Statement
Provides the means of choosing between multiple possible execution paths
Can be categorized into 2 general categories:
Two-way selection – if … else
Multiple selection -
switch
Iterative Statement
An iterative statement is one that causes a statement or collection of statements to be executed zero, one or more times
2 styles can be applied to write an iterative statement: pre-test & post-test
Logic-controlled loops (Logical)
Flag – controlled loops
Counter-controlled loops (Counting)
A mixed controlled loops (Combination of both)