Variables & Operators in C++ Programming
Variables & Operators in C++ Programming
BSE 1
Joddat Fatima
1
[email protected]
Department of C&SE
Bahria University Islamabad
VALUES & VARIABLES
2
VALUE
3
VARIABLES
5
TYPES OF VARIABLES
Variable type Keyword used in Size in bits
declaration
6
Text type data char 8 bits
TYPES OF VARIABLES
To use a variable in our code
First, we must have to declare it, variable are known with its
keywords. “Use the name of keyword” for declaration of a variable”.
Example: int number1 = 10;
Example: float floatData = 20.93;
Example: signed int myData = -10;
Example: char textData = ‘A’;
#include <stdio.h>
#include <stdio.h> void main (void)
void main (void) {
{ int number1 = 20, number2 = 10;
int number1 = 10; }
float floatData = 20.93;
7
}
MANIPULATING VARIABLES
Assignment Statement
In Mathematics the value x = x + 1 is not possible why?
In C++ x = x +1 is possible because “=” is an assignment operator
and not an equality operator.
Assignment operator means that the contents of the right hand side
is transferred to the memory location of the left hand side.
8
CONSTANTS
Constants are values which cannot be modified e.g. the value
of Pi
To declare a constant in C++, we write a keyword “const”
before the variable type.
const double pi = 3.14;
RESERVE WORDS
Some names cannot be declared as variable names because
they are reserved words in C++
9
TYPE CHAR
Computers process character data too char
Shortfor character
Can be any single character from the keyboard
char constants
Character constants are enclosed in single quotes
char letter = 'a';
Strings of characters, even if only one character is enclosed in
double quotes
"a" is a string of characters containing one character
'a' is a value of type character
10
TYPE BOOLEAN
11
CASTING
static_cast<double> (intVar)
int a; int a, b, c;
int a;
float mynumber; int b;
int c;
unsigned NextYear; short Year;
unsigned int NextYear; short int Year;
13
VARIABLE CODE EXAMPLE
15
STRING EXAMPLE
16
CHARACTER AND STRING LITERALS
18
LIST OF ESCAPE CODES
\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\a alert (beep)
20
OPERATOR PRECEDENCE AND ASSOCIATIVITY
21
MANIPULATING VALUES
Mathematical Operators
• Common mathematical operators are available in C++ for
manipulating values e.g. addition(+), subtraction(-),
multiplication(*), division(/), and modulus (%).
C++ has many other operators also which we will study in
due course.
ARITHMETIC EXPRESSIONS
Operator Precedence
Operator precedence controls the order in which operations are
performed
Operator Associativity
22
The associativity of an operator specifies the order in which
operations of the same precedence are performed
Assignment (=)
a = 5;
a = b;
Arithmetic operators ( +, -, *, /, % )
The five arithmetical operations supported by the C++ language are:
+ addition
- subtraction
* multiplication
/ division
% modulo
23
Compound assignment (+=, -=, *=, /=, %=, >>=, <<=,
&=,^=, |=)
24
Relational and equality operators ( ==, !=, >, <, >=, <= )
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
25
Comma operator ( , )
The comma operator (,) is used to separate two or more expressions
that are included where only one expression is expected. For
example, the following code:
a = (b=3, b+2);
Conditional operator ( ? )
The conditional operator evaluates an expression returning a value if
that expression is true and a different one if the expression is
evaluated as false. Its format is:
condition ? result1 : result2
27
EXAMPLES OF FLOW CHARTS
28
COMPARISON OF ALGORITHM, FLOWCHART
AND PSEUDO-CODE
29
FLOW CHART PRACTICE SESSION
30
CALCULATE AND PRINT THE AVERAGE GRADE
OF 3 TESTS FOR THE ENTIRE CLASS
Input
3 test scores for each student
Output
Average of 3 tests for each student
Process
1. Get three scores
2. Add them together
3. Divide by three to get the average
4. Print the average
5. Repeat step 1 to 4 for next student
6. Stop if there are no more students
31
31
ALGORITHM ATM FOR WITHDRAWAL
Output
Money, error messages
Inputs
User Identification (ATM card), password, amount
Process
1. Get the ATM card for identification and ask for password
2. Check password
3. If password is not valid, generate an error message and go to step number 8.
4. Get the amount from the user
5. Check the current balance
6. If amount is greater than current balance, generate an error message and go to
step number 8.
7. Subtract the amount from the balance and give out the cash.
8. Return the ATM card
9. Stop
32