Академический Документы
Профессиональный Документы
Культура Документы
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
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
TRUTH TABLE
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
<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
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
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