Information Science 1

Similar documents
Information Science 1

Information Science 1

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

Fundamentals of Programming Session 7

Chapter 2. Elementary Programming

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

Chapter 2 Elementary Programming

Declaration and Memory

Visual C# Instructor s Manual Table of Contents

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

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

Full file at

UNIT- 3 Introduction to C++

ECE 122 Engineering Problem Solving with Java

Chapter 2: Using Data

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

Chapter 2: Data and Expressions

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 2: Data and Expressions

Operators in C. Staff Incharge: S.Sasirekha

Full file at

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

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

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Chapter 3 Structured Program Development in C Part II

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

SECTION II: LANGUAGE BASICS

Chapter 2: Data and Expressions

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

Information Science 1

Operators & Expressions

CMPT 125: Lecture 3 Data and Expressions

Datatypes, Variables, and Operations

CS 106 Introduction to Computer Science I

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

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

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

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

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators. Java operators are classified into three categories:

Operators and Expressions

PART II. Computation

Information Science 1

Data Types and Variables in C language

Chapter 2 Primitive Data Types and Operations. Objectives

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

Programming in C++ 5. Integral data types

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chapter 3 Structure of a C Program

Programming in C++ 6. Floating point data types

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Chapter 12 Variables and Operators

Chapter 2 ELEMENTARY PROGRAMMING

CIS 110: Introduction to Computer Programming

CIS133J. Working with Numbers in Java

Program Fundamentals

Basics of Java Programming

CS110: PROGRAMMING LANGUAGE I

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

Primitive Data Types: Intro

Topic 4 Expressions and variables

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

A Java program contains at least one class definition.

LECTURE 3 C++ Basics Part 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Computer System and programming in C

Lecture 3 Tao Wang 1

BITG 1233: Introduction to C++

Chapter 3: Operators, Expressions and Type Conversion

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

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

Java Primer 1: Types, Classes and Operators

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

CSI33 Data Structures

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

QUIZ: What value is stored in a after this

Chapter 4: Basic C Operators

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

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

Chapter 2 Primitive Data Types and Operations

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

JAVA Programming Fundamentals

3. Java - Language Constructs I

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Transcription:

Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type arithmetic and type casting Shortcut operators Week 09 College of Information Science and Engineering Ritsumeikan University white spaces comment statements Quiz Recall Week 08 Compiler, interpreter Type, data type, ADT Declaration Variable Constant Literal Integer, float, char, boolean Identifier Assignment statement Our goals today To realize the importance of documenting the source code To understand how to correctly do arithmetic with computers, using high-level programming languages To understand arithmetic operator precedence White space To increase readability of your code, surround operators and operands with white space and skip lines between logical sections of program 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 Comment statements 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

Documenting programs 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 Expressions <left side> = <right side> Use line and block comments to explain the logic of the program (for example include pseudocode) Arithmetic expressions Arithmetic expressions compute numeric results and make use of the arithmetic operators: Operator + * / % 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 8 / 12 addition subtraction division % usage examples 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 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 6th day in a week (Sunday is 0th) 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 equals to equals to 4 0 The remainder modulus operator % (or multiplication modulus (remainder after division) 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 always explain constants used (there must be no unexplained magic coefficients ) explain algorithm by default assumptions always explain the program I/O (interface) Recall assignment statements: 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 Division by zero Integer division by 0: Example: int x = 4 / 0; This will result in no compiler error, but at run time, ArithmeticException message (fatal error) will be generated, and the program will stop executing 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)

Precedence Operator precedence Operator Order of evaluation 1 ( ) left - right 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 3 4 2 1 Expression trees Sometimes, the evaluation of an expression is convenient to show using an expression tree The operators lower in the tree have higher precedence for that expression, e.g. a + (b c) / d a + b c / d Mixed-type arithmetic 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" Any expression involving a floating-point operand will have a floating-point result (on right side) 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 // Pseudocode: 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, but 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) Explicit type casting Syntax: (datatype)( expression ) Examples: x = 2.5 + (float)5/(float)2; c = (int)(x - 3); Note that parentheses around expression are optional if expression consists of one variable Useful for calculating averages (think, why?)

Shortcut operators ++ is increment by 1, and -- is decrement by 1. Examples: count++; // count = count + 1; count--; // count = count - 1; Postfix version (var++, var--): use value of var in expression, then increment or decrement Prefix version (++var, --var): increment or decrement var, then use its value in expression Examples of shortcuts // Pseudocode: int x,y,z; z = x++ + y; // first z is 7; then x = 3 z = ++x + y; // first x = 3; then z is 8 z = x++ + ++y + y/2; /* first y = 6, then it becomes x++ + 6 + 6/2 z = 2 + 6 + 3 z = 11 then x = 3 */ 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; Note that no spaces are allowed between the arithmetic operator and the equals sign Precedence Operator Operator precedence Order of evaluation 1 ( ) left - right parenthesis for explicit grouping 2 ++ -- right - left preincrement, predecrement 3 * / % left - right multiplication, division, modulus 4 + - left - right addition or String concatenation, subtraction 5 = += -= *= /= %= right - left assignment After evalua tion ++ -- right - left postincrement, postdecrement Concepts you need to know after this class White spaces and comment statements: Why we do need to document programs Arithmetic expressions: Operators and operator precedence (including shortcuts) Mixed-type arithmetic rules and type casting Homework Read these slides again Do the self-preparation assignments Learn the vocabulary Consult, whenever necessary, the textbook materials

Next class Boolean expressions Binary logic Relational and logical operators Operator precedence Boolean and algebraic expressions Write your name (in English), student ID number, and solve the following problems (write only answers): 1. Quiz 04