Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

COMPUTER

-“to do things for us”-purpose  Assembly Language


-speak their language to describe what we -Uses mnemonics
want done. -Machine-dependent
-Users have it easy--users just pick the -Not portable
ones we want to use.
 High-Level Language
Users -Uses English-like language
-see computers as a set of tools - word -Machine independent
processor, spreadsheet, map, todo list, etc. -Portable (but must be compiled for
different platforms)
Programmers + Examples: Pascal, C, C++, Java,
- learn the computer “ways” and the Fortran, . . .
computer language .
- have some tools that allow them to build Low Level Languages
new tools. - Computer language consisting of
- sometimes write tools for lots of users mnemonics that directly correspond to
and sometimes. machine language instructions.
-write little “helpers” for themselves to -Assembly language, machine
automate a task. language, hardware.

A Layered View of the Computer High Level Languages


- symbolic languages that use English
 Application Programs words and/or mathematical symbols rather
-Word-Processors, Spreadsheets, than mnemonic codes.
Database Software, IDEs, etc… -uses statements consisting of English-like
keywords such as "FOR", "PRINT" or “IF“, ...
 System Software etc.
- Compilers, -one-to-many correspondence
Interpreters,Preprocessors, etc. -Much easier to program than in assembly
Operating System, Device Drivers language.
Machine with all its hardware. -Data are referenced using descriptive
names.
Programs -Operations can be described using familiar
- are written in programming languages. symbols.

PL / programming language -EXAMPLE: visual languages, fortran, c,


-Pieces of the same program can be written pascal, high - level language.
in different.
-special purpose and limited language. Machine Language
- set of rules and symbols used to -The representation of a computer program
construct a computer program. which is actually read and understood by
- language used to interact with the the computer.
computer. - A program in machine code consists of a
sequence of machine instructions.
PLs / Programming Languages Instructions:
- Languages closer to the machine can be - Machine instructions are in binary code
more efficient, As long as they agree on - Instructions specify operations and
how to communicate. memory cells involved in the operation.

Computer Languages

 Machine Language
-Uses binary code
-Machinedependent
-Not portable
Assembly Language Dynamically Type
-symbolic representation of the machine -checked at run-time.
language of a specific processor.
-converted to machine code by an *Type Safety: Safe or Unsafe
assembler. -degree to which a language will prohibit
-Each line of assembly code produces one operations on typed variables that might
machine instruction (One-to-one lead to undefined behavior or errors.
correspondence). Safe Language
-Programming in assembly language is -will do more to ensure that such
slow and error-prone but is more efficient operations or conversions do not occur.
in terms of hardware performance. Unsafe Language
- A Mnemonic representation of the -will give more responsibility to the user in
instructions and data. this regard.

Compiled Languages PROGRAMMING PARADIGM


-are translated to the target machine's -methodology or way of programming that
native language by a program called a a programming language supports.
COMPILER.
-This can result in very fast code, especially Common Paradigms
if the compiler is effective at optimizing, ▪ Declarative
however the resulting code may not port ▪ Functional
well across operating systems and the ▪ Generic
compilation process may take a while. ▪ Imperative
Interpreted Languages ▪ Structured
-are read by a program called an ▪ Procedural
interpreter and are executed by that ▪ Object-Oriented
program.
- portable as their interpreter and have no ▪ Declarative
long compile times. -supposed to accomplish rather than by
- usually much slower than an equivalent what means it is supposed to accomplish it.
compiled program. -paradigm might be used to avoid
undesired sideeffects resulting from having
TYPE SYSTEM to write one's own code.
-logical system comprising a set of rules.
-assigns a property called a type to the ▪ Functional
various constructs of a computer -subset of declarative programming.
program, such as variables, expressions, - tries to express problems in terms of
functions or modules. mathematical equations and functions.
-to avoid the concepts of states and
*Type Strength: Strong or Weak mutable variables which are common in
Strong Typing System imperative languages.
-puts restrictions on how different types of
variables can be converted to each other ▪ Generic
without any converting statements. -focuses on writing skeleton algorithms.
An ideal strong typing system would that
do not make any sense, such as an integer ▪ Imperative
to a Fruit object. -allow programmers to give the computer
Weak Typing System ordered lists of instructions
-would try to find some way to make the
cast work. forbid implicit "casts" to types. ▪ Structured
-aim to provide some form of noteworthy
*Type Checking: Static or Dynamic structure to a language.
Statically Typed
- compiler/interpreter does the type ▪ Procedural
checking once before the program runs/is -imperative structured programming
compiled. language which supports the concept of
procedures and subroutines.
▪ Object-Oriented  +
-abbreviated to OOP.  ==
-subset of structured programming which  -
expresses programs in the terms of  .
"objects".  <=
-meant to model objects in the real world. 
 ;
