0% found this document useful (0 votes)
33 views19 pages

Datalgo Lesson 2

The document discusses topics related to expressions, variables, memory allocation, input/output statements, and preprocessor directives in C++. It provides examples and explanations of declaring and initializing variables, assignment statements, and using cout and cin for output and input. Key terms discussed include data types, constants, operators, and header files.

Uploaded by

mama.mo.patatas7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views19 pages

Datalgo Lesson 2

The document discusses topics related to expressions, variables, memory allocation, input/output statements, and preprocessor directives in C++. It provides examples and explanations of declaring and initializing variables, assignment statements, and using cout and cin for output and input. Key terms discussed include data types, constants, operators, and header files.

Uploaded by

mama.mo.patatas7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

BOARD

DATA STRUCTURES AND


ALGORITHM
(DATALGO)

Lesson 2:
Expressions and Declaring & Intializing Variables

Lecturer:
Ms. Jochelle P. Plazo

ACCESS COMPUTER COLLEGE


BOARD

TOPIC

1. Expressions
2. Declaring & Initializing Variables
3. Allocating Memory
4. Assigment Statement
5. Input and Output Statement
BOARD
Topic 1: Expressions

• If all operands are integers


- Expression is called an integral expression
- Yields an integral result
- Example: 2 + 3 * 5

• If all operands are floating-point


- Expressions is called a floating point
expressions
- Yields a floating point result
- Example: 12.8 * 17.5 – 34.50
BOARD
Topic 1: Mixed Expressions
• Mixed Expressions:
- Has operands of different data types
- Contains integers and floating point

Examples:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
BOARD
Topic 2: Declaring & Initializing Variables

• Variables can be initialized when


declared:
int first =13, second = 10;
char ch=‘ ‘;
double x = 12.6;
BOARD
Topic 3: Allocating Memory with Constants and Variables

• Variable: memory location whose content may


change during execution
• The syntax to declare a named constant is:
dataType identifier, identifier, . . . . ;

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

• Named constant: memory locating whose content


can’t change during execution
• The syntax to declare a named constant is :
const dataType identifier = value;
• In C++, const is a reserved word.

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.”;

• In C++, = is called assignment operator Example 2:

num1 = 18;
num1 = num1 + 27;
num2 = num1;
num3 = num2 / 5;
num3 = num3 / 4;
BOARD
Topic 4: Saving and Using the Value of an Expression

• To save the value of an expression:


Declare a variable of the appropriate data
type
Assign the value of the expressions to the
variable that was declared
• Whenever the value of the expressions is
needed, use the variable holding the
value
BOARD
Topic 5: Input (Read) Statement

✔ cin is used with >> to gather input


cin >> variable >> variable
• The Stream extraction operation is
>>cin >>miles;
- Causes computer to get a value of type
double
- Places it in the variable miles
BOARD
Topic 5: Increment & Decrement Operators
• Increment operator: Increment variable by 1
Pre-increment: ++variable
Post-increment: variable++
• Decrement operator: decrement variable by 1
Pre-decrement: --variable
Post-decrement: variable--
• What is the difference between the following?
x = 5; x = 5;
y= ++x y = x++
BOARD
Topic 2: Output cout<<29/4<<endl;
cout<<“Hello there.”<<endl;
• The syntax of cout and << is:
cout<<12<<endl;
cout<< expression or manipulator << expression or
manipulator ….; Output:
7
Calles an output statement Hello there
12
• The stream insertion operator is <<
• Expression evaluated and its value is printed at the cout << “Hello there.”;
current cursor position on the screen cout << “My name is James.”;
• A manipulator is used to format the output
Output:
Example: endl causes insertion point to move to Hello there. My name is James.
beginning of nextline
cout << “Hello there.\n”;
• The new line character is ‘\n’ cout << “My name is James.”;
- May appear anywhere in the string
Output:
Hello there.
My name is James.
BOARD
Topic 1:Reserved Codes

\n newline
\t Tab
\b Backspace
\r Return
\\ Backslash
\’ Single Quotation
\” Double Quotation
BOARD
Topic 6: Preprocessor Directives

• Syntax to include a header file:


#include <headerFileName>

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()

• The statements enclosed between the curly


braces ({ and }) form the body of the function
- Declaration statements
- Executable statements
BOARD
Topic 6: Try this Activity
Copy and try this sample on your own computers.
#include <iostream> //Line 1
using namespace std; //Line 2
const int NUMBER=12; //Line 3
Int main() //Line 4
{ //Line 5
int firstnum; //Line 6
int secondNum; //Line 7
firstNum=18; //Line 8
cout<<“Line 9: firstNum=“<<firstNum<<endl; //Line 9 Sample Run:
cout<<Line 10: Enter an integer: “; //Line 10
Line 9: firstNum = 18
cin>>secondNum; //Line 11
Line 10: Enter an integer: 15
cout<<endl; //Line 12
cout<<Line 13: secondNum=“<<secondNum<<endl; //Line 13
firstNum=firstNum+NUMBER+2 * secondNum; //Line 14 Line 13: secondNum = 15
cout<<Line 15: The new value of “<< “firstNum=”<<firstNum<<endl; //Line 15 Line 15: The new value of firstNum=60
Return 0; //Line 16
} //Line 17
BOARD

Reference
• www.slideshare.net/vigneras/data-structures-and-algo
rithms
BOARD

Next Session

1. Documentation
2. Arguments and Parameters
3. Function
4. Function Prototypes

THANK YOU FOR WATCHING

You might also like