Professional Documents
Culture Documents
3-Flow Control (Continued)
3-Flow Control (Continued)
3-Flow Control (Continued)
if(TheWeatherIsGood) if(TheWeatherIsGood)
if(NiceRestaurantFound) GoForAWalk();
HaveLunch(); else if(TicketsAvailable)
else GoToATheatre();
EatASandwich(); else if(TableAvailable)
else GoForALunch();
if(TicketsAvailable) else
GoToATheatre(); PlayChessAtHome();
else
GoShopping();
3.2.1 Not only the int is an int
int short The modifier
long
unsigned
short int Counter; short Counter;
long long int Ants; long long Ants;
unsigned int Positive; unsigned Positive;
unsigned long long int Numher; unsigned long long Number;
unsigned short int Lambs; unsigned short Lambs;
2^32=4,294,967,296 4 octets
short float b;
long float var;
is a synonym for another type named double
double a;
3.3.1 Metamorphosis or conversion of data (1)
The “C” language knows two types
of conversions:
• implicit conversions ; their
operation is silent and automatic;
• explicit conversions ; the
developer should insert them
explicitly inside
3.3.2 Metamorphosis or conversion of data (2)
typecast operator is a unary operator with a high
priority, equal to unary minus priority
the updated priority table
(type) value;
float x;
double y;
y = (double) x;
3.3.5 Metamorphosis or conversion of data (5)
• int Int;
• char Char;
• short Short;
• float Float;
• Int = Short + Char + Float;
• Int = (int) Short + (int) Char + Float;
• Int = (float) ((int) Short + (int) Char) + (float) Float);
• Int = (int) ((float) ((int) Short + (int) Char) + (float) Float));
3.3.6 Some simple programs
while(Number != 0) { ... }
while(Number) { ... }
if(Number % 2 == 1) ...
if(Number % 2) ...
3.4.11 The “while” loop in some examples
#include <stdio.h> #include <stdio.h>
int main(void) { int main(void) {
int counter = 5; int counter = 5;
while(counter != 0) { while(counter) {
puts("I am an awesome program"); puts("I am an awesome program");
counter--; counter--;
} }
return 0; return 0;
} }
#include <stdio.h>
int main(void) {
We will use it to compact int counter = 5;
our program once again. while(counter--)
puts("I am an awesome program");
return 0;
}
3.4.14 The “do” loop, or do it at least once (1)
• condition is checked at the do
end of body execution statement;
• the loop's body is while(condition); G2
executed at least once even
if the condition is not met do {
statement_1;
statement_2;
:
:
statement_n;
} while(condition);
3.4.16 “for” - the last loop
All three decisive parts are gathered together. The loop is clear and easy edible. Its name is for.
3.4.20 “for” - the last loop
The for loop has an interesting singularity.
If we omit any of its three components it is presumed
that there is the number 1 instead.
An infinite loop
for( ; ; ) {
/* the body goes here */
}
3.4.21 “for” - the last loop
#include <stdio.h>
int main(void) {
int exp;
int pow = 1;
for(exp = 0; exp < 16; exp++) {
printf("2 to the power of %d is %d\n",exp,pow);
pow *= 2;
}
return 0;
}
3.4.22 break and continue – the loop's spices (1)
• break - exits the loop immediately and
unconditionally ends the loop’s operation;
• continue – behaves as if the program suddenly
reached the end of the body; the end of the loop's
body is reached.
3.5.1 Computers and their logic
• the conjunction “and”, the && operator.
• the disjunction “Or”, the digraph | | (bar bar).
• logical negation, single character ! (exclamation mark) and its priority
is very high
logical operators
3.5.6 Some logical expressions
int i,j;
j = !!i;
3.5.9 How to deal with single bits
there are four operators that allow you to manipulate single bits of data.
i: 00000000000000000000000000001111
j: 00000000000000000000000000010110
int log = i && j;
int bit = i & j;
int logneg = !i; logneg: 00000000000000000000000000000000
int bitneg = ~i; bitneg: 11111111111111111111111111110000
the bitneg variable value is -16. Strange? Not at all!
3.5.14 How to deal with single bits
Each of the above two-argument operators can be used
in the abbreviated form.
bitwise operators
3.5.23 How to deal with single bits
Assessment chap3
Q1
#include <stdio.h>
int main(void){
int i,j, k;
i = -1;
j = 1;
if(i)
j--;
if(j)
i++;
k = i*j;
printf("%d\n",k);
return 0;
}
#include<stdio.h>
int main(void){
int i, j, k;
Q2
i = 0;
j = 0;
if(j)
j--;
else
i++;
if(i)
i--;
else
j++;
k = i + j;
printf("%d\n",k);
return 0; }
#include <stdio.h>
int main(void){
int i, j, k; Q3
i=2;
j=3;
if(j)
j--;
else if(i)
i++;
else
j++;
if(j)
i--;
else if(j)
j++;
else
j=0;
k=i+j; printf("%d\n",k); return 0; }
Q4
#include <stdio.h>
int main(void) {
double x = -.1;
int i = x;
printf("%d\n",i);
return 0;
}
Q5
#include <stdio.h>
int main(void) {
float x,y;
int i,j;
x = 1.5; y = 2.0;
i = 2; j = 3;
x = x * y + i/j;
printf("%f\n", x);
return 0;
}
Q6
#include <stdio.h>
int main(void) {
float x,y;
int i,j;
x = 1.5; y = 2.0;
i = 2; j = 4;
x = x * y + (float)i/j;
printf("%f\n",x);
return 0;
}
Q7
#include <stdio.h>
int main(void) {
int i;
i = 1;
while(i< 16)
i *= 2;
printf("%d\n",i);
return 0;
}
Q8
#include <stdio.h>
int main(void) {
int i,j;
i = 1; j = 1;
while(i<16){
i+=4;
j++;
}
printf("%d\n",j);
return 0;
}
Q9
#include <stdio.h>
int main(void) {
int i = 7, j = i - i;
while(i) {
i /= 2 ;
j++;
}
printf("%d\n",j);
return 0;
}
Q10
#include <stdio.h>
int main(void){
int i = 7, j = i - i;
while(!i) {
i/= 2;
j++;
}
printf("%d\n",j);
return 0;
}
Q11
#include<stdio.h>
int main(void) {
int i, j = 1;
for(i = 11; i > 0; i /= 3)
j++;
printf("%d\n",j);
return 0;
}
Q12
#include <stdio.h>
int main (void) {
int i, j = 0;
for( i = 0; !i ; i++)
j++;
printf("%d\n",j);
return 0;
}
Q13
#include <stdio.h>
int main (void ){
int i = 1, j = -2;
for(;;){
i *= 3;
j++;
if(i > 30)
break;
}
printf("%d\n", j);
return 0;
}
Q14
#include <stdio.h>
int main(void) {
int i = 1, j =-2, k;
k = (i >= 0) && (j >= 0) || (i <= 0) && (j <= 0);
printf("%d\n",k);
return 0;
}
Q15
#include <stdio.h>
int main(void){
int i = 1, j =-2, k;
k = (i >=0) || (j >= 0) && (i <= 0) || (j <=0);
printf("%d\n",k);
return 0;
}
Q16
#include <stdio.h>
int main(void) {
int i = 1, j=-2, k;
k = !(i>=0) || !(j>=0) && !(i<= 0) || !(j<= 0);
printf("%d\n",k);
return 0;
}
Q17
#include <stdio.h>
int main(void){
int i = 1, j = 0, k;
k = i & j;
k |= !!k;
printf("%d\n",k);
return 0;
}
Q18
#include <stdio.h>
int main(void) {
int i = 1, j = 0, k;
k = !i | j;
k = !k;
printf("%d\n",k);
return 0;
}
Q19
#include <stdio.h>
int main(void) {
int i = 1, j = 0, k;
k = (i ^ j) + (!i ^ j) + (i ^ !j) - (!i ^ !j);
printf("%d\n",k);
return 0; }
Q20
#include <stdio.h>
int main(void) {
int i = 0, j = 1, k;
k = i << j + j << i;
printf("%d\n", k);
return 0;
}