Arithmetic and Assignment Operations in CS112: A Deep Dive, Exercises of C programming

A lecture note from the cs112 introduction to programming course at yale university. It covers the topics of arithmetic and assignment operations. The note explains the concept of arithmetic expressions, arithmetic operators, and their precedence. It also discusses the assignment operators and increment/decrement operators. The note provides examples and code snippets to help students understand the concepts.

Typology: Exercises

2010/2011

Uploaded on 10/06/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to
Programming
Lecture #6:
Arithmetic
and Assignment Operations
http://flint.cs.yale.edu/cs112/
2
Outline
qAdmin. and review
ØArithmetic operations
qAssignment operations
3
Arithmetic Expressions
rAn expression is a combination of operators and
operands
rArithmetic expressions (we will see logical
expressions later) compute numeric results and make
use of the arithmetic operators:
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
rThere are no exponents
Outline
Arithmetic.cs
1 // Arithmetic.cs
2 // An arithmetic program.
3
4 using System;
5
6class Arithmetic
7 {
8 static void Main( string[] args )
9 {
10 string firstNumber, // first string entered by user
11 secondNumber; // second string entered by user
12
13 int number1, // first number to add
14 number2; // second number to add
15
16 // prompt for and read first number from user as string
17 Console.Write( "Please enter the first integer: " );
18 firstNumber = Console.ReadLine();
19
20 // read second number from user as string
21 Console.Write( "\nPlease enter the second integer: " );
22 secondNumber = Console.ReadLine();
23
24 // convert numbers from type string to type int
25 number1 = Int32.Parse( firstNumber );
26 number2 = Int32.Parse( secondNumber );
27
28 // do operations
29 int sum = number1 + number2;
30 int diff = number1 -number2;
31 int mul = number1 * number2;
32 int div = number1 / number2;
33 int mod = number1 % number2;
This is the start of class Arithmetic
Two string variables
defined over two lines
The comment after the
declaration is used to briefly
state the variable purpose
These are two ints that are declared
over several lines and only use one
semicolon. Each is separated by a coma.
Console.ReadLine is used to take the
users input and place it into a variable.
This line is considered a prompt
because it asks the user to input data.
Int32.Parse is used to convert
the given string into an integer.
It is then stored in a variable.
The operation result are stored
in result variables.
Outline
Arithmetic.cs
32 // display results
33 Console.WriteLine( "\n{0} + {1} = {2}.", number1, number2, sum );
34 Console.WriteLine( "\n{0} {1} = {2}.", number1, number2, diff );
35 Console.WriteLine( "\n{0} * {1} = {2}.", number1, number2, mul );
36 Console.WriteLine( "\n{0} / {2} = {2}.", number1, number2, div );
37 Console.WriteLine( "\n{0} % {1} = {2}.", number1, number2, mod );
34
35 } // end method Main
36
37 } // end class Arithmetic
Putting a variable out through Console.WriteLine
is done by placing the variable after the text while
using a marked place to show where the variable
should be placed.
6
Division and Remainder
rIf both operands to the division operator (/) are integers, the
result is an integer (the fractional part is discarded)
rThe remainder operator (%) returns the remainder after dividing the
second operand into the first
8 / 12 equals?
14 / 3 equals? 4
0
14 % 3 equals?
8 % 12 equals?
2
8
pf3
pf4

Partial preview of the text

Download Arithmetic and Assignment Operations in CS112: A Deep Dive and more Exercises C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #6:

Arithmetic

and Assignment Operations

http://flint.cs.yale.edu/cs112/

Outline

q Admin. and review

ÿ Arithmetic operations

q Assignment operations

Arithmetic Expressions

r An expression is a combination of operators and

operands

r Arithmetic expressions (we will see logical

expressions later) compute numeric results and make

use of the arithmetic operators:

Addition +

Subtraction -

Multiplication *

Division /

Remainder %

rThere are no exponents

Outline

Arithmetic.cs

1 // Arithmetic.cs 2 // An arithmetic program. 3 4 using System; 5 6 class Arithmetic 7 { 8 static void Main( string[] args ) 9 { 10 string firstNumber, // first string entered by user 11 secondNumber; // second string entered by user 12 13 int number1, // first number to add 14 number2; // second number to add 15 16 // prompt for and read first number from user as string 17 Console.Write( "Please enter the first integer: " ); 18 firstNumber = Console.ReadLine(); 19 20 // read second number from user as string 21 Console.Write( "\nPlease enter the second integer: " ); 22 secondNumber = Console.ReadLine(); 23 24 // convert numbers from type string to type int 25 number1 = Int32.Parse( firstNumber ); 26 number2 = Int32.Parse( secondNumber ); 27 28 // do operations 29 int sum = number1 + number2; 30 int diff = number1 - number2; 31 int mul = number1 * number2; 32 int div = number1 / number2; 33 int mod = number1 % number2;

