Variables Types Operators
Variables Types Operators
Try this on your own Also try giving a character instead of a number
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
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
Try this in C++ using int variables A = 5 B = 2 C = A / B ? What data type is suitable for this application?
10
11
Examples unsigned short int number_of_people; signed int my_mccount_balance; //is this correct? short int Year;
12
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;
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
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++
19
Arithmetic operators
+ * / %
20
Compound Assignment
Using these you can reduce length of codes Used to modify value of a variable expression is equivalent to
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
>=
<=
>=
<=
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
A && B
Expression is True iff A and B are both true
T F
T T F
F F F
26
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
!(A==B)
If (A==B) = True
28
( (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
& ^
| && || ?: = *= /= %= += -= >>= <<= &= ^= |= ,
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