Lecture3

Lecture3 - ECE15: Introduction to Computer Programming...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 3: Expressions and Operators
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Basic Types of Expressions in C: Arithmetic Expressions Type Conversions in Arithmetic Expressions Assignment Expressions Example: conversion from Fahrenheit to Celsius Logical expressions sizeof operator, typedef declaration, and enum specifier Lecture Unit 3 ECE15: Introduction to Computer Programming Using the C Language 2 Outline of this Lecture
Background image of page 2
Lecture Unit 3 ECE15: Introduction to Computer Programming Using the C Language 3 What Is an Expression? An expression is a constant, a variable, or a meaningful (valid) combination of constants, variables, and/or other expressions, that are combined using operators : Every expression has a type and a value . Expressions that involve only constants and operators are called constant expressions . They are evaluated at compilation time, and can be used in a #define directive: Note that operators in expressions can be unary ( x++ ), binary ( x+y ), or ternary ( found ? x : y ). 77 x x = 3 + y (x > 0) && (x < 70) x++ found ? x : y ‘77’ for x = 3 +/ y (x > 0),(x < 70) x** found ! x : y #define GOLDEN_RATIO (1+2.236)/2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Unit 3 ECE15: Introduction to Computer Programming Using the C Language 4 Simple Arithmetic Expressions Expression Value Comments 1+2*3 7 Multiplication always precedes addition 1-2+3 2 Additions/subtractions proceed from left to right 1-(2+3) -4 Parentheses can be used to alter the operation order 1.0/2.0*3.0 1.5 Multiplications/divisions proceed from left to right 1/2*3 0 Integer division: 1/2 evaluates to 0 12%5 2 This means 12 modulo 5 (remainder upon division by 5 ) 5%12 5 The remainder of 5 when divided by is 5 12.0%5.0 Error The modulo operator % applies only to integers The five basic arithmetic operations are addition + , subtraction - , multiplication * , division / , and modulo % . Here are some examples: Be careful with division / and modulo % operators!
Background image of page 4
Lecture Unit 3 ECE15: Introduction to Computer Programming Using the C Language 5 Integer Division and Modulo Floating-point division works as expected. Integer division always truncates down to the nearest integer . Thus if a and b l of type int are both positive or both negative, then: What if one of a and b is negative? The absolute value of a/b is the same as before (as if both were positive), but with negative sign. The modulo operation a%b produces the integer remainder in the division of a by b . What if the sign of a%b ? It is always true that: a b 13 -13 4 3 1 -3 -1 -4 -3 1 3 -1 a/b = a/b -- the greatest integer less than or equal to (a/b)*b + a%b a Example :
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Unit 3 ECE15: Introduction to Computer Programming Using the C Language 6 Type of an Arithmetic Expression Arithmetic operations on operands of the same type usually produce a result of this same type .
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

Lecture3 - ECE15: Introduction to Computer Programming...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online