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

Similar documents
Programming in C++ 5. Integral data types

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

Chapter 2: Using Data

3. Java - Language Constructs I

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

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

LECTURE 3 C++ Basics Part 2

Lecture 3 Tao Wang 1

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

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

Basics of Java Programming

More Programming Constructs -- Introduction

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

Operators. Java operators are classified into three categories:

Full file at

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

CMPT 125: Lecture 3 Data and Expressions

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

Lecture 3. More About C

A First Program - Greeting.cpp

JAVA OPERATORS GENERAL

Operators & Expressions

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

CHAPTER 3 Expressions, Functions, Output

Operators in C. Staff Incharge: S.Sasirekha

Operators and Expressions:

Datatypes, Variables, and Operations

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

Programming for Engineers Iteration

Chapter 3: Operators, Expressions and Type Conversion

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

Computer System and programming in C

Visual C# Instructor s Manual Table of Contents

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

SECTION II: LANGUAGE BASICS

Reserved Words and Identifiers

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

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

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

Chapter 2: Basic Elements of C++

Operators in java Operator operands.

Expressions and Precedence. Last updated 12/10/18

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

Programming Lecture 3

Lecture 4 CSE July 1992

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

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

MODULE 02: BASIC COMPUTATION IN JAVA

Chapter 2: Data and Expressions

Basics of Programming

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 2: Data and Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

UNIT- 3 Introduction to C++

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

Declaration and Memory

Zheng-Liang Lu Java Programming 45 / 79

Information Science 1

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

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

1.00 Lecture 4. Promotion

Chapter 3 Structured Program Development in C Part II

CS102: Variables and Expressions

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

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

Work relative to other classes

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Topic 2: C++ Programming fundamentals

DATA TYPES AND EXPRESSIONS

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

The C++ Language. Arizona State University 1

Unit 3. Constants and Expressions

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Bits, Words, and Integers

Computer Programming CS F111

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

A Java program contains at least one class definition.

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Lecture Set 4: More About Methods and More About Operators

Introduction to Computer Science Midterm 3 Fall, Points

3. Java - Language Constructs I

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

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

Lecture 3 Operators MIT AITI

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Chapter 12 Variables and Operators

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

Data types Expressions Variables Assignment. COMP1400 Week 2

Variables and Operators 2/20/01 Lecture #

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

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.

Fundamentals of Programming

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Information Science 1

Transcription:

C/C++ Programming for Engineers: Working with Integer Variables John T. Bell Department of Computer Science University of Illinois, Chicago Preview Every good program should begin with a large comment block explaining what the program does, who wrote it and when. How do you indicate a comment in C/C++? A. // B. % C. /*... */ D. & E. Either A or C 2 1

C++ has five basic types of data: Integers Whole numbers with no fractions. Floating Point Numbers that may contain fractions, and have a larger range due to exponents. Characters Small integer codes representing individual letters and other symbols. Strings Collections of zero or more characters. Booleans Variables holding True or False. 3 Integers come in many flavors: int The natural size for the machine. short May use less bits than an int. long May use more bits than an int. long long Twice as many bits as a long unsigned No negatives, but twice the positives. char 8 bits, usually used to hold characters. wchar 16 bits, wide char for more symbols. 4 2

If a byte is 8 bits, how many bits are in an int? A. 8 B. 16 C. 32 D. 64 E. It depends on the computer hardware and the compiler being used. # of bits: short int long < long long 5 All variables must be declared before use, by specifying their type: int i, j, k; int numstudents; int natoms, nelectrons, nprotons; // Number of students // Number of atoms // Number of electrons // Number of protons. Variables may be initialized when declared: int ncarbons = 0, nhydrogens, noxygens = 16; Uninitialized variables should be assumed random. 6 3

Variable name ( Identifier ) rules Identifiers must begin with a letter ( or underscore. ) Additional characters may be letters, numbers, or underscores. Identifiers are case sensitive: ball Ball BALL Characters beyond a certain number ( 32? ) are ignored. Identifiers may not be C++ reserved words. 7 Variable name ( Identifier ) conventions Integers often begin with i to n. Floating point with a to h, m to z. counters with n or num: natoms, numstudents normal variables begin with lower case. Globals begin with upper case. CONSTANTS in all upper case. Multiword variables in camel case: newhouse Alternate using underscores: new_house Use meaningful names from application domain. 8 4

