You are on page 1of 16

Prep by: Engr. Feviclene L.

Villamor
OPERATION ON NUMBER
10
Binary Addition + 1
------
11

1 1 1 1 1
1 1 1 1 0 1
+ 1 0 1 1 1
1 0 1 0 1 0 0
64 32 16 8 4 2 1
64 + 16 + 4 = 84
OPERATION ON NUMBER
10
Binary Subtraction - 1
------
1

1
0 10 10
1 0 0 1 1 1
- 1 1 1 0 1
0 0 1 0 1 0
Binary Multiplication

1 0 1 1 1
x 1 0 1 0
-------------------------------------------
0 0 0 0 0
1 0 1 1 1
0 0 0 0 0
1 0 1 1 1
1 1 1 0 0 1 1 0
128 64 32 16 8 4 2 1
128 + 64 + 32 + 4 + 2 = 230
Binary Division
11 1
110 101010
110
10 01
110
01 10
110
0
SIMPLE CONTROL STRUCTURES
A program is usually not limited to a linear sequence of
instructions. During its process it may bifurcate, repeat code
or take decisions. C++ provides control structures that serve
to specify what has to be done by our program, when and
under which circumstances.

With the introduction of control structures we are going to


have to introduce a new concept: the compound-statement or
block.
A block is a group of statements which are separated by
semicolons (;) like all C++ statements, but grouped together
in a block enclosed in curly brackets: { }:
{ statement1; statement2; statement3; }
Conditional structure: if and else
if(condition)statement
Where condition is the expression that is being evaluated. If this
condition is true, statement is executed. If it is false, statement is
ignored (not executed) and the program continues right after this
conditional structure. For example, the following code fragment prints
x is 100 only if the value stored in the x variable is indeed 100:
Conditional structure: if and else
if (condition) statement1 else statement2
Decision Making in C++
Flowchart if statements
int main()
{
int i = 10;
if (i > 5)
{
sum=3+i
cout << "sum of 3 and i is "<<sum;
}
}
Output:
sum of 3 and i is 13
Flowchart if else statements
int main()
{
int i = 10;
if (i > 5)
{
sum=3+i
cout << "sum of 3 and i is "<<sum;
}
else
cout<<"Error";
}
Output:
sum of 3 and i is 13
Output “sum of
3 and I is” sum
Flowchart if-else if statements

Shown in previous lesson


Variables & Data Types
All variables use data-type during declaration to restrict the type of
data to be stored. Therefore, we can say that data types are used to
tell the variables the type of data it can store.
Primitive Data Types: These data types are
built-in or predefined data types and can be used
directly by the user to declare variables. example:
int, char , float, bool etc. Primitive data types
available in C++ are:
o Integer
o Character
o Boolean
o Floating Point
o Double Floating Point
o Valueless or Void
Integer: When you need to store a whole number without decimals.Keyword used for
integer data types is int.

Character: Character data type is used for storing characters. Keyword used for
character data type is char.

Boolean: Boolean data type is used for storing boolean or logical values. A boolean
variable can store either true or false. Keyword used for boolean data type is bool.

Floating Point: Floating Point data type is used for storing single precision floating
point values or decimal values. Keyword used for floating point data type is float.

Double Floating Point: Double Floating Point data type is used for storing double
precision floating point values or decimal values. Keyword used for double floating
point data type is double.

void: Void means without any value. void data type represents a valueless entity.
Void data type is used for those function which does not returns a value. void.
String Types
The string type is used to store a sequence of characters (text). This is
not a built-in type, but it behaves like one in its most basic usage.
String values must be surrounded by double quotes:

Example
string greeting = "Hello";
cout << greeting;

To use strings, you must include an additional header file in the source
code, the <string> library:

Example

#include <string>
string greeting = "Hello";
cout << greeting;

You might also like