computers and programming cpc
Post on 06-Jan-2016
47 Views
Preview:
DESCRIPTION
TRANSCRIPT
Computers and Programming CPC
The 2nd lecture
Jiří Šebesta
TOPIC
1. Expressions
2. Arithmetic conversions
3. Operators
4. Statements in C – introduction
5. Selection statements
6. Iteration statements I.
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)
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;}
• Operands as 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, 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 2nd converted to unsigned
Arithmetic conversions (2/2)
• Example: arithmetic conversion
Example: Ex09.c
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
Operators (1/10)
int a=1, b=6;a = -a; //changing signb = !b; //negation printf("%3d\n", a); //-1printf("%3d\n", b); //0
int a=1, b=6, c, d, e;c = a<=b; d = a!=b; //not equal e = a==b;printf("%3d\n", c); //1printf("%3d\n", d); //1printf("%3d\n", e); //0
Example: Ex10.c + Ex11.c
• Unary (a single operand):– changing a sign;– logic negation;– etc.
• Binary (two operands):– arithmetic (addition, division, …);– logic (and, or, xor, …);– comparison (>, , ==, , <, , …);– assignment (=, …).
Operators (2/10)
• Arithmetic
Operators (3/10)
• Example: priority of arithmetic operators (higher number in col. of priority = lower priority)
Example: Ex12.c
int a = 3, b = 12, c = 7;int x, y, z;
x = -a + b % c; //-3 + (12 % 7) = -3 + 5 = 2y = -(a + b) % c; //-(3 + 12) % 7 = -15 % 7 = -1z = (-a + b) % c; //(-3 + 12) % 7 = 9 % 7 = 2
printf("%3d\n", x);printf("%3d\n", y);printf("%3d\n", z);
Operators (4/10)
• Comparison
Example: Ex13.c
int a = 1, b = 6, c, d, e;
c = a <= b; //a is smaller or equals b, c = 1d = a != b; //a does not equal b, d = 1e = a == b; //a equals b, e = 0
Operators (5/10)
Example: Ex14.c
• 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 = 0y = a + (b == c); //3 + (12 == 7) = 3 + 0 => y = 3z = a <= b + c != a; //(3 <= (12 + 7)) != 3 =
//(3 <= 19) != 3 => z = 1;
Operators (6/10)
• Logic
Example: Ex15.c
int a = 3, b = 12, c = 7;
x = !a || b; //!3 || 12 => 0 || 1 => x = 1y = !(a || b); //!(3 || 12) => !(0 || 1) => y = 0z = a || b && c;//3 || (12 && 7) => 0 || (1 && 1)
//=> z = 1
Operators (7/10)
• Bit-wise
Example: Ex16.c
int b = 12, c = 7;
x = b >> 2; //1100b => 0011b = 3y = b & c; //1100b & 0111b = 0100b = 4z = b ^ c; //1100b ^ 0111b = 1011b = 11
Operators (8/10)
• Truth table for bit-wise operators
Operators (9/10)
Increment, decrement: priority = 2
• Increment / decrement
Example: Ex17.c
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.1r2 = ++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);
Operators (10/10)
• Assignment
Example: Ex18.c
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 => 00010000bs2 >>= 2; //00000100b => 00000001b
printf( "%3d\n" , s1);printf( "%3d\n" , s2);
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)
• an empty block { } has the same meaning as a empty statement
if(err) goto end; // in case of error go to endc++; // otherwise increment
end: ; // label and empty command
• Empty statement
• Expression statement:– assignments– function calling– etc.
Statements – introduction (3/4)
C ++ ;
A = cos(b) + c;
• Expression statement – an example
• Compound statement: a sequence of statements closed in braces – {stat1; stat2; stat3;}
• Compound statement can contain another compound statement:– a nested block– a superior block
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;}
Statements – introduction (4/4) • Compound statements – an example
Selection statements (1/7)
if(test) statement; if(test) statement_this;else statement_that;
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");
• Conditioned statement:
condition
- true
- false
Selection statements (2/7)
• if
Selection statements (3/7)
• if - else
Selection statements (4/7)
• if – else if – else
Selection statements (5/7)
Test ? Statement_this : Statement_that;
int a, b, c;
a = 3; b = 9; c = (a > b) ? a : b; // c = 9// if (a>b) c = a;// else c = b;
• Ternary condition ?: (operator)
conditionstatement for true condition
Selection of higher value:
statement for false condition
Example: Ex19.c
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;}
leaving
the switch bodyexpression in case of another value
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 }
Selection statements (7/7)
• Switch statement – an example
Example: Ex20.c
Iteration statements I. (1/5)
• None parameter is compulsory: for( ; ; ) is an infinite loop
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);
• for
Example: Ex21.c
Iteration statements I. (2/5)
• for
Iteration statements I. (3/5)
• Trapezoid approximation: area for one section:
• Example - numeric integration
2
)()( 0101
00101
xfxfxxxfxxS xx
2
)()( 11
1)()1(nnnn
nnnnxnx
xfxfxxxfxxS
• Generally:
• Integral:
is a sum of all partial areas for all sections
Iteration statements I. (4/5)
• Calculation:
• Example - numeric integration of sin(x) for interval from 0 to π
2)11(0coscos)cos()sin( 0
0
xdxx
• 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 intervalint start = 3, stop = 100; // max. is 100
Iteration statements I. (5/5)
Example: Ex22.c
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); }
TOPIC OF THE NEXT LECTURE
1. Statements II.
2. Strings
3. Libraries stdio.h and string.h
THANK YOU FOR YOUR ATTENTION
top related