1 / 18

EEE 243B Applied Computer Programming

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;

tavon
Download Presentation

EEE 243B Applied Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEE 243BApplied Computer Programming Blocks and Mixed Type Expressions §3.5 - 3.6 Dr Alain Beaulieu

  2. 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

  3. Outline • Type conversion • Implicit • Explicit • Statements • Compound Statements (Blocks) Dr Alain Beaulieu

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Promotion Hierarchy Dr Alain Beaulieu

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Next Lecture • Flow and control Dr Alain Beaulieu

More Related