Operators and Control Structures
Operators and Control Structures
Logical
Logical &&(AND) expects it's operands to be Boolean and
returns a Boolean value.
According to logical AND(or OR), 0 means false, and 1
means true
Logical AND does not evaluate the second operand if the
first operand becomes false. Similarly, logical OR(||) doesn't
evaluate if the first operand is true.
Bitwise
bitwise AND works on integral value and returns an integral
value(any data type except float)
Bitwise AND and bitwise OR always evaluate their operands
Unary operators
(++): Increment: Increases the value by 1
(--): Decrement: decreases the value by 1
X Y X^Y
000
011
101
110
(~) - Bitwise one's complement - flips one to zero and zero to
one
(<<) - bitwise left shift operator - shifts binary number by on
place to the left and returns the result
(>>) - bitwise right shift operator - shifts binary number by on
place to the right and returns the result.
Other operators
sizeof() - compile-time unary operator which can be used to
compute the size of the operand
(,) Comma operator : used to string together several
expressions, result of the rightmost expression becomes the
value of the total comma-separated expression
eg: b = (a=3,a+1); // result is 4
(?:) Ternary operator:
- syntax : (Expression 1) ? (Expression 2) : (Expression 3);
- if expression 1 evaluates to true, then expression two is
returned, else expression 3
- Eg: int a = 10; int b = 20;
printf("%d",(a>b)?a:b); // output - 20
L and R value:
Variables can be L or R but actual value should be in R.
Control Structures:
Selection Structures
- Simple if else
- if-else statement
- nested if-else statement
- else if ladder
- switch statement
if statement:
if (e1)
<block>|<stmt>
if-else:
if(e1)
<block>|<stmt>
else
<block>|<stmt>
if-else if-else if-else:
if(e1)
<block>|<stmt>
else if(e2)
<block>|<stmt>
else if(e3)
<block>|<stmt>
else
<block>|<stmt>
switch case:
switch(expression)
{
case integral constant: <stmt>break;
case integral constant: <stmt>break;
default: <stmt>;
}
Looping structures
for:
for (e1;e2;e3)
<block>|<stmt>
while:
while(e2)
<block>|<stmt>
do while
do
{ <block>
}while(e2);
e1, e2 and e3 are expressions where e1:
initialization, e2:condition, e3:modifiaction
Unconditional Structures:
goto :
type 1: goto label;
.
.
label:
type 2 : label:
.
.
goto label;
break
continue
return