The Loop Control Structure
*
1
Bitwise Operators
*
2
Operation | C Code | a | b | c | d | e | f |
BITWISE AND | c = a & b | 0000 | 1111 | 0000 | 1111 | 1111 | 1111 |
BITWISE OR | d = a | b | 0101 | 1100 | 0100 | 1101 | 1001 | 1010 |
BITWISE XOR | e = a ^ b | 1010 | 1110 | 1010 | 1110 | 0100 | 0101 |
BITWISE COMPLEMENT | f = ~a | 1001 | 0111 | 0001 | 1111 | 1110 | 0110 |
Bitwise Operators
*
3
Bitwise OR Operator |
*
4
Bitwise XOR Operator ^
*
5
Bitwise Complement Operator ~
*
6
Right Shift Operator >>
*
7
Left Shift Operator <<
*
8
Example use of bitwise operators
*
9
Precedence Table with Bitwise Operators�
*
10
Operators | Description | Associativity |
unary + -, ++, --, type, sizeof, ~ | Unary plus/minus, increment/decrement, typecast, sizeof, bitwise complement | Right to left |
* / % | Arithmetic: Multiply, divide, remainder | Left to right |
+ - | Arithmetic: Add, subtract | Left to right |
<< >> | Bitwise left-shift, bitwise right shift | Left to right |
< > >= <= | Relational operators | Left to right |
== != | Relational operators | Left to right |
& | Bitwise AND | Left to right |
^ | Bitwise XOR | Left to right |
| | Bitwise OR | Left to right |
Precedence Table with Bitwise Operators�
*
11
Operators | Description | Associativity |
&& | Logical AND | Left to Right |
|| | Logical OR | Left to Right |
? : : | Conditional | Right to Left |
= | Assignment | Right to Left |
| | |
| | |
| | |
| | |
| | |
Loops
*
12
while
while (expression)
statement
*
13
while
char ch;
ch = getchar( );
while ( ch != ‘\n’ ) {
count ++;
ch = getchar( );
}
printf(“you entered %d characters”, count);
*
14
while
char ch;
while ( (ch = getchar( ) ) != ‘\n’ )
count ++;
printf(“you entered %d characters”, count);
*
15
while
char ch;
ch = getchar( );
while ( ch != ‘\n’ ) {
count ++;
}
printf(“you entered %d characters”, count);
*
16
Breaking with break;
char ch;
while ( 1 ) {
ch = getchar( );
if ( ch == ‘\n’ ) break;
else count ++;
}
printf(“you entered %d characters”, count);
*
17
for loop
statement
while (expr2) {
statement
expr3; }
*
18
for
statement
*
19
for
int j;
for(j=0; j<10; j++)
{ scanf(“%d”,&s);
sum=sum+s;
}
*
20
int count = 0;� char ch;� ch = getchar( ); � while ( ch != ‘\n’ ) � { count ++; ch = getchar( ); }� printf(“you entered %d characters”, count);
int count = 0;
char ch;
for (ch = getchar( ); ch != ‘\n’; count++ )
ch = getchar( );
printf(“you entered %d characters”, count);
*
21
for to replace a while
This is equivalent to
for
*
22
Comma ,
*
23
Comma
It is 3
It is 2 because (j=2),3;
It is 5 because j = (((2,3),4),5)
*
24
for
for(j=0, k=9; j < k; j++,k--) {
t = a[j];
a[j] = a[k];
a[k] = t;
}
*
25
Do-while
statement
while ( expression );
*
26
Do-while
int j = 0;
do {
ch[j ++] = getchar( );
} while(ch[j -1] != ‘\n’);
ch[j -1] = ‘\0’;
*
27
Break and continue
while(exp2) {
statement1
beak;
}
statement2
}
*
28
continue;
*
29
continue example
scanf(“%f”, &v);
if (v < 0) continue;
else {
sv = sqrt(v); /* include<math.h> */
printf(“root is %f\n”, sv);
}
}
*
30
Nested loops
int j, a[5];
int m;
do{
char ch;
for(j=0;j<5;j++)
scanf(“%d”, &a[j]);
m=a[0]; j =1;
while(j<5){
if(m < a[j]) m = a[j];
j ++;
}
printf(“Max is %d \n Want to enter another five ints:”,m);
ch = getchar();
}while(ch == ‘y’ || ch == ‘Y’);
*
31
Nested loops; sorting
/* a[ ] is an int array of 10 elements */
int j,k;
int t;
for(j=10; j >1; j--) {
for(k=0; k < j -1; k++) {
if(a[k] > a[k+1]) {
t = a[k]; a[k] = a[k+1]; a[k+1] = t;
}
}
} /* this is called bubble sort */
*
32
Nested loops; sorting
/* a[ ] is an int array of 10 elements */
int j,k;
int t;
for(j=10; j >1; j--)
for(k=0; k < j -1; k++)
if(a[k] > a[k+1])
t = a[k], a[k] = a[k+1], a[k+1] = t;
*
33