Organization  >=
❑ American National Standards Institute  /
(ANSI)  *
❑ International Organization for  \
Standardization (ISO)  !=
❑ De-facto standard
Reserved words, keywords, or word
History of C++ symbols:
-C is a programming language developed in ▪ int, float, double, char, const, void,
the 1970's alongside the UNIX operating return.
system.
-C provides a comprehensive set of Identifiers
features for handling a wide variety of - All C++ variables must be identified with
applications, such as systems development unique names.
and scientific computation. - These unique names.
-C++ is an “extension” of the C language, - can be short names (like x and y) or more
in that most C programs are also C++ descriptive names (age, sum, totalVolume).
programs.
-C++, as opposed to C, supports ✓ Names can contain letters, digits and
“objectoriented programming. underscores. ✓ Names must begin with a
letter or an underscore
Features of C++ ✓ Names are case sensitive ( myVar and
- open ISO-standardized language. myvar are different variables)
-compiled language. ✓ Names cannot contain whitespaces or
- strongly-typed unsafe language. special characters like !, #, %, etc.
-supports both manifest and inferred typing. ✓ Reserved words (like C++ keywords,
-supports both static and dynamic type such as int ) cannot be used as names.
checking.
-offers many paradigm choices.
-portable. Whitespaces
-upwards compatible with C -Every C++ program contains whitespaces-
- has incredible library support. - blanks, tabs, and newline characters.
-Used to separate special symbols,
BASICS OF C++ PROGRAMS reserved words, and identifiers.
Function: -Proper utilization of whitespaces is
- collection of statements; when executed, important.
accomplishes something.
Syntax: Data Type
-rules that specify which statements -specifies the size and type of information
(instructions) are legal. the variable will store.
Programming language:
-a set of rules, symbols, and special words. Int
Semantic rule: -4 bytes
-meaning of the instruction. - Stores whole numbers, without decimals.

Special Symbols Float


