Information Science 1

Similar documents
Information Science 1

Expressions and Precedence. Last updated 12/10/18

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

UNIT- 3 Introduction to C++

Operators. Java operators are classified into three categories:

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

Operators in C. Staff Incharge: S.Sasirekha

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

SECTION II: LANGUAGE BASICS

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

Information Science 1

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

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

Operators & Expressions

A flow chart is a graphical or symbolic representation of a process.

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Computers Programming Course 6. Iulian Năstac

JAVA OPERATORS GENERAL

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

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Fundamentals of Programming Session 7

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

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

Chapter 12 Variables and Operators

Chapter 2. Elementary Programming

Chapter 3: Operators, Expressions and Type Conversion

Operators and Expressions

Chapter 2 Elementary Programming

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

Full file at

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 2: Data and Expressions

Visual C# Instructor s Manual Table of Contents

PART II. Computation

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Chapter 2: Using Data

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

Operators and Expressions:

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Declaration and Memory

QUIZ: What value is stored in a after this

ECE 122 Engineering Problem Solving with Java

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Department of Computer Science

Chapter 3 Structure of a C Program

Operators in java Operator operands.

Chapter 7. Expressions and Assignment Statements ISBN

Part I Part 1 Expressions

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

LECTURE 3 C++ Basics Part 2

Chapter 2: Data and Expressions

ME 461 C review Session Fall 2009 S. Keres

CMPT 125: Lecture 3 Data and Expressions

Data Types and Variables in C language

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 2: Data and Expressions

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

Full file at

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Programming in C++ 5. Integral data types

Expressions & Assignment Statements

Expression and Operator

Programming I Lecture 7

Basics of Java Programming

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

CSC 1214: Object-Oriented Programming

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Basics of Programming

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

JAVA Programming Fundamentals

Chapter 3 Structured Program Development in C Part II

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

Fundamental of Programming (C)

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Chapter 7. Expressions and Assignment Statements

Programming in C++ 6. Floating point data types

Engineering Computing I

Arithmetic Operators. Portability: Printing Numbers

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

ISA 563 : Fundamentals of Systems Programming

UNIT 3 OPERATORS. [Marks- 12]

Chapter 12 Variables and Operators

Chapter 2 Primitive Data Types and Operations. Objectives

Review of the C Programming Language for Principles of Operating Systems

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Infix to Postfix Conversion

Prepared by: Shraddha Modi

Prof. Navrati Saxena TA: Rochak Sachan

CIS 110: Introduction to Computer Programming

Transcription:

Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University

Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting programs white spaces comment statements Expressions Arithmetic operators and arithmetic operator precedence Mixed-type arithmetic and type casting Shortcut operators l Quiz 2

Recall Week 08 l Compiler, interpreter l Type, data type, ADT l Declaration l Variable l Constant l Literal l Integer, float, char, boolean l Identifier l Assignment statement 3

Our goals today l To realize the importance of documenting the source code l To understand how to correctly do arithmetic with computers, using high-level programming languages l To understand arithmetic operator precedence 4

White space l To increase readability of your code, surround operators and operands with white space and skip lines between logical sections of program l Space, tab, and new line are white space characters At least one white space character is required between a keyword (reserved word, operator) and identifier Any amount of white space characters are permitted between identifiers, keywords, operators, and literals 5

Comment statements l Comment statements (comments) explain the program to yourself and others There are two types of comments: Block comments Can span several lines Begin with /* and end with */ The compiler ignores everything from /* to */ Line comments Start with // The compiler ignores text from // to the end of line 6

Documenting programs l Always include a block comment at the beginning of each source file (new program) identify the author of the program briefly describe the function of the program l Use line and block comments to explain the logic of the program (for example include pseudocode) always explain constants used (there must be no unexplained magic coefficients ) explain algorithm by default assumptions always explain the program I/O (interface) 7

l Recall assignment statements: <left side> = <right side> Expressions l An expression is a combination of one or more operators and operands that evaluate to a single value, such that: the value is assigned to left side left side is (must be) a single variable or named constant The value must be compatible with (convertible to) the left side s data type 8

Arithmetic expressions l Arithmetic expressions compute numeric results and make use of the arithmetic operators: Operator Operation + addition - subtraction * multiplication / division % modulus (remainder after division) 9

Division and remainder If both operands to the division operator / are integers, the result is an integer (the fractional part is discarded), e.g. 14 / 3 equals to 8 / 12 equals to The remainder modulus operator % (or MOD ) returns the remainder after dividing the first operand by the second, e.g. 14 % 3 equals to 2 8 % 12 equals to 8 Taking modulus is an integer operation both operands must be integers 4 0 10

% usage examples l Remainder operator is very useful in programming. For example: for any even number q, q % 2 is always 0, and for any odd number p, p % 2 is always 1 l Another example: Suppose we know that January 1 is Saturday. We can then easily calculate that the day for February 1 is Tuesday: Saturday is the 6 th day in a week (Sunday is 0 th ) January has 31 days A week has 7 days All this can be written as: (6+31)%7 that gives 2, the second day in a week which is Tuesday 11