This is the start of class Arithmetic

Two string variables

defined over two lines

The comment after the

declaration is used to briefly

state the variable purpose

These are two ints that are declared

over several lines and only use one

semicolon. Each is separated by a coma.

Console.ReadLine is used to take the

users input and place it into a variable.

This line is considered a prompt

because it asks the user to input data.

Int32.Parse is used to convert

the given string into an integer.

It is then stored in a variable.

The operation result are stored

in result variables.

Outline

Arithmetic.cs

32 // display results 33 Console.WriteLine( "\n{0} + {1} = {2}.", number1, number2, sum ); 34 Console.WriteLine( "\n{0} – {1} = {2}.", number1, number2, diff ); 35 Console.WriteLine( "\n{0} * {1} = {2}.", number1, number2, mul ); 36 Console.WriteLine( "\n{0} / {2} = {2}.", number1, number2, div ); 37 Console.WriteLine( "\n{0} % {1} = {2}.", number1, number2, mod ); 34 35 } // end method Main 36 37 } // end class Arithmetic

Putting a variable out through Console.WriteLine

is done by placing the variable after the text while

using a marked place to show where the variable

should be placed.

Division and Remainder

r If both operands to the division operator (/) are integers, the

result is an integer (the fractional part is discarded)

r The remainder operator (%) returns the remainder after dividing the

second operand into the first

8 / 12 equals?

14 / 3 equals? 4

14 % 3 equals?

8 % 12 equals?

Operator Precedence

r Operators can be combined into complex expressions

result = total + count / max - offset;

r Operators have a well-defined precedence which

determines the order in which they are evaluated

r Precedence rules

m Parenthesis are done first

m Division, multiplication and modulus are done second

  • Left to right if same precedence (this is called associativity)

m Addition and subtraction are done last

  • Left to right if same precedence

Precedence of Arithmetic Operations

Operator(s) Operation Order of evaluation (precedence)

( ) Parentheses Evaluated first. If the parentheses are nested,

the expression in the innermost pair is

evaluated first. If there are several pairs of

parentheses “on the same level” (i.e., not

nested), they are evaluated left to right.

*, / or % Multiplication

Division

Modulus

Evaluated second. If there are several such

operators, they are evaluated left to right.

+ or - Addition

Subtraction

Evaluated last. If there are several such

operators, they are evaluated left to right.

Precedence of arithmetic operators.

Operator Precedence: Examples

r What is the order of evaluation in the

following expressions?

a + b + c + d + e

a + b * c - d / e

a / (b + c) - d % e

a / (b * (c + (d - e)))

Example: TemperatureConverter.cs

Data Conversions

r Sometimes it is convenient to convert data from one

type to another

m For example, we may want to treat an integer as a floating

point value during a computation

r Conversions must be handled carefully to avoid

losing information

r Two types of conversions

m Widening conversions are generally safe because they tend

to go from a small data type to a larger one (such as a

short to an int)

  • Q: how about int to long?

m Narrowing conversions can lose information because they

tend to go from a large data type to a smaller one (such as

an int to a short)

Data Conversions

r In C#, data conversions can occur in three

ways:

m Assignment conversion

• occurs automatically when a value of one type is

assigned to a variable of another

• only widening conversions can happen via assignment

• Example: aFloatVar = anIntVar

m Arithmetic promotion

• happens automatically when operators in expressions

convert their operands

• Example: aFloatVar / anIntVar

m Casting

Data Conversions: Casting

r Casting is the most powerful, and dangerous,

technique for conversion

r Both widening and narrowing conversions can be

accomplished by explicitly casting a value

r To cast, the type is put in parentheses in front of the

value being converted

r For example, if total and count are integers, but

we want a floating point result when dividing them,

we can cast total:

result = (float) total / count;

Example: DataConversion.cs

Backup Slides

Operators Associativity Type

left to right

right to left

parentheses

unary postfix

++ -- + - ( type) right to left^ unary prefix

* / % left to right^ multiplicative

+ - left to right^ additive

< <= > >= left to right^ relational

== != left to right^ equality

?: right to left^ conditional

= += -= *= /= %= right to left^ assignment

Precedence and Associativity

high

low