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

Operators and Control Structures

Uploaded by

snigenigmatic972
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Operators and Control Structures

Uploaded by

snigenigmatic972
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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.

Swapping two variables


int a,b;
a = a^b;
b = a^b;
`a = a^b;'

- n<<1 can be used to multiply variable by 2


- n & 1 can be used to check weather n is odd
- To check weather i'th bit in the variable n is
1:
n & (1<<i) : 0 implies not set and non o implies set;i=bit
number, starts from 0 goes till n from right to left

- To set the ith bit in n


n = n | 1<< i

- To clear the ith bit in n


n = n &~(1<<i)

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

Sequence Point operation:


some basic sequence points available in c are : Logical
AND,OR,conditional opertor, comma operator etc...

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

You might also like