340 likes | 492 Vues
This lecture delves into essential concepts of C programming, focusing on expressions, operators, and control statements. It defines operands, operators, and how expressions compute values. Learn about different types of operators including arithmetic, logical, and comparison. The presentation also covers arithmetic conversions, demonstrating how various data types interact in expressions. Gain insights into statements in C, including selection and iteration, essential for writing efficient code. This foundational understanding sets the stage for more advanced programming techniques.
E N D
Computers and Programming CPC The 2nd lecture Jiří Šebesta
TOPIC Expressions Arithmetic conversions Operators Statements in C – introduction Selection statements Iteration statements I.
Expressions (1/3) • Operand: • variable, constant or function calling returning a value Expression: serves to computing value consists of operands and operators • Operator: • symbol defining anarithmetic, logic, assignment, comparison and other operation using operands
Expressions (2/3) int main(void) { int y1, y2, a=10, b=6; char c; y1 = a + b; // a, b are operands y2 = a / b; // +, / are operators printf("%3d\n", y1); printf("%3d\n", y2); scanf("%c", &c); return 0; } Operandsas variables Example: Ex06.c + Ex07.c
Expressions (3/3) ... #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.4f is %10.8f\n", x, y); scanf("%c", &c); return 0; } Operands as returning value from function calling Example: Ex08
Arithmetic conversions (1/2) • All operands of the type … • …char, shortconverted toint • …floatconverted todouble • If one operand is of the type … • …double, the second converted todouble • …long, the second converted tolong • …unsigned, the 2nd converted tounsigned
Arithmetic conversions (2/2) int a, b, c; float d, e; char f = 'A'; a = 3/5; // {int} = {int}/{int} b = 3/5.0; // {int} = {int}/{float} c = f+1; // {int} = {char->int}+{int} d = 3/5; // {float} = {int}/{int} e = 3/5.0; // {float} = {int}/{float} printf("%3d\n", a);// 0 printf("%3d\n", b);// 0 printf("%3d\n", c);// 66 (ASCII of 'A' is 65) printf("%6.2f\n", d);// 0.00 printf("%6.2f\n", e);// 0.60 Example: arithmetic conversion Example: Ex09.c
int a=1, b=6; a = -a; //changing sign b = !b; //negation printf("%3d\n", a);//-1 printf("%3d\n", b);//0 Operators (1/10) • Unary (a single operand): • changing a sign; • logic negation; • etc. • Binary (two operands): • arithmetic (addition, division, …); • logic (and, or, xor, …); • comparison (>, , ==, , <, , …); • assignment (=, …). int a=1, b=6, c, d, e; c = a<=b; d = a!=b; //not equal e = a==b; printf("%3d\n", c);//1 printf("%3d\n",d);//1 printf("%3d\n",e); //0 Example: Ex10.c + Ex11.c
Operators (2/10) Arithmetic
Example: priority ofarithmetic operators (highernumberin col. of priority = lower priority) Operators (3/10) int a=3, b=12, c=7; intx, y,z; x=-a + b % c;//-3 + (12 % 7) = -3 + 5 = 2 y =-(a + b) % c;//-(3 + 12) % 7 = -15 % 7 = -1 z=(-a + b) % c;//(-3 + 12) % 7 = 9 % 7 = 2 printf("%3d\n",x); printf("%3d\n", y); printf("%3d\n",z); Example: Ex12.c
Comparison Operators (4/10) inta =1, b =6, c, d, e; c = a <= b;//a is smaller or equals b, c = 1 d = a != b;//a does not equal b, d = 1 e = a == b;//a equals b, e = 0 Example: Ex13.c
results of comparison operator • 0 = false • or • 1 = true • comparison operators can be used in complex expressions for testing of complicated conditions Operators (5/10) 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: Ex14.c
Logic Operators (6/10) 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: Ex15.c
Bit-wise Operators (7/10) int b=12, c=7; x= b >>2;//1100b => 0011b = 3 y = b & c;//1100b & 0111b = 0100b = 4 z= b ^ c;//1100b ^ 0111b = 1011b = 11 Example: Ex16.c
Truth table for bit-wise operators Operators (8/10)
Increment / decrement Operators (9/10) double r1, r2, a1=5.1, a2=5.1, b=4.2; r1 = a1++ + b; //r1 = 5.1 + 4.2 = 9.3 //a1 = a1 + 1 = 6.1 r2 = ++a2 + b; //a2 = a2 + 1 = 6.1 //r2 = 6.1 + 4.2 = 10.3 printf("%6.2f\n", a1); printf("%6.2f\n", r1); printf("%6.2f\n", a2); printf("%6.2f\n", r2); Increment, decrement:priority = 2 Example: Ex17.c
Assignment Operators (10/10) double r1=2.2, r2=3.3, a1=4.4, a2=5.5; int s1=4, s2=4; r1 += a2-a1;//r1=r1+(a2-a1) r2 /= a2-a1;//r2=r2/(a2-a1) printf("%6.2f\n", r1); printf("%6.2f\n", r2); s1 <<= 2;//00000100b=> 00010000b s2 >>= 2;//00000100b=> 00000001b printf( "%3d\n", s1); printf( "%3d\n", s2); Example: Ex18.c
Statements – introduction (1/4) • If a statement does not cause • control transfer to another part of the program • program interruption • then statements are executed sequentially Program:a sequence of statements (incl. expression statements, e.g. function calling) • 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
Empty statement Statements – introduction (2/4) if(err) goto end; // in case of error go to end c++; // otherwise increment end: ; // label and empty command an empty block {} hasthe same meaning as a empty statement • Expression statement: • assignments • function calling • etc.
Expression statement – an example Statements – introduction (3/4) C++ ; A = cos(b) + c; Compound statement:a sequence of statements closed in braces – {stat1; stat2; stat3;} • Compound statementcan contain another compound statement: • a nested block • a superior block
Compound statements – an example Statements – introduction (4/4) 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) if(test) statement; if(test) statement_this; else statement_that; condition - true - false Conditioned statement: char day = '1'; if(day<48 || day >57) printf("Not a number\n"); else if(day>48 && day<56)// 1,2,3,…7 printf("Now is the %cth day\n", day); else printf("An invalid day number\n");
Selection statements (3/7) if - else
Selection statements (4/7) if – else if – else
Selection statements (5/7) Test?Statement_this :Statement_that; statement for true condition statement for false condition condition Selection of higher value: Ternary condition ?: (operator) int a,b,c; a =3; b =9; c =(a>b) ? a : b;// c = 9 // if (a>b)c = a; // else c = b; Example: Ex19.c
Selection statements (6/7) 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; } Switch statement:when choosing from more than two possibilities • leaving • the switch body expression in case of another value
Selection statements (7/7) printf("Which girl should go to the cinema with me?\n") srand(time(NULL)); switch(rand()%9)// random number from 0 to 8 { case0: printf("Jana");break;//if rand()%9 is 0 case1: printf("Eva");break;//if rand()%9 is 1 case2: printf("Klara");break;//if rand()%9 is 2 case3: printf("Milena");break;//if rand()%9 is 3 case4: printf("Dominika");break;//if rand()%9 is 4 case5: printf("Erika");break;//if rand()%9 is 5 case6: printf("Petra");break;//if rand()%9 is 6 case7: printf("Zuzana");break;//if rand()%9 is 7 default: printf("alone");//if rand()%9 is not from 0 to 7, i.e. 8 } Switch statement – an example Example: Ex20.c
for Iteration statements I. (1/5) for(init; test; update) statement; char text[] = "Vjku\"oguucig\"ku\"ugetgv#"; unsigned int n; for(n=0; text[n]!='\0'; n++)// loop for all chars in str if(text[n]!='') // excluding space text[n] -= 2;// character code shift printf("%s\n", text); None parameter is compulsory:for( ; ; ) is an infinite loop Example: Ex21.c
for Iteration statements I. (2/5)
Example - numeric integration Iteration statements I. (3/5) Trapezoid approximation:area for one section: • Generally: • Integral: • is a sum of all partial areas for all sections
Example - numeric integration of sin(x) for interval from 0 to π Iteration statements I. (4/5) • 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 Calculation:
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("\nFor %d intervals is integral %10.8f.", n, sum); } Example: Ex22.c
TOPIC OF THE NEXTLECTURE Statements II. Strings Libraries stdio.h and string.h THANK YOUFOR YOUR ATTENTION