Professional Documents
Culture Documents
Introduction to OPERANDS
C/C++
DECISION MAKING
Variables • Declaration and
&
Constants definitions
Topics Operators
Decision • If/switch
making
Variables
Definition
Variables are defined with the type followed by the list of variable
Type tells the compiler how much memory to allocate
type variable_list;
Examples: int i, j, k;
char c, ch;
float f, salary;
double d;
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
Literals
Integer
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
Literals
Floating point
3.14159f /* Legal float*/
3.14159 /* Legal double*/
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
Literals
Strings
"hello, dear"
"hello, \
dear"
"hello, "
"d"
"ear"
Constants
Precompiler
Constants replaced by the precompiler
Defined using #define
#define identifier value
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n’
int main() {
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
Constants
In memory
Constants in memory are typed as variables
const type variable = value;
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n’;
Static
◦ Default for global variables
◦ Allocation in data memory at compile time
Extern
◦ Variable is defined in another file (global variables)
Register
◦ Variable should be placed in register (max size is the size of the register)
Volatile
◦ Reload from memory
Variables • Declaration and
&
Constants definitions
Topics Operators
Decision • If/switch
making
Operators
Arithmetic
Operat Description Example
or
== Checks if the values of two operands are equal or not. If yes, then (A == B)
the condition becomes true.
!= Checks if the values of two operands are equal or not. If the (A != B)
values are not equal, then the condition becomes true.
> Checks if the value of left operand is greater than the value of (A > B)
right operand. If yes, then the condition becomes true.
< Checks if the value of left operand is less than the value of right (A < B)
operand. If yes, then the condition becomes true.
>= Checks if the value of left operand is greater than or equal to the (A >= B)
value of right operand. If yes, then the condition becomes true.
<= Checks if the value of left operand is less than or equal to the (A <= B)
value of right operand. If yes, then the condition becomes true.
Operators
Logical
Operator Description Example
&& Called Logical AND operator. If both the operands are (A && B)
non-zero, then the condition becomes true.
& Binary AND Operator copies a bit to the result if it exists in (A & B)
both operands.
| Binary OR Operator copies a bit if it exists in either operand. (A | B)
>> Binary Right Shift Operator. The left operands value is moved
right by the number of bits specified by the right operand. A >> 2
Operators
Assignment
Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side operand C = A + B will assign the value
of A + B to C
+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C +
A
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left C -= A is equivalent to C = C -
operand. A
*= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left C *= A is equivalent to C = C *
operand. A
/= Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left C /= A is equivalent to C = C /
operand. A
%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C
%A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
Topics Operators
Decision • If/switch
making
Decision making
if/else
Conditional execution
Test condition and execute statement if condition is true – if
Optional execute other statement if condition is false
if (condition) if (condition) {
statement_true; statement1_true;
else statement2_true;
statement_false; } else {
statement1_false;
statement2_false;
}
Decision Making
Nested if
if (condition) {
if (condition) { if (condition1) {
statement1_true; statement1_true;
statement2_true; statement2_true;
} else { } else if (condition2) {
statement1_false; statement3_true;
statement2_false; statement4_true;
} } else {
} else { statement1_false;
statement3_false; statement2_false;
statement4_false; }
}
Decision making
Switch
Test on integer variable switch (variable) {
case value1:
Different execution depending on value
statement1;
statement2;
break;
case value2: case value3:
statement3;
break;
case value4:
statement4;
return;
default:
statement_default;
}
Homework
Write a program that
◦ Reads 2 integer numbers H and W
◦ Multiplies the numbers to calculate the area
◦ Prints the Area
◦ Print Area is small if less than 100, medium if less than 200 and large if more than 200