Division by zero l Integer division by 0: Example: int x = 4 / 0; l This will result in no compiler error, but at run time, ArithmeticException message (fatal error) will be generated, and the program will stop executing l Floating-point division by 0: If dividend is not 0, the result is Infinity If dividend and divisor are both 0, the result is NaN (not a number) 12

Operator precedence Precedence Operator Order of evaluation Operation 1 ( ) parenthesis for explicit grouping 2 * / % left - right multiplication, division, modulus 3 + - left - right addition, subtraction last = right - left assignment Example: x = a / b * (c + (d - e)) 5 1 4 3 2 13

Expression trees l Sometimes, the evaluation of an expression is convenient to show using an expression tree l The operators lower in the tree have higher precedence for that expression, e.g. a + (b c) / d + a / d b c 14

l In most programming languages, when performing calculations with operands of different data types (on right side): Lower-precision operands are promoted to higher-precision data types, then the calculation is performed Promotion is effective only for expression evaluation and does not cause a permanent change to the data This is called "implicit type casting" l Any expression involving a floating-point operand will have a floating-point result (on right side) 15 Mixed-type arithmetic

16 Rules of promotion Applies the first of these rules that fits: 1. If either operand is a double, the other operand is converted to a double 2. If either operand is a float, the other operand is converted to a float 3. If either operand is a long, the other operand is converted to a long 4. If either operand is an int, the other operand is promoted to an int 5. If neither operand is a double, float, long, or an int, both operands are promoted to int

Examples of promotion int x,y; // integers are 4 bytes using 2's compliment format short z; // shorts are 2 bytes using 2's compliment format float a; // floats are 4 bytes, using floating point format x = 7; z = 3; a = 3.14; y = a * 2 + x/z; Explanation: a = 3.14 (floating point number) '2' is integer but is promoted to '2.0' a floating point number z is a short and will be promoted to an integer x/z involves only integers (7/3) so will give a result of 2 (an integer) then the integer 2 will be promoted to 2.0 (a floating point number) calculation: 3.14 * 2.0 + 2.0 = 8.28 (a floating point number) finally y is assigned an integer 8 since y is an integer data type (the fractional part is removed) 17

18 Explicit type casting l Syntax: (datatype)( expression ) l Examples: float x = 2.5 + (float)5/(float)2; int c = (int)(x 3); l Note that parentheses around an expression with no operators have no effect l Casts are useful for calculating averages (think: why?)

Shortcut operators l ++ is increment by 1, and -- is decrement by 1. Examples: count++; // count = count + 1; count--; // count = count - 1; l Postfix version (var++, var--): use value of var in expression, then increment or decrement l Prefix version (++var, --var): increment or decrement var, then use its value in expression 19

Examples of shortcuts int x,y,z; x = 2; y = 5; z = x++ + y; // first z is 7; then x = 3 x = 2; y = 5; z = ++x + y; // first x = 3; then z is 8 x = 2; y = 5; z = x++ + ++y + y/2; /* is y incremented by ++y before or after y is read for the evaluation of y/2? The result is undefined! */ 20

Note that no spaces are allowed between the arithmetic operator and the equals sign 21 Other shortcut operators Operator Example Equivalent += a += 3; a = a + 3; -= a -= 10; a = a - 10; *= a *= 4; a = a * 4; /= a /= 7; a = a / 7; %= a %= 10; a = a % 10;

22 Operator precedence Precedence 1 ( ) Operator Order of evaluation Operation parenthesis for grouping ++ -- left - right postincrement, postdecrement 2 ++ -- right - left preincrement, predecrement 3 * / % left - right multiplication, division, modulus 4 + - left - right addition or String concatenation, subtraction 5 = += -= *= /= %= right - left assignment

Real example: C99 operator precedence Precedence Operator Description Associativity 1 ++ -- Suffix/postfix increment and decrement Left-to-right ( ) Function call [ ] Array subscripting. -> Structure and union member access ( type ) { list } Compound literal(c99) 2 ++ -- Prefix increment and decrement Right-to-left + - Unary plus and minus! ~ Logical NOT and bitwise NOT (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of 3 * / % Multiplication, division, and remainder Left-to-right 4 + - Addition and subtraction 5 << >> Bitwise left shift and right shift 6 < <= > >= Relational operators (inequalities) 7 ==!= Relational operators (equalities) 8 & Bitwise AND 9 ^ Bitwise XOR (exclusive or) 10 Bitwise OR (inclusive or) 11 && Logical AND 12 Logical OR 13?: Ternary conditional Right-to-Left 14 = Simple assignment += -= Assignment by sum and difference *= /= %= Assignment by product, quotient, and remainder <<= >>= Assignment by bitwise left shift and right shift &= ^= = Assignment by bitwise AND, XOR, and OR 15, Comma Left-to-right 23

24 Concepts you need to know after this class l White spaces and comment statements: Why we do need to document programs l Arithmetic expressions: Operators and operator precedence (including shortcuts) l Mixed-type arithmetic rules and type casting

25 l Read these slides again l Do the self-preparation assignments l Learn the vocabulary Homework l Consult, whenever necessary, the textbook materials

26 Next class l Boolean expressions Binary logic Relational and logical operators Operator precedence Boolean and algebraic expressions

Quiz 04 27