CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Similar documents
Lecture 3 Tao Wang 1

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 2, Part III Arithmetic Operators and Decision Making

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

(2-1) Numeric Expressions in C H&K Chapter 2. Instructor - Andrew S. O Fallon CptS 121 (August 27, 2018) Washington State University

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Lecture 4 CSE July 1992

C: How to Program. Week /Mar/05

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Computer Programming CS F111

Programming for Engineers Introduction to C

Chapter 2 - Introduction to C Programming

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

JAVA OPERATORS GENERAL

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Reserved Words and Identifiers

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Introduction to Programming

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

C Programming

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Chapter 4: Basic C Operators

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Operators & Expressions

Chapter 3: Operators, Expressions and Type Conversion

Computer System and programming in C

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

COMP Primitive and Class Types. Yi Hong May 14, 2015

C++ Programming: From Problem Analysis to Program Design, Third Edition

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Fundamentals of Programming CS-110. Lecture 3

CS102: Variables and Expressions

C/C++ Programming for Engineers: Working with Integer Variables

Arithmetic Expressions in C

Visual C# Instructor s Manual Table of Contents

LECTURE 3 C++ Basics Part 2

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Information Science 1

4. Inputting data or messages to a function is called passing data to the function.

Variables and Operators 2/20/01 Lecture #

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Basics of Programming

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Full file at C How to Program, 6/e Multiple Choice Test Bank

Data Types and Variables in C language

ANSI C Programming Simple Programs

Chapter 1 & 2 Introduction to C Language

Programming in C++ 5. Integral data types

Full file at

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

1.00 Lecture 4. Promotion

Lecture 3. More About C

Chapter 2: Using Data

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Using Data

BITG 1233: Introduction to C++

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Operators and Expressions:

CHAPTER 3 Expressions, Functions, Output

Section we will not cover section 2.11 feel free to read it on your own

Objectives. In this chapter, you will:

Fundamental of Programming (C)

Chapter 2: Overview of C++

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Program Fundamentals

Information Science 1

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

printf( Please enter another number: ); scanf( %d, &num2);

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Chapter 2 Basic Elements of C++

Fundamentals of Programming

CS110: PROGRAMMING LANGUAGE I

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Creating a C++ Program

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Informatics Ingeniería en Electrónica y Automática Industrial

Objectives. Data Types (continued) Data Types 4. การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Transcription:

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso 1

Topics #define Arithmetic Operators Assignment Operators Operator Precedence Evaluating Arithmetic Expressions Incremental Programming 2

Preprocessor directive #define IDENTIFIER value MUST start in column 1 Used to define data and values that cannot change They are notvariables They don t use an assignment operator (=)

Example #define MY_CONSTANT 4 The preprocessor will go through the source code and replace all instances of MY_CONSTANT with 4 Will only match the entire identifier Eg, Will not replace MY_CONSTANT1 Will not replace anything inside of a string ( ie, )

Source code: #define MY_CONSTANT 4 main () { int constant = MY_CONSTANT; printf( MY_CONSTANT is %d, MY_CONSTANT); } Precompiler changes the program in memory to: main () { int constant = 4; printf( MY_CONSTANT is %d, 4); }

x = a + b = is the assignment operator It computes the value of whatever is to the right of the = Stores that value in the location named on the left hand side of the = There may only be onevariable on the left side of the = 6

x = a + b a + b x =

Unary Operators Operates on oneinput value one result -old_value!true_value Binary Operators Operates on twoinput values oneresult height + margin length * width 8

Name Operator Example Addition + num1 + num2 Subtraction - initial - spent Multiplication * fathoms * 6 Division / sum / count Modulus % m % n 9

Division by zero is mathematically undefined. If you allow division by zero in a program, it will cause a fatal error. Your program will terminate execution and give an error message. Non-fatal errors do not cause program termination, just produce incorrect results. 10

The expression m % n yields the integer remainder after mis divided by n. Modulus is an integer operation both operands MUST be integers. Examples : 17 % 5 = 2 6 % 3 = 0 9 % 2 = 1 5 % 8 = 5 11

Used to determine if an integer value is even or odd 5 % 2 = 1 odd 4 % 2 = 0 even If you take the modulus by 2 of an integer, a result of 1 means the number is odd and a result of 0 means the number is even. 12

Operator(s) Precedence & Associativity () Evaluated first. If nested(embedded), innermost first. * / % Evaluated second. If on same level, left to right. If there are several, left to right. + - Evaluated third. If there are several, left to right. = Evaluated last, right to left. 13

1-14

Use parentheses to change the order in which an expression is evaluated. a + b * c Would multiply b * c first, then add a to the result. If you really want the sum of a and b to be multiplied by c, use parentheses to force the evaluation to be done in the order you want. (a + b) * c Also use parentheses to clarify a complex expression. 15

There are no implied operations in C ALL of them must be specified explicitly Algebraic Formula 2a --------- 1 b C Expression 2* a / ( 1 b ) 3a(b-2x) 3 * a * ( b 2 * x )

Evaluate the following expressions: a = 1 c = 3 b = 2 d = 4 a + b -c + d a * b / c 1 + a * b % c a + d % b c e =b =d + c / b -a 17

Data Type Length in bytes char 1 int 4 float 4 double 8 18

Can mix data types in numerical expressions Hierarchy of types By precision: int float By size: float double Lower size/precision is promotedto greater size/precision before operation is applied Final result is of the highest type 19

E.g.: int num_sticks = 5; double avg_stick_length = 4.5; double total_length; total_length = num_sticks* avg_stick_length; num_stickspromoted to double-precision, then multiplied by avg_stick_length 20

If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away. Examples : 17 / 5 3 4 / 3 1 35 / 9 3 21

Division where at least one operand is a floating point number will produce a floating point answer. Fractional portion is kept. Examples : 17.0 / 5 3.4 4 / 3.2 1.25 35.2 / 9.1 3.86813 What happens? The integer operand is temporarily converted to a floating point, then the division is performed. 22

Example1 : int my_integer = 5; int my_product; /* What will following print out? */ my_product= (my_integer/ 2) * 2.0; printf( my_product is %d \n, my_product); /* What about this? */ my_product= (my_integer/ 2.0) * 2; printf( my_product is %d \n, my_product); 23

What happens when you put a numerical expression in a printf() argument? printf( Result of ( 127 * 5) / 3 is %5d \n, ( 127 * 5) / 3 );

printf shows 6 decimal places by default You can override this by using the format %X.Yf X is the total number of digits to display (including the period) Y is the number of digits after the decimal point to display Examples: Value Format Displayed 12.789 %5.2f 12.79 12.789 %7.2f 12.79 12.789 %8.5% 12.78900 In these examples, an underscore (_) is used to show where a blank would be displayed

There are three kinds of programming errors 1. Syntax error Caught by the pre-processor, compiler, or linker Basically, you typed something wrong 2. Run-time error Something bad happened while the program was running You should implement logic and checks to handle unexpected situations Can cause the application to crash (bad) or give incorrect results (really bad) 3. Logic error The wrong algorithm was implemented Errors in a piece of software are called bugs

It is best not to take the big bang approachto coding. Use an incremental approachby writing your code in incomplete, yet working, pieces. For example, for your projects, Don t write the whole program at once. Just write enough to display the user prompt on the screen. Get that part working first (compile and run). Next, write the part that gets the value from the user, and then just print it out. 27

Get that working (compile and run). Next, change the code so that you use the value in a calculation and print out the answer. Get that working (compile and run). Continue this process until you have the final version. Get the final version working. Bottom line: Always have a working version of your program! 28