Computers and Programming CPC The 2 nd lecture

  • Slides: 34
Download presentation
Computers and Programming CPC The 2 nd lecture Jiří Šebesta

Computers and Programming CPC The 2 nd lecture Jiří Šebesta

TOPIC 1. 2. 3. 4. 5. 6. Expressions Arithmetic conversions Operators Statements in C

TOPIC 1. 2. 3. 4. 5. 6. Expressions Arithmetic conversions Operators Statements in C – introduction Selection statements Iteration statements I.

Expressions (1/3) • Expression: – serves to computing value – consists of operands and

Expressions (1/3) • Expression: – serves to computing value – consists of operands and operators • Operand: – variable, constant or function calling returning a value • Operator: – symbol defining an arithmetic, logic, assignment, comparison and other operation using operands

Expressions (2/3) • Operands as variables int main(void) { int y 1, y 2,

Expressions (2/3) • Operands as variables int main(void) { int y 1, y 2, a=10, b=6; char c; y 1 = a + b; y 2 = a / b; // // printf("%3 dn", y 1); printf("%3 dn", y 2); scanf("%c", &c); return 0; } Example: Ex 06. c + Ex 07. c a, b +, / are operands are operators

Expressions (3/3) • Operands as returning value from function calling. . . #include "math.

Expressions (3/3) • Operands as returning value from function calling. . . #include "math. h" // library for math. functions int main(void) { double y, x = 10000. 2501; char c; y = sqrt(x); printf("Square root of %10. 4 f is %10. 8 fn", x, y); scanf( "%c" , &c); return 0; } Example: Ex 08

Arithmetic conversions (1/2) • All operands of the type … – … char, short

Arithmetic conversions (1/2) • All operands of the type … – … char, short converted to int – … float converted to double • If one operand is of the type … – … double , the second converted to double – … long , the second converted to long – … unsigned, the 2 nd converted to unsigned

Arithmetic conversions (2/2) • Example: arithmetic conversion int float char a b c d

Arithmetic conversions (2/2) • Example: arithmetic conversion int float char a b c d e = = = a, b, c; d, e; f = 'A'; 3/5; 3/5. 0; f+1; 3/5. 0; // // // {int} = {float} printf("%3 dn", a); printf("%3 dn", b); printf("%3 dn", c); printf("%6. 2 fn", d); printf("%6. 2 fn", e); Example: Ex 09. c // // // {int}/{int}/{float} {char->int}+{int} = {int}/{float} 0 0 66 (ASCII of 'A' is 65) 0. 00 0. 60

Operators (1/10) • Unary (a single operand): – changing a sign; – logic negation;

Operators (1/10) • Unary (a single operand): – changing a sign; – logic negation; – etc. int a=1, b=6; a = -a; //changing sign b = !b; //negation printf("%3 dn", a); //-1 printf("%3 dn", b); //0 • Binary (two operands): – – arithmetic (addition, division, …); logic (and, or, xor, …); comparison (>, , ==, , <, , …); assignment (=, …). Example: Ex 10. c + Ex 11. c int a=1, b=6, c, d, e; c = a<=b; d = a!=b; //not equal e = a==b; printf("%3 dn", c); //1 printf("%3 dn", d); //1 printf("%3 dn", e); //0

Operators (2/10) • Arithmetic

Operators (2/10) • Arithmetic

Operators (3/10) • Example: priority of arithmetic operators (higher number in col. of priority

Operators (3/10) • Example: priority of arithmetic operators (higher number in col. of priority = lower priority) int a = 3, b = 12, c = 7; int x, y, z; x = -a + b % c; y = -(a + b) % c; z = (-a + b) % c; printf("%3 dn", x); printf("%3 dn", y); printf("%3 dn", z); Example: Ex 12. c //-3 + (12 % 7) = -3 + 5 = 2 //-(3 + 12) % 7 = -15 % 7 = -1 //(-3 + 12) % 7 = 9 % 7 = 2

Operators (4/10) • Comparison int a = 1, b = 6, c, d, e;

Operators (4/10) • Comparison int a = 1, b = 6, c, d, e; c = a <= b; d = a != b; e = a == b; Example: Ex 13. c //a is smaller or equals b, c = 1 //a does not equal b, d = 1 //a equals b, e = 0

Operators (5/10) • results of comparison operator 0 = false or 1 = true

Operators (5/10) • results of comparison operator 0 = false or 1 = true • comparison operators can be used in complex expressions for testing of complicated conditions int a = 3, b = 12, c = 7; x = a + b == c; //(3 + 12) == 7 => x = 0 y = a + (b == c); //3 + (12 == 7) = 3 + 0 => y = 3 z = a <= b + c != a; //(3 <= (12 + 7)) != 3 = //(3 <= 19) != 3 => z = 1; Example: Ex 14. c

Operators (6/10) • Logic int a = 3, b = 12, c = 7;

Operators (6/10) • Logic int a = 3, b = 12, c = 7; x = !a || b; //!3 || 12 => 0 || 1 => x = 1 y = !(a || b); //!(3 || 12) => !(0 || 1) => y = 0 z = a || b && c; //3 || (12 && 7) => 0 || (1 && 1) //=> z = 1 Example: Ex 15. c

Operators (7/10) • Bit-wise int b = 12, c = 7; x = b

Operators (7/10) • Bit-wise int b = 12, c = 7; x = b >> 2; //1100 b => 0011 b = 3 y = b & c; //1100 b & 0111 b = 0100 b = 4 z = b ^ c; //1100 b ^ 0111 b = 1011 b = 11 Example: Ex 16. c

Operators (8/10) • Truth table for bit-wise operators

Operators (8/10) • Truth table for bit-wise operators

Operators (9/10) • Increment / decrement double r 1, r 2, a 1=5. 1,

Operators (9/10) • Increment / decrement double r 1, r 2, a 1=5. 1, a 2=5. 1, b=4. 2; r 1 = a 1++ + b; r 2 = ++a 2 + b; //r 1 //a 2 //r 2 printf("%6. 2 fn", = = 5. 1 + 4. 2 = 9. 3 a 1 + 1 = 6. 1 a 2 + 1 = 6. 1 + 4. 2 = 10. 3 a 1); r 1); a 2); r 2); Increment, decrement: priority = 2 Example: Ex 17. c

Operators (10/10) • Assignment double r 1=2. 2, r 2=3. 3, a 1=4. 4,

Operators (10/10) • Assignment double r 1=2. 2, r 2=3. 3, a 1=4. 4, a 2=5. 5; int s 1=4, s 2=4; r 1 += a 2 -a 1; r 2 /= a 2 -a 1; //r 1 = r 1 + (a 2 - a 1) //r 2 = r 2 / (a 2 - a 1) printf("%6. 2 fn", r 1); printf("%6. 2 fn", r 2); s 1 <<= 2; s 2 >>= 2; //00000100 b => 00010000 b //00000100 b => 00000001 b printf( "%3 dn" , s 1); printf( "%3 dn" , s 2); Example: Ex 18. c

Statements – introduction (1/4) • Program: a sequence of statements (incl. expression statements, e.

Statements – introduction (1/4) • Program: a sequence of statements (incl. expression statements, e. g. function calling) • If a statement does not cause – control transfer to another part of the program – program interruption then statements are executed sequentially • Standard statement (ANSII C/C++): given by the reserved word (e. g. for, if, else) • notice: reserved words are blue in MS Visual Studio or Code: Blocks – they may not be used as names of variables

Statements – introduction (2/4) • Empty statement if(err) goto end; // in case of

Statements – introduction (2/4) • Empty statement if(err) goto end; // in case of error go to end c++; // otherwise increment end: ; // label and empty command • an empty block { } has the same meaning as a empty statement • Expression statement: – assignments – function calling – etc.

Statements – introduction (3/4) • Expression statement – an example C ++ ; A

Statements – introduction (3/4) • Expression statement – an example C ++ ; A = cos(b) + c; • Compound statement: a sequence of statements closed in braces – {stat 1; stat 2; stat 3; } • Compound statement can contain another compound statement: – a nested block – a superior block

Statements – introduction (4/4) • Compound statements – an example int main( void) {

Statements – introduction (4/4) • Compound statements – an example int main( void) { char text[40] = "The momentary laps of. . . "; int n, conv = 0; for(n=0; n<strlen(text); n++) { if( text[n]==' ') { text[n+1] = text[n+1] - 32; conv++; } } printf("Mod. : %s (%d changed)", text, conv); getchar(); return 0; }

Selection statements (1/7) • Conditioned statement: if(test) statement; condition - true - false if(test)

Selection statements (1/7) • Conditioned statement: if(test) statement; condition - true - false if(test) statement_this; else statement_that; char day = '1'; if(day<48 || day >57) printf("Not a numbern"); else if(day>48 && day<56) // 1, 2, 3, … 7 printf("Now is the %cth dayn", day); else printf("An invalid day numbern");

Selection statements (2/7) • if

Selection statements (2/7) • if

Selection statements (3/7) • if - else

Selection statements (3/7) • if - else

Selection statements (4/7) • if – else

Selection statements (4/7) • if – else

Selection statements (5/7) • Ternary condition ? : (operator) Test ? Statement_this : Statement_that;

Selection statements (5/7) • Ternary condition ? : (operator) Test ? Statement_this : Statement_that; statement for condition true condition Selection of higher value: int a, b, c; a = 3; b = 9; c = (a > b) ? a : b; // if (a>b) c = a; // else c = b; Example: Ex 19. c // c = 9 statement for false condition

Selection statements (6/7) • Switch statement: when choosing from more than two possibilities expression

Selection statements (6/7) • Switch statement: when choosing from more than two possibilities expression with integer value switch(value) { case 1 : statement_1; break; case 2 : statement_2; break; case 3 : statement_3; break; case 4 : statement_4; break; default: statement_other; } expression in case of another value leaving the switch body

Selection statements (7/7) • Switch statement – an example printf("Which girl should go to

Selection statements (7/7) • Switch statement – an example printf("Which girl should go to the cinema with me? n") srand(time(NULL)); switch(rand()%9) // random number from 0 to 8 { case 0: printf("Jana"); break; //if rand()%9 is 0 case 1: printf("Eva"); break; //if rand()%9 is 1 case 2: printf("Klara"); break; //if rand()%9 is 2 case 3: printf("Milena"); break; //if rand()%9 is 3 case 4: printf("Dominika"); break; //if rand()%9 is 4 case 5: printf("Erika"); break; //if rand()%9 is 5 case 6: printf("Petra"); break; //if rand()%9 is 6 case 7: printf("Zuzana"); break; //if rand()%9 is 7 default: printf("alone"); //if rand()%9 is not from 0 to 7, i. e. 8 } Example: Ex 20. c

Iteration statements I. (1/5) • for(init; test; update) statement; • None parameter is compulsory:

Iteration statements I. (1/5) • for(init; test; update) statement; • None parameter is compulsory: for( ; ; ) is an infinite loop char text[] = "Vjku"oguucig"ku"ugetgv#"; unsigned int n; for(n=0; text[n]!=''; n++) // loop for all chars in str if(text[n]!=' ') // excluding space text[n] -= 2; // character code shift printf("%sn", text); Example: Ex 21. c

Iteration statements I. (2/5) • for

Iteration statements I. (2/5) • for

Iteration statements I. (3/5) • Example - numeric integration • Trapezoid approximation: area for

Iteration statements I. (3/5) • Example - numeric integration • Trapezoid approximation: area for one section: • Generally: • Integral: is a sum of all partial areas for all sections

Iteration statements I. (4/5) • Example - numeric integration of sin(x) for interval from

Iteration statements I. (4/5) • Example - numeric integration of sin(x) for interval from 0 to π • Calculation: • Program: #include <stdio. h> #include <stdlib. h> #include <math. h> #define pi 3. 141529 int main(void) { double a[101], f[101]; //a = angle, f = func. value int n, i; double sum = 0, step; //step = lenght of interval int start = 3, stop = 100; // max. is 100

Iteration statements I. (5/5) for(n=start; n<=stop; n++) { sum = 0; for(i=0; i<=n; i++)

Iteration statements I. (5/5) for(n=start; n<=stop; n++) { sum = 0; for(i=0; i<=n; i++) { a[i] = 180. 0*i/(n*1. 0); f[i] = sin(a[i]*pi/180. 0); } step = pi/(1. 0*n); for(i=0; i<n; i++) sum += f[i]*step + (f[i+1]-f[i])*step/2. 0; printf("n. For %d intervals is integral %10. 8 f. ", n, sum); } Example: Ex 22. c

TOPIC OF THE NEXT LECTURE 1. Statements II. 2. Strings 3. Libraries stdio. h

TOPIC OF THE NEXT LECTURE 1. Statements II. 2. Strings 3. Libraries stdio. h and string. h THANK YOU FOR YOUR ATTENTION