3-Flow Control (Continued)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

3.

Flow control (continued),


more data types and computer
logic
Objectifs
1. if-else statement
2. More data types
3. Conversions
4. Loops
5. Computer logic
3.1.1 The conditional statement
if(TheWeatherIsGood) {
if(TheWeatherIsGood) GoForAWalk();
GoForAWalk(); HaveFun();
}
else
else {
GoToATheatre();
GoToATheatre();
HaveLunch();
EnjoyTheMovie();
}
HaveLunch()
3.1.4 The conditional statement
nesting cascade

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

0…4 294 967 295


-2147483648 .. 2147483647
3.2.9 Not only the int is an int
must not be used
long char;
short char;
must not be used in a single declaration
long short int;
small value such as the number of months or even the day of
the month.
unsigned char LittleCounter;  [0…255]
Char LittleCounter1;  [-128 .. 127].
3.2.9 Not only the int is an int
there are some cases when the compiler recognizes
literals of a type long.
This will happen if:
a literal value goes beyond the acceptable range of
type int;
0L or 1981l  type long
3.2.10 Another float type

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

Ecrire un programme invitant l’utilisateur a saisir deux


nombres, et affiche le plus grand.
3.3.7 Some simple programs

Ecrire un programme invitant l’utilisateur a saisir trois


nombres, et affiche le plus grand,
3.3.8 Some simple programs
But what happens if we ask you to write a program that finds the
largest of a hundred of numbers? Can you imagine the code?
we try to write the algorithm. We call such notation the pseudo-
code.
1. max = -999999999;
2. read number
3. if(number == -1) print max next stop;
4. if(number > max) max = number
5. go to 2
3.4.1 The “while” loop (1)
while my hands are dirty
I am washing my hands;
while(conditional_expression)
statement;
while(conditional_expression) {
statement_1;
statement_2;
…….
statement_n;
}
3.4.4 The “while” loop (4)

Here is an example of a loop that is not able


to finish its execution.
while(1) {
printf("I am stuck inside a loop");
}
The “while” loop (7)

Ecrire un programme invitant l’utilisateur a saisir une suite de nombre, et


affiche le plus grand. Pour arrêter la saisie on tape -1.
3.4.8 The “while” loop in some examples

program reads a sequence of numbers and counts


how many numbers is even and how many is odd;
program terminates when a zero is entered.
3.4.9 The “while” loop in some examples
• note that these two forms are equivalent.

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

Variable > 0 !(Variable <= 0)


Variable != 0 !(Variable == 0)
Morgan's laws.
!(p && q) == !p || !q
!(p || q) == !p && !q
3.5.8 How to deal with single bits
the presented snippet will assign value 1 to
the j variable if i is not zero; otherwise, it will be 0

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.

They are called bitwise operators.


& (ampersand) bitwise conjunction
| (bar) bitwise disjunction
~ (tilde) bitwise negation
^ (caret) bitwise exclusive or

Let us add an important remark: arguments of these operators must be


integer (int as well aslong, short or char); we must not use floats here.
3.5.10 How to deal with single bits
int i = 15, j = 22;

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;
}

You might also like