Week03Varialbe Data Types, Operators
Week03Varialbe Data Types, Operators
CS 131
PROGRAMMING I
2023-2024
First Program in C++:
Printing a Line of Text
• Run-time
• E.g. division by zero
• Logical
• E.g. write addition operator instead of multiplication
Variable, Data types
and operators
1. Data_Type Identifier;
int width; // width of rectangle
float area; // result of calculating area stored in it
char separator; // word separator
2. Data_Type Identifier = Initial_Value;
int width = 10; // width of rectangle
float area = 255; // result of calculating area stored in it
char seperator = ‘,’; // word separator
3. Data_Type Identifier, Identifier, Identifier ,….;
int width, length, temporary;
float radius, area = 0;
A scope is a region of the program and broadly
speaking there are three places, where variables can be
declared :
Inside a function or a block which is called local
variables,
In the definition of function parameters which is
called formal parameters.
Outside of all functions which is called global
variables.
We will learn what is a function and it's parameter in
subsequent lectures. Here let us explain what are local
and global variables.
Type Keyword
Boolean bool
Character char The size and range of
Integer int these data types may
vary among processor
Floating point float
types and compilers
Double floating point double
Valueless void
unsigned: 5000u 4U
long: 123456789l 56L
unsigned long: 536489ul
long long : 5361254865LL 25lL
Example :
0xABu 0123uL 017LL
A floating-point value contains a decimal point
33.5 0.0 -657.983 .2 6.
String
cout<<"string is array of char!!!";
cout<<“example of escape sequence is \n";
Constants provide a way to define a variable
which cannot be modified by any other part in
the code
#define: without memory consume
const: memory consume
2+3*5 ???
Is it 25 or 17
+= c += 7; c = c + 7; c = 16
-= c -= 8; c = c – 8; c=1
Example :
c = 9; *= c *= 10; c = c * 10; c = 90
/= c /= 5; c = c / 5; c=1
%= c %= 5; c = c % 5; c=4
&= c &= 2 ; c = c & 2; c=0
^= c ^= 2; c = c ^ 2; c = 11
|= c |= 2; c = c | 2; c = 11
<<= c <<= 2; c = c << 2; c = 36
>>= c >>= 2; c = c >> 2; c=2
The if statement allows a program to take alternative
action based on whether a condition is true or false.
If the condition is true, the statement in the body of the if
statement is executed.
If the condition is false, the body statement is not executed.
Conditions in if statements can be formed by using the
equality operators and relational operators summarized in
Fig. 2.12.
The relational operators all have the same level of
precedence and associate left to right.
The equality operators both have the same level of
precedence, which is lower than that of the relational
operators, and associate left to right.
◦ This performance feature for the evaluation of logical AND and logical .
All
OR expressions is called short-circuit evaluation. Ri
gh
ts
Re
se
rv
ed
.
There are following logical operators supported by C++
language.
Assume variable A holds 1 and variable B holds 0, then −