0% found this document useful (0 votes)
48 views

Variables Types Operators

The document discusses variables, types, and operators in C++. It covers declaring variables and data types, initializing variables, variable scope, arithmetic operators, relational operators, logical operators, and operator precedence. Examples are provided for declaring integer, character, and string variables and using various operators on variables.

Uploaded by

ajithkk199
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Variables Types Operators

The document discusses variables, types, and operators in C++. It covers declaring variables and data types, initializing variables, variable scope, arithmetic operators, relational operators, logical operators, and operator precedence. Examples are provided for declaring integer, character, and string variables and using various operators on variables.

Uploaded by

ajithkk199
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Variables, Types & Operators

Object-Oriented Programming in C++ (ICTCO10306)

Simple C++ Program


#include <iostream.h> using namespace std; // int main() { // Declarations // Statements return 0; }

From last week

Sample C++ Program


#include <iostream.h> using namespace std; void main() { int number; cout << Enter a number << endl; cin >> number; cout << You entered: << number << endl; }

Try this on your own Also try giving a character instead of a number

Did you try this?

Problem: To determine the average of three numbers Task: Request, from the user, three numbers, compute the average of the three numbers, and print out the original values and the computed average

Declaring a Variable

Variable provides you a named storage in Computer Memory Declaring a variable int number;

Type

Identifier (This is the name we give to The variable) 5

Identifiers

Valid Identifiers sequence of one or more letters, digits or underscore characters (_) Neither spaces nor punctuation marks or symbols can be part of an identifier Are there invalid identifiers?
6

Identifiers

How an identifier become invalid? begin with a digit (number) or symbol Try this on previous example reserved words under some circumstances and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq keyword of the C++ language
7

Reserved words

You cannot use any of these as an identifier asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while 8

Identifiers

Identifiers are case sensitive Example Int year; Int Year; // these are identified as two different variables by C++ compiler Always try to give meaningful phrases as identifiers

Data type for the variable

Try this in C++ using int variables A = 5 B = 2 C = A / B ? What data type is suitable for this application?

10

Fundamental Data Types in C++

Try again with the suitable data type


Source http://www. cplusplus.com

11

Signed (+/-) Vs. Unsigned

Examples unsigned short int number_of_people; signed int my_mccount_balance; //is this correct? short int Year;

12

String of Characters as a single variable

What is a string Set of characters placed in order A special type of variable to store a complete string of characteds Cannot be handled by Iostream.h You need <string.h> 13

Initializing a Variable

Why Initialize? There can be stray values / residuals from previous programs Example: int a = 0;

It is a good programming practice to intialize a variable as soon as you declare it 14

Initializing a Variable

How do you initialize a variable? Int a = 5; Int b(5); Both methods are valid in c++ Variables a and b will be initialized to value 5

15

Where do you declare a variable?


1.

Definitely before you use them 1. Within the main function 2. Before the main function Example:
#include <iostream.h>
short unsigned int year = 2013; int main() { short unsigned int month;

month = 11;
cout<<"Current year = "<<year<<endl; cout<<"Current month = "<<month<<endl; return 0; }

16

Scope of a variable

short unsigned int year = 2013; This is a Global Variable (Visible everywhere in the program) short unsigned int month; This is a local variable (Can use only in main function)

17

Example:
#include <iostream.h> Using namespace std; short unsigned int year = 2013;//Global variable int main() { short unsigned int month;//Local variable month = 11; cout<<"Current year = "<<year<<endl; cout<<"Current month = "<<month<<endl; return 0; } 18

Operators in C++

Name the operators you already know


Example int a,b; a = 2 + (b = 5); a = b = 5;

19

Arithmetic operators

+ * / %

Addition Subtraction Multiplication Division Modulo division (Remainder of the division)

20

Compound Assignment

Using these you can reduce length of codes Used to modify value of a variable expression is equivalent to

value += increase; a -= 5; a /= b; price *= units + 1;

value = value + increase; a = a - 5; a = a / b; price = price * (units + 1);


21

Increase and decrease (++, --)


Also called increment and decrement operators Example C++; ++C; // These are different operations Example 2 B=3; A=B++; // A contains 3, B contains 4 22

Example 1 B=3; A=++B; // A contains 4, B contains 4

Relational and equality operators

Comparison between values or variables


== != > < Equal to Not equal to Greater than Less than

>=
<=

Greater than or equal to


Less than or equal to

Do not confuse between == and = operators Evaluate the expression: ((b=2) == a)


23

Relational and equality operators

Comparison between values or variables


== != > < Equal to Not equal to Greater than Less than

>=
<=

Greater than or equal to


Less than or equal to

Do not confuse between == and = operators Evaluate the expression: ((b=2) == a)


24

Comma Operator

A = (B=3, B+2); 1. Will assign B=3; 2. Then evaluate a = B+2 This is equivalent to B=3; A=B+2;

25

Logical Operations: And(&&)


A && B
Expression is True iff A and B are both true

T F

T T F

F F F

26

Logical Operations: Or(||)


A || B
Expression is True if either A or B are True Note: Also True when A and B are both True

T F

T T T

F T F

27

Logical Operations: not(!)

!(A==B)
If (A==B) = True

What is the value of !(A==B)

28

Logical Operations: Exercises


A = 1, B = 1, C = 0 1. A || B 2. A && C 3. A || B && C
4.

( (5 == 5) && (3 > 6) )
29

Precedence of operators
Level Operator 1 :: 2 () [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid ++ -- ~ ! sizeof new delete 3 *& +Description scope postfix unary (prefix) Grouping Left-to-right Left-to-right

4
5 6 7 8

(type)
.* ->* */% +<< >>

indirection and reference (pointers) unary sign operator type casting pointer-tomember multiplicative additive shift

Right-to-left

Right-to-left
Left-to-right Left-to-right Left-to-right Left-to-right

30

Precedence of operators
9 10 < > <= >= == != relational equality Left-to-right Left-to-right

11 12
13 14 15 16 17 18

& ^
| && || ?: = *= /= %= += -= >>= <<= &= ^= |= ,

bitwise AND bitwise XOR


bitwise OR logical AND logical OR conditional assignment comma

Left-to-right Left-to-right
Left-to-right Left-to-right Left-to-right Right-to-left Right-to-left Left-to-right

31

You might also like