-4 bytes - Performed from left to right (associativity).
-Stores fractional numbers, containing one
or more decimals. Sufficient for storing 7 Expressions
decimal digits. -If all operands are integers.
-Expression is called an integral expression
Double - Yields an integral result - Example: 2 + 3
-8 bytes *5
-Stores fractional numbers, containing one -If all operands are floating-point.
or more decimals. -Expression is called a floating-point
-Sufficient for storing 15 decimal digits. expression - Yields a floating-point result.
Example: 12.8 * 17.5 - 34.50
Boolean
- 1 byte Mixed expression
-Stores true or false values -Has operands of different data types.
-bool keyword. -Contains integers and floating-point.
-used for conditional testing. Examples of mixed expressions:
-When the value is returned, true = 1 and ▪2 + 3.5
false = 0. ▪6 / 4 + 3.9
▪5.4 * 2 ? 13.6 + 18 / 2
char
-1 byte Evaluation rules:
-Stores a single character/letter/number, ➢If operator has same types of operands
or ASCII values. - Evaluated according to the type of the
-smallest integral data type. operands
-Each character is enclosed in single quotes.
-blank space is a character and is written ' ', ➢If operator has both types of operands
with a space left between the single quotes. - Integer is changed to floating-point
- Operator is evaluated
String Data Type - Result is floating-point
-store a sequence of characters (text).
-not a built-in type Entire expression is evaluated according to
-values must be surrounded by double precedence rules.
quotes “ ”
- Each character has relative position in Phases of C++ Programs
string. EDITOR—DISK
- Position of first character is 0. -Program is created in the editor and
- Length of a string is number of characters stored on disk.
in it.
Preprocessor—Disk
Arithmetic Operators -Preprocessor program processes the code.
-Operators are used to perform operations
on variables and values. Compiler-- Disk
- Compiler creates object code and stores it
Modulus- Return the division remainder. on disk.
Symbol: “%”
Increment- increases the value by 1 . Linker—Disk
Symbol: “++” - Linker links the object code with the
Decrement- decreases the value by 1. libraries, creates a.out and stores it on disk
Symbol: “--”
Operator Precedence Loader—Disk—Primary Memory
- All operations inside of () are evaluated - Loader puts program in memory.
first
- *, /, and % are at the same level of
precedence and are evaluated next. CPU—Primary Memory
- and ? have the same level of precedence - CPU takes each instruction and executes
and are evaluated last . it, possibly storing new data values as the
-When operators are on the same level. program executes.
T T T
Variables T F F
-portion of a memory to store a value. F T F
-programmers get choose the name of the F F F
variable.
-can change the contents of a variable in a BOOLEAN OPERATOR – OR(||)
later statement.
a b a||b
Identifiers
-sequence of one or more letters, digits, or T T T
underscore characters. T F T
-Spaces, punctuation marks, and symbols F T T
cannot be part of it. F F F
-always begin with a letter.
-case sensistive. Conditional Operator (?)
-evaluates an expression returning a value
Declaration Statement: int x; if that expression is true and a different
Assigment Statement: x = 2; one if the expression is evaluates as false.
Assigment with Expression: x = x + 2; FORMAT:
Display Statement: cout << x; Condition? Result 1: Result 2
T F
Assigment Statement
- “=”-used to assign statement. COMMA OPERATOR (,)
-consist of statement on the rightside and a -used to separate two or more expression.
variable store the result.
Bitwise Operators
COMPOUND ASSIGNMENT -modify variables considering the bit
patterns that represent the values they
value += increase; store.
 value = value + increase;
a -= 5; AND (&) -Bitwise AND
 a = a-5; OR (|) -Bitwise Inclusive OR
a /= b; XOR (^) - Bitwise Exclusive OR
 a = a / b; NOT (~) -Unary Complement (bit
price *= units + 1; inversion)
 price = price * (units + 1); SHL (<<) -Shift LEFT
