Datalgo Lesson 2
Datalgo Lesson 2
Lesson 2:
Expressions and Declaring & Intializing Variables
Lecturer:
Ms. Jochelle P. Plazo
TOPIC
1. Expressions
2. Declaring & Initializing Variables
3. Allocating Memory
4. Assigment Statement
5. Input and Output Statement
BOARD
Topic 1: Expressions
Examples:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
BOARD
Topic 2: Declaring & Initializing Variables
Example:
Consider the following statements:
double amountDue;
int counter;
char ch;
int x,y;
String name;
BOARD
Topic 3: Allocating Memory with Constants and Variables
Example:
Consider the following C++ statements:
const double CONVERSION = 2.54;
const int NO_OF_STUDENTS = 2.0;
const char BLANK = ‘ ‘;
const double PAY_RATE = 15.75;
BOARD
Topic 4: Assignment Statement
Example 1:
int num1,num2;
• The assignment statement takes the double sale;
char first;
form: string str;
variable = expression; num1 =4;
num2 =4*5-11;
sale – 0.02 * 1000;
first = ‘D’;
• Expression is evaluated and its value is str = “ It is a sunny
assigned to the variable on the left side day.”;
num1 = 18;
num1 = num1 + 27;
num2 = num1;
num3 = num2 / 5;
num3 = num3 / 4;
BOARD
Topic 4: Saving and Using the Value of an Expression
\n newline
\t Tab
\b Backspace
\r Return
\\ Backslash
\’ Single Quotation
\” Double Quotation
BOARD
Topic 6: Preprocessor Directives
Example:
#include <iostream>
BOARD
Topic 6: Preprocessor Directives
• To use the string type, you
• Syntax to include a header file: need to access its definition
#include <headerFileName> from the header file string
#include <string>
Example:
#include <iostream>
• C++ program has two parts
• Cin and cout are declared in the header file - Preprocessor directives
iostream, but within stdnamespace. - The Program
#include <iostream>
Using namespace std;
BOARD
Topic 6: Creating a C++ Program
• A C++ program is a collection of functions, one
of which is the function main
int main()
Reference
• www.slideshare.net/vigneras/data-structures-and-algo
rithms
BOARD
Next Session
1. Documentation
2. Arguments and Parameters
3. Function
4. Function Prototypes