Variables, Data Types, and Operators

2. Variables, Data Types, and Operators#

A sales total, an inventory count, and a customer name require different kinds of data. This chapter follows values through variables, assignments, expressions, conversion, and console input/output. You will trace how program state changes and use built-in methods before learning to define your own methods in Chapter 5.

  • Variables: Names let programs store values and refer to them later.

  • Types: C# uses types such as int, double, bool, and string to define what each value can do.

  • Operators: Arithmetic, comparison, logical, and string operators let programs compute new values.

  • Built-in methods: Math, Parse, and string methods are ready-made tasks you call with arguments to get a return value.

  • Console I/O: Console.ReadLine and Console.WriteLine connect a program to user input and output.

Chapter Video

C# variables ✖️ (8:44) by Bro Code.

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Declare and initialize variables, then trace assignment and reassignment through changes in program state, including values that must be recalculated.

  2. Choose numeric, Boolean, character, and string types appropriate to a stated data requirement and check the units of a numeric calculation.

  3. Predict arithmetic, comparison, logical, and string-expression results using C# precedence and division rules.

  4. Read a built-in method signature, supply arguments, and use results from methods such as Math.Round and int.Parse.

  5. Convert values with casts or parsing, read console input, and produce labeled, formatted output with checkable results.

Chapter Flow

Suggested three-meeting pace (110 minutes): read the relevant explanations and predict the selected example before class.

Meeting

Minutes

Guided focus

1

35

declarations, assignment, and type choice.

2

40

conversions, operators, and one invoice calculation.

3

35

input, formatting, and expected-output checks.

Class uses selected traces, runs, and checks. Use the remaining worked examples and detailed API tables for practice and reference. Supporting readings are selected separately; lab and homework follow the core lessons.

Glossary

#

Term

Explanation

1

Variable

A named storage location whose value can be read and changed while a program runs.

2

Type

A rule that tells C# what kind of value a variable can store and what operations are allowed.

3

Declaration

A statement that introduces a variable name and its type.

4

Initialization

Giving a variable its first value.

5

Literal

A value written directly in source code, such as 42, true, or "hello".

6

Integer type

A type that stores whole numbers without a fractional part.

7

Floating-point type

A type that stores numbers with fractional values, such as double or decimal.

8

Boolean

A type whose values are true or false.

9

String

A sequence of characters stored as text.

10

Expression

A piece of code that produces a value.

11

Operator

A symbol or keyword that performs an operation on one or more values.

12

Method call

Code that runs a named method, written as the method name followed by arguments in parentheses.

13

Argument

A value you pass to a method inside the parentheses of a call.

14

Return value

The result a method hands back to the code that called it.

15

Math class

A built-in class of common calculations such as Max, Round, Ceiling, and Sqrt.

16

Cast

An explicit conversion from one type to another.

17

Parsing

Converting text input into a typed value.

18

Console input

Data read from the terminal while a program runs.

19

Formatted output

Output that combines text and values in a controlled layout.

20

State table

A record of variable values after each statement executes.

21

Unix timestamp

A count of seconds relative to 1970-01-01 00:00:00 UTC.

22

Program state

The values held by the program at a particular point in execution.

23

Method signature

The name and parameter information that distinguish a method; its declaration also shows the return type.

24

Definite assignment

A compile-time requirement that a local variable has a value before it is read.

25

Local constant

A named compile-time value declared with const that cannot be reassigned.

26

Numeric suffix

Source notation such as m or L that selects a numeric literal type.

27

Integer overflow

An integer calculation produces a mathematical result outside the type’s range.

28

Prefix increment

An increment expression that produces the updated value.

29

Postfix increment

An increment expression that produces the previous value while updating the variable.

30

Associativity

The grouping rule for operators of equal precedence.

31

Bit mask

A pattern selecting bit positions to inspect or change.

32

Input contract

The agreed order, formats, and allowed values for input.

33

Culture

Conventions for interpreting and displaying values, including numeric separators.

34

Format provider

An object supplying conventions to a parsing or formatting operation.

35

Derived value

A result calculated from other values; ordinary assignment stores its current result rather than a live formula.

36

Rate

A quantity expressed relative to another quantity, such as a discount fraction.

37

Percentage point

One unit on a percentage scale; a change from 15 percent to 18 percent is three percentage points.

Lecture Slides

Chapter 2 lecture slides follow the current reading order. Supporting parts remain optional; instructor notes and editable decks are distributed separately.