Assigning values to variables The assignment operator, =, takes the value from the right and stores it in the variable on the left: X = 7; // Store 7 in X X = 5 + Y; // Add 5 to Y, store result in X X = X + 9; // Add 9 to original value of X, // Store result back into X. 9 Some expressions from math class are not legal in C++, and vice versa: X = Y + 7; // Legal in C++ and math class X Y = 7; // Legal in math, not in C++ 7 = X Y; // Legal in math, not in C++ X = X + 42; // Legal in C++, contradiction in math class. 10 5

Basic arithmetic using integers + ( plus ) adds two numbers and yields the result. X = Y + 7; - ( minus ) can be used two ways: Unary takes the negative: X = -Y; Binary subtracts and yields difference: X = Y 10; * ( asterisk or star ) multiplies two numbers and yields the product: X = 3 * 5; 11 DANGER DANGER! Integer division truncates fractions! / ( slash ) divides the number on the left by the number on the right: X = Y / 2; 15 / 5 yields 3 5 / 2 yields 2 99 / 100 yields 0 centigrade = 5 / 9 * ( fahrenheit 32 ); // Stores 0 for all values of Fahrenheit // ( 5.0 / 9.0 would yield correct results. ) 12 6

The modulo operator, %, yields the remainder after division 137 % 10 yields 7 5 % 3 yields 2 3 % 5 yields 3 // 3 / 5 is zero, remainder 3 N % 2 yields 0 if N is even, 1 if N is odd N % M yields an integer from 0 to M-1 inclusive, regardless of the value of N o card = rand( ) % 52; // Yields random 0 to 51 13 Precedence and Associativity *, /, and % have equal precedence, all higher than + and binary ( which are also equal to each other. ) All these operators associate left to right, within operators of equal precedence. So all *, /, and % would be evaluated first, from left to right, followed by all + and binary from left to right. ( Unary has higher precedence than *, /, % ) ( ) can be used to override default precedence. 14 7

What is stored as a result of this statement? N = 5 + 3 / 2; A. 4 B. 5 C. 6 D. 6.5 E. 26.5 15 Combination Assignment Operators It is so common to modify a variable and then store the result back into the same variable that a special set of operators exists: Operator Example Equivalent X += Y; X = X + Y; X -= Y; X = X Y; X *= Y; X = X * Y; X /= Y; X = X / Y; X %= Y; X = X % Y; X *= Y + Z; X = X * ( Y + Z ); // NOT X = X * Y + Z; Note that all assignment operators have lower precedence than any math operators. E.g. + before *= 16 8

What is stored as a result of this statement if N is originally 10? N *= 5 + 3 / 2; A. 16.5 B. 40 C. 53.2 D. 60 E. 65 17 Auto-increment (++) and Auto-decrement (--) Operators adjust integers by one: i++;, as a stand-alone statement, is equivalent to i += 1; or i = i + 1; i--;, as a stand-alone statement, is equivalent to i -= 1; or i = i - 1; Auto-increment and auto-decrement are frequently used in loops, which we will get to a little later. ( Zyante covers ++ and -- in the loop chapter. ) 18 9

++ and -- Prefix versus Postfix The auto-increment and auto-decrement operators come in two forms: Prefix puts the operator before the variable, e.g. ++N or --N Postfix puts the operator after the variable, e.g. N++ or N-- As stand-alone statements the two forms are exactly equivalent, and only change the variable. 19 The difference appears when the operator is used as part of a larger expression Postfix uses the original value of the variable: int copy, N = 3; copy = N++; // Stores 3 in copy; Changes N to 4 Prefix uses the new value of the variable: int copy, N = 3; copy = ++N; // Stores 4 in copy; Changes N to 4 20 10

Precedence and Associativity of ++ and -- Postfix N++ and N-- have higher precedence than anything we have seen, and are evaluated left to right. Prefix ++N and --N have the next lower precedence, equal to unary - and!. This group of operators is evaluated right to left. ( Example: M = - ++N; evaluates ++, then -. ) Prefix / Postfix still determines whether to use the original or new values in the expression. 21 What value is used for N in this location? int copy, N = 3; copy = 3 * N++ * 15.0 / N; A. 2 B. 3 C. 4 D. Error E. Undefined 22 11

What value is used for N in this location? int copy, N = 3; copy = 3 * N++ * 15.0 / N; A. 2 B. 3 C. 4 D. Error E. Undefined Danger! Danger! Never use the same variable more than once in the same expression if it is autoincremented or auto-decremented anywhere in the expression! 23 Review In the following statement, which of the following is the correct order in which the operators will be evaluated? A *= B + C / D E++; A. *=, +, /, -, ++ B. /, +, -, *=, ++ C. ++, /, +, -, *= D. ++, +, /, -, *= E. None of the above. The correct order of operations is not listed here. 24 12