SHR(>>) -Shirt RIGHT
Relational and Equality Operator
Explicit Type Casting Operator
== -Equal to -provides a method for explicit conversion
!= -Not Equal to of the type of an object in a specific
> -Greater than situation.
< -Less than -can be used as unary expression.
>= -Greater than or equal to
<= -Less than or equal to The “Hello World” Program
Line 1:
LOGICAL OPERATOR -must begin with two slash sign (//) also
! -NOT known as comment.
&& -AND -use to include short explanation or
|| -OR observation about the program.

BOOLEAN OPERATOR – AND(&&) Line 2: #include <iostream>


-begin with the hash sign and directives
a b a&&b read and interprented (pre-processor)
Header iostream
-include a section of standard c++ code. Program FLOWCHART
-allows to perform standard input and -used to describe the steps and process
output operations. using pre-defined symbols.
-represents an algorithm process.
Line 3: Blank Line -show logics of algorithm.
-no effect on the program. -emphsizes individuals steps and their
-improve readability. interconnections.

Line 4: int main() BENEFITS of Program Flowchart


-Initiates the declaration of a function. 1. Help programmers to find the bug in
the process before carrying out.
Lines 5 & 7: { and } 2. Works as a blueprint when analysing
-everything between these braces is the the systems and developing
function’c body that defines what happens programs which makes coding more
in the main. efficient.
3. Improves programmers efficiency in
Line 6: std::cout<<”HELLO WORLD!”; maintaining the operating program.
-c++ statement 4. Communicating the logic of a system
-expression that can actually produce some concerned gets much easier.
effect.
-meat of a program, specifying its actual COMMON FLOWCHART SYMBOL
behaviour.
FLOWLINE
-used to connect symbols and indicate the
Preprocessor directives flow of logic.
-begin by #
-read and processed by the pre-processor TERMINAL
before proper compilation begins. -used to represent the start and end of a
-must be specified in their own line a. task.
-do not end in semicolon (;)
Input & Output
Comments
line comment – discards everything from -used for input and output.
where the pair of slash sign. -for reading and displaying.
//line comment
Block comment- discards everything
between /*, with the possibility of including Processing
multiple lines.
/*block comment*/ -for arithmetic and data-manipulation
operations.
Return Statement -for instruction.
-return 0;
-causes the main function to finish.
-may be followed by a return code. Decision
-program worked as expected without any -for any logic or comparison.
errors during the execution. -has one entry and two exit paths.
-usual way to end a c++ console program. -path chosen depends on the answer if it’s
“yes” or “no”.
Standard Output (cout)
-used in conjuction with the insertion Connector
operator(<<) - to join different flowlines.

Standard Input (cin)


-applying the overloaded operator of Offpage Connector
extraction (>>)
-to indicate that the flowchart continues to -numbered.
a second page. -used to describe the actions a program will
taken when it’s coded in a programming
language.

Predefined process ADVANTAGES OF Pseudocode


-to represent a group of statements that  Improves the readability of any
perform one processing task. approach. It’s one of the best
approaches to start implementation
of an algorithm.
 Acts as a bridge between the
Annotation program and algorithm or flowchart.
-to provide additional info about another  It’s main goal is to explain what
flowchart symbol. exactly each line of a s program
should do, hence making the code
construction phase easier for the
PARTS of a Flowchart programmer.

 Start of a Program
- Marks the beginning of the DISADVANTAGES OF Pseudocode
program.
 Does notv provide a visual
 Statement Block representation of a logic
-statement to execute or behaviour programming.
to perform.  No proper format for writing the
pseudocode.
 Decision Block  There is extra need to maintain
-decision point in program. documentation.
-ask a simple question, and do  There is no proper standard.
different things depending on the
answer. WRITING a Pseudocode
Step 1:
 Yes/No (True or False, etc.) Arrange the sequence of tasks and
-Answers to the question posed in write the pseudocode accordingly.
the decision block.
Step 2:
 End Program Start with the statement of a
-Marks the end of the program. pseudocode which establishes the
main goal or aim.
GUIDELINES for developing Flowchart
 Can only have one start and stop Pseudocode COMPONENTS
symbol.
 On-page connectors are referenced  Repitition
using numbers.  Variables
 Off-page connector are referenced  Input-Output
using alphabets.  Selection
 General flow of processes is stop to  Assignment
bottom or left to right.
 Arrows should not cross each other.

6 Specific Structured Programming


Pseudocode Contructs
-set of sequential written human language  Sequence
instructions.
-Linear progression where one -used when some particular task is to be
task is performed sequentially repeated for a number of times according
after another. to the specified condition.
-looping is used to avoid writing the same
 While set of instructions again.
-a loop(repetition) with a simple -one time or multiple times until the
conditional test as its beginning. desired output was obtained.

 If-Then-Else  While looping


-a decision(selection) in which a  Repeat-Until
choice is made between two  For loop
alternative sources of action.
Linear Sequence of Statements
 Repeat-Until -each individuals instructions of a program.
-a loop with a simple conditional -end in semicolon.
test at the bottom. -executed in the same order in which they
appear in a program.
 For  Variable declarations: int a, b, c;
-“counting” loop.  Expressions: N = 2*(N+1)

 Case Flow control statement


-multiway branch (decision) -compound statement
based on the value of an -group of statements
expression; generalization of IF- -each of them was terminated by its own
THEN-ELSE. semicolon.
Sequence -all grouped together in a block.
-instructions to be computed simply follow -enclosed in curly braces
one another in a logical progression.  {statement1; statement2;
-indicates a processing group in a program. statement3;}
 Sequence Construct
-linear progression where one Iteration Statement
task is performed sequentially -set of statements to be executed
after another. repeatedly.
-as long as the condition evaluates to True,
 Sequential Control the set of statement(s) is executed.
-indicated by writing one action
after another, each action on a C++ Loops
line by itself and all actions -sequence of instructions that is repeated
aligned with the logical indent. until a certain condition is reached.

Checking of Counter:
Selection(Decision)  Counter Not Reached: counter
-decision-making structure has not reached the desired
-process of deciding which choice is made numbers, the next instruction in
between two or more alternative courses of the sequence returns to the first
action. instruction in the sequence and
 If-then-else repeat it.
 Case Construct  Counter Reached: If the
-indicates a multiway branch condition has been reached, the
based on the conditions that are next instruction “falls through” to
mutually exclusive(Case, Of, the next sequential instruction or
Others, and Endcase) branches outside the loop.

Iteration or Repetition(Looping)

2 categories of loops:
-three fields in a for-loop are optional.
 Entry Controlled Loops -can be left empty, but in all cases the
-The test condition is at the top. semicolon between of them are required.
-While loop, for loop.
Example :
 Exit Controlled Loop For(;n<10;)
-test condition is at the bottom. --without initialization or increase (while-
loop)
3 statements of Loop:
 Initialization expression For(;n<10;++n)
 Test expression --with increase, but no initialization (maybe
 Update expression because the variable was already initialized
before the loop).
While loop
Syntax: -No condition (INFINITE LOOP)
initialization expression; --loop with true as condition.
while(test expression)
{ Infinite Loop
//statements -if the condition is always TRUE—infinite
update_expression; times.
} Syntax:
For Loop //infinite while loop
Syntax:
for(initialization expression;condition; While(true){
update_expression){
//body of loop //body of the loop
}
}
Initialization
-initializes variables and is executed only IMPORTANT POINTS
once.  Use for loop
Condition -when number of iteration is known
-if true—executed. beforehand.
-if false—terminated.
Update  Use while loops
-updates the value of initialized variables -where exact number of iterations is
and again checks the condition. not known but the loop termination
condition is known.
PROCESS-for Loop
 Use do while loop
1. Initialization is executed.(only at the -if the code needs to be executed at
beginning of the loop) least once like MENU driven
2. Condition is checked.(if true— programs.
continues. If false—end. Will be
skipped and jump to step 5) Jump Statement
3. Statement is executed.(single -allow altering the flow of a program by
statement or enclosed in curly performing jumps to specific locations.
braces) -used to alter the flow of control
4. Increase is executed, and the loop unconditionally.
gets back to step 2. -Transfer the program control within a
5. The loop execution continues by the function unconditionally.
next statement after it. -used standard library function exit()
To jump out the entire program.

TIPS-for Loop
Jump Statement  BREAK STATEMENT
 Break -used when we are not sure about
-used to terminate the loop. the actual number of iterations for
the loop or we want to terminate the
 Continue loop base on the some condition.
-used to execute other parts of the
loop while skipping some parts  CONTINUE STATEMENT
declared inside the condition, rather -used to skip the current iteration of
that terminating the loop, it the loop and the control of the
continues to execute the next program goes to the next iteration.
iteration of the same loop.
 GOTO STATEMENT
 Goto -jump statement and sometimes
-unconditional jump. referred to unconditional jump
-used to jump anywhere within a statement.
function.
- used to jump anywhere within a
 Return function.
-stronger than break.
Used to terminate the entire function  EXIT()
after the execution of the function of -terminates the calling process
after some condition. without executing the rest code
which is after exit() function.
Exit() statement
Syntax: exit();  RETURN STATEMENTS
-returns the flow of execution to the
-standard lib function that terminate the function from where it’s called.
entire program immediately and passes the
control to the operating system.

-takes a single parameter and exit status of


the program and returns the same status
to the opening system upon termination.

-status can be either a zero(successful


termination) or non-zero(unsuccessful
termination) value.

Return Statement
Syntax: return expression(opt);

-statement terminates execution of a


function and returns control to the calling
function, with or without a return value.

-function may contain any number of


return statements.

-if it is with an expression, it cannot appear


in a function whose return type is void.

IMPORTANT POINTS

You might also like