180 likes | 265 Views
EEE 243B Applied Computer Programming. Blocks and Mixed Type Expressions §3.5 - 3.6 Dr Alain Beaulieu. Review Quiz. What type of operator is sizeof ()? What is the value of all the variables for the following expressions int a=5, b=3, c=4, d; d=++ a%c -b--*b;
E N D
EEE 243BApplied Computer Programming Blocks and Mixed Type Expressions §3.5 - 3.6 Dr Alain Beaulieu
Review Quiz • What type of operator is sizeof()? • What is the value of all the variables for the following expressions • int a=5, b=3, c=4, d;d=++a%c-b--*b; • float PI=3.14159, x=7.2, y=-3; bool w;w = y>-x;z = x%y; Dr Alain Beaulieu
Outline • Type conversion • Implicit • Explicit • Statements • Compound Statements (Blocks) Dr Alain Beaulieu
Types Revisited • A type defines • a set of values and • a set of operations • Why do you keep saying the operators are different for different types sir? • They do the exact same operation. Don’t they? Dr Alain Beaulieu
Addition Example Carry out both additions below by hand, listing the steps you took: • Add the integers • 125 + 375 • Add the floating point values • 3.7525e2 + 7.525e-1 • Integer and floating point addition require the computer to do different operations Dr Alain Beaulieu
Multiplication Example Carry out both multiplication below by hand, listing the steps you took: • multiply the integers • 125 * 375 • multiply the floating point values • 3.7525e2 * 7.525e-1 • Integer and floating point multiplication also require the computer to do different operations Dr Alain Beaulieu
Operators and Types • Clearly the *,+ operators require the computer to do different operations depending upon the type • The other operators /, -, >, <, ++, … are also type dependent • So what if we want to add an integer and a floating point number? • x = 3.14159 + 1; Dr Alain Beaulieu
1a. Mixed Type Expressions – Implicit Conversion • What happens when you multiply an integer with a float? • In an assignment expression, the final expression must have the same type as the leftoperand • But before you get to the assignment in a complex expression, C will use implicit type conversionto store temporary values. • C has a promotion hierarchy in order not to lose information during a conversion Dr Alain Beaulieu
Promotion Hierarchy Dr Alain Beaulieu
1b. Mixed Type Expressions –Explicit Conversion • Rather than let the compiler decide, you can explicitly specify or cast your temporary variables • This is what should always be done to ensure that you get the results you want. • Ex.: a, x and y are int • (float) a //converts a to a float • (float) (x+y+3) //converts the result of //x+y+3 to float Dr Alain Beaulieu
Mixed Type Expressions - casting • But there can be some problems in casting: • float x,y; //my result vars • int a; //some int variable • a = 3; // an initial value • x = (float) (a / 10); • y = (float) a / 10; • … • Are x and y the same value? Dr Alain Beaulieu
2. Statements in C • There are various types of statements in C. • The most obvious one is the expression statement. • An expression can be turned into a statement by adding a semicolon ; at the end of the line. • x = a + 3; • y = sizeof(int); • A statement can also be a function call. • TransposeMatrix(matrixA); Dr Alain Beaulieu
3. Compound statements (Blocks) • Compound Statements (aka Blocks) are delimited by braces {} • Blocks are made of a declaration section (optional) and a set of statements. • The main reason for using blocks is to create name spaces and to allocate memory more efficiently. • A variable declared within a block is only accessible inside that block • Once the block terminates the memory can be reused for other purposes. • We will soon see that the for and while loops repeat only 1 statement • Thus a block is used to repeat a group of statements in a for or while loop. Dr Alain Beaulieu
Parallel Blocks • Blocks can follow each other (parallel) { //start block A int k = 5; } //end block A { //Start block B inti = 2; } //end of outside block • Note that there is no need for a semi-colon after the right curly brace • A compound statement is already a statement Dr Alain Beaulieu
Nested Blocks • blocks can be inside other blocks (nested) { //Start outside block A inti = 2; int k = 5; //k is a variable inside block A { //start inside block B int j; int k = 8; //Note that k is a new variable j = k * 3; //declared inside block B!! k = i; //i can be used because it is visible } //end of inside block B i = k; //What is the value of k here? i = j – 6; //ERROR -->j is not visible outside B } //end of outside block A Dr Alain Beaulieu
Practice Problems • As with expression, implicit type conversion and possible errors are very popular exam questions • If x is float, yintand zchar what is the type of the expressions: • x /(y+z) ? • x * (double) y / z Dr Alain Beaulieu
Review • Implicit type casting is done for operators for the built in data types • Assigning for a higher precision data type to a lower precision type can result in a loss of information (errors) • Compound statement have many advantages: • Sometimes the only way to go • Efficient memory organization • Information Hiding (much more on that in OOAD) • Makes code easier to read Dr Alain Beaulieu
Next Lecture • Flow and control Dr Alain Beaulieu