You are on page 1of 41

PROGRAMMING

LEARNING YOUR LAGUAGE


PROGRAM

A SET OF INSTRUCTIONS GIVEN BY THE


PROGRAMMER THAT THE COMPUTER WILL
PERFORM
Learn your jargons
Variable

A memory location which can store specific value for


use by the programs.
A variable consists of an identifier (name) and a data
type
Two types of variable
user-defined
constant has a fixed value through-out program run
Data type

A data type defines the kind of data which a data


storage may contain. It is a format for memory
location that can hold a specific type or range of
values
Types of data type (commonly used)
int
- Represents whole number value
float
- Represents numbers with floating point
char
- Represents a character
bool
- Represents Boolean values
double
- Represents numerical values but consumes twice space as int
Literal

A notation for representing a fixed value in source code


Examples:
numeric literal (1, 0, 10)
character literal (a,b,c)
string literal (hello, cake)
Boolean values (True, False)
Reserved words

Programming lines or words that has functionalities in the


language that cannot be used as a variable
Delimiter

Has a corresponding character in any programming language,


its presence indicates the closing of a sentence
STRUCTURE OF A PROGRAM

Varies on used programming language but generally


it should contain the following:
Declaration
Statements
Comments (optional)
Functions
How program run works

Lexical Breaking of objects in a program


into lexical values

Syntax Grammar checking process

Semantic Logical checking process


C++
PROGRAMMING
Libraries

Contains compiler-defined functions that are necessary for programming


reserved words to work
Example:
iostream.h
string.h
math.h
Syntax
#include <iostream>
Input/Output

Input (cin)
- Used to accept value in a program
- Syntax:
- cin>> <variable>;

Output (cout)
- Used to display a value or string/s in a program
- Syntax:
- cout<< <variable>/<string>;
Comments
Commonly used for notation of what your program does for easy understanding
Rule:
All statements with comment sign or under comment signs will not be read on
program run
Syntax
One liner comment
// code goes here
Block comment
/* code goes here
other codes
*/
Declaration
The process of identifying variables to be used in the program
Two types of declaration:
Global declaration
Syntax
<datatype><identifier>;
main(){
}
Local declaration
Syntax
main(){
<datatype><identifier>;
}
Assignment

The process of assigning a value to a variable


Syntax:
<variable> = <literal>;
Operators

Arithmetic
Addition the process of combining two or more values
Syntax: var1 + var2;
Subtraction the process of removing value/s or object from a
collection
Syntax: var2-var1;
Multiplication the process of reproducing a value
Syntax: var1*var2;
Division the process of allocating values to a number of value
Syntax: var1/var2; (division must not be zero-divide)
Operators

Arithmetic
Increment Adds a value to an existing value
Syntax: var1++;
Decrement Subtracts a value to an existing value
Syntax: var2--;
Operators

Relational Returns Boolean value


Less than Checks if the value of first element is LT than second element
Syntax: var1 < var2;
Greater than Checks if the value of first element is GT than second element
Syntax: var2 > var1;
Less than or Equal to Checks if the value of first element is LT or EQ than second
element
Syntax: var1 <= var2;
Greater than or Equal to Checks if the value of first element is GT or EQ than second
element
Syntax: var1 >= var2;
Operators

Relational Returns Boolean value


Equal Checks if both elements are equal
Syntax: var1 = var2;
Not Equal Checks if both elements are not equal
Syntax: var1 != var2;
Operators

Logical Returns Boolean value


AND Returns 0 if one of the two values compared is 0
Syntax: var1 && var2;
OR Returns 0 if one of the two values compared is 1
Syntax: var2 || var1;
NOT Negates value
Syntax: !(var1&&var2);
Operators

TRUTH TABLE

First Second NOT


AND OR
Element Element (on first elem)

0 0 0 0 1

0 1 0 1 1

1 0 0 1 0

1 1 1 1 0
Conditional Statements

Used for cases that you need to execute a block of codes depending on the
situation
Types of conditional statements:
If
If-else
switch
IF statement

An if statement consists of a Boolean expression followed by one or more


statements.
Syntax:
if (<condition>) {
// statement(s) will execute if the condition is true
}

<condition> : <var><relop><var>/
IF ELSE statement
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false.
Syntax:
if(<condition>) {
// statement(s) will execute if the condition is true
}
else {
// statement(s) will execute if the condition is false
}
SWITCH Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is
called a case, and the variable being switched on is checked for each case.
Syntax:
switch(expression){
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
// you can have any number of case statements.
default : //Optional
statement(s);
}
Looping

Used for cases that you need to execute a block of codes n times
Types of loops:
For
While
Do-while
FOR Loop

A for loop is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.
Syntax:
for (initialization; condition; increment ) {
statement(s);
}
WHILE Loop

A while loop statement repeatedly executes a target statement as long as a


given condition is true.
Syntax:
while(condition){
statement(s);
}
DO-WHILE Loop

Unlike for and while loops, which test the loop condition at the top of the loop,
the do-while loop checks its condition at the bottom of the loop. A do-while loop
is similar to a while loop, except that a do-while loop is guaranteed to execute at
least one time.
Syntax:
do {
statement(s);
}while(condition );
Function

Named section of a program that performs a specific task. In this sense, a


function is a type of procedure or routine. Syntax:
Syntax:
<function-name>( ){
//code goes here
}
Array

A collection of data with the same data type


Syntax:
- <datatype> <var>[<num>/null];
- <datatype> <var>[<num>/null] = {<lit1>,<lit2>,..<litn>};
String Functions

Used for string manipulation


Uses <cstring> library
Common functions:
- strcpy
- strlen
- strncat
String Functions

STRCPY copies a string


Syntax:
strcpy (<var2>,<var1>/<string lit>);
Where:
var2 copies the value of var1 or string lit
String Functions
STRNCAT concatenates two or more string
Syntax:
strncat (<var1>,<var2>/<string lit>,<num>);
Where:
var1 will be the main holder of the concatenated string
var2 contains string literal
num valid whole number, basis of length of string to be concatenated to
original string
String Functions

STRLEN counts and gets the length of a string


Syntax:
strlen (<var1>/<string lit>);
Numeric Functions

Square root finding the square of a number


Syntax:
sqrt(var1);
Numeric Functions

Power raises a value by itself with number of times


Syntax:
pow(<var1>,);
Numeric Functions

Radians
Sin
Cos
Tan
Syntax:
sin(<var>/<literal>);
cos(<var>/<literal>);
tan(<var>/<literal>);
* Value representing an angle, expressed in radians.
One radian is equivalent to 180/PI degrees.
FIN

You might also like