Docsity

Program Structure - White Space - Lecture Slides | CSE 1301, Study notes of Computer Science

Toggle sidebar
bg1
CSE 1301
Lecture 2
Data Types
Figures from Lewis, “C# Software Solutions”, Addison Wesley
Briana B. Morrison
CSE 1301 1-2
Topics
•Review
• Character Strings
• Variables and Assignments
• Primitive Data Types
CSE 1301 1-3
Review
• C# program structure
• Comments
• Identifiers
• White space
• Objects
• Classes
• Methods
CSE 1301 1-4
CSE 1301 1-5
Program Structure
• In the C# programming language:
– A program is made up of one or more classes
– A class contains one or more methods
– A method contains program statements
• These terms will be explored in detail
throughout the course
• An application always contains a method
called Main
CSE 1301 1-6
Building Blocks - Comments
• Comments explain the program to yourself and
others
• Block comments
– Can span several lines
– Begin with /*
–End with */
– Compiler ignores all text between /* and */
• Line comments
– Start with //
– Compil er ignores text from // to end of line
pf3
pf4
pf5

Partial preview of the text

Download Program Structure - White Space - Lecture Slides | CSE 1301 and more Study notes Computer Science in PDF only on Docsity!

CSE 1301

Lecture 2

Data Types

Figures from Lewis, “C# Software Solutions”, Addison Wesley

Briana B. Morrison

CSE 1301 (^) 1-

Topics

• Review

• Character Strings

• Variables and Assignments

• Primitive Data Types

CSE 1301 (^) 1-

Review

• C# program structure

• Comments

• Identifiers

• White space

• Objects

• Classes

• Methods

CSE 1301 (^) 1-

Program Structure

• In the C# programming language:

– A program is made up of one or more classes

– A class contains one or more methods

– A method contains program statements

• These terms will be explored in detail

throughout the course

• An application always contains a method

called Main

Building Blocks - Comments

• Comments explain the program to yourself and

others

• Block comments

  • Can span several lines
  • Begin with /*
  • End with */
  • Compiler ignores all text between /* and */

• Line comments

  • Start with //
  • Compiler ignores text from // to end of line

CSE 1301 (^) 1-

Identifiers - symbolic names

• Identifiers are used to name classes,

variables, and methods

• Identifier Rules:

– Must start with a letter

– Can contain essentially any number of letters

and digits, but no spaces

– Case sensitive!!

  • Number1 and number1 are different!

– Cannot be keywords or reserved words

CSE 1301 (^) 1-

CSE 1301 (^) 1-

White Space

• Spaces, blank lines, and tabs are called white

space

• White space is used to separate words and

symbols in a program

• Extra white space is ignored

• A valid C# program can be formatted many ways

• Programs should be formatted to enhance

readability, using consistent indentation

CSE 1301 (^) 1-

Topics

• Review

• Character Strings

• Variables and Assignments

• Primitive Data Types

Character Strings

• Object in C#, defined by string class

• String literal is 0 or more characters

enclosed with double quotes

– “The quick brown fox jumped.”

– “x”

• Can contain any valid character

Write and WriteLine Methods

Console.Out.WriteLine (“Whatever you are, be a good one.”);

Output device Monitor Console – class Out - objects

Method name parameter

WriteLine method includes a “new line” character.

CSE 1301 (^) 1-

Topics

• Review

• Character Strings

• Variables and Assignments

• Primitive Data Types

CSE 1301 (^) 1-

Variables

• A variable is a name for a location in memory

used to hold a data value.

• A variable must be declared by specifying the

variable's name and the type of information

that it will hold

int total; int count, temp, result; Multiple variables can be created in one declaration

data type variable name

CSE 1301 (^) 1-21 CSE 1301 (^) 1-

• Names of variables should be

meaningful and reflect the data they will

store

– This makes the logic of the program clearer

• Don't skimp on characters, but avoid

extremely long names

• Avoid names similar to C# keywords

Conventions

Assignment

• An assignment statement changes the

value of a variable

• The assignment operator is the = sign

total = 55;

• The value that was in total is overwritten

• You can only assign a value to a variable that

is consistent with the variable's declared type

• The expression on the right is evaluated and

the result is stored in the variable on the left

Assignment Operator

Syntax:

target = expression;

expression: operators and operands that

evaluate to a single value

--value is then assigned to target

--target must be a variable (or constant)

--value must be compatible with target's

data type

CSE 1301 (^) 1-

Examples:

int numPlayers = 10; // numPlayers holds 10 numPlayers = 8; // numPlayers now holds 8

int legalAge = 18; int voterAge = legalAge;

The next statement is illegal int height = weight * 2; // weight is not defined int weight = 20;

CSE 1301 (^) 1-

• Declare a variable only once

• Once a variable is declared, its data

type cannot be changed.

These statements:

double twoCents; double twoCents = .02;

generate a compiler error

CSE 1301 (^) 1-

• Once a variable is declared, its data

type cannot be changed.

These statements:

double cashInHand; int cashInHand;

generate a compiler error

CSE 1301 (^) 1-

Constants

• Value cannot change during program

execution

• Syntax:

const dataType constantIdentifier = assignedValue;

Note: assigning a value when the constant is

declared is optional. But a value must be

assigned before the constant is used.

Constants

• Constants are useful for three important reasons

• First, they give meaning to otherwise unclear

literal values

  • For example, MAX_LOAD means more than the literal 250

• Second, they facilitate program maintenance

  • If a constant is used in multiple places, its value need only be updated in one place

• Third, they formally establish that a value should

not change, avoiding inadvertent errors by other

programmers

• Use all capital letters for constants and

separate words with an underscore:

Example:

const double TAX_RATE = .05;

• Declare constants at the top of the

program so their values can easily be

seen

• Declare as a constant any data that should

not change during program execution

Conventions

CSE 1301 (^) 1-

Decimal Data Type

• 128 bit storage

• greater precision and smaller range than

other numeric types

• suitable for financial and monetary

calculations

CSE 1301 (^) 1-

char Data Type

• One Unicode character (16 bits - 2 bytes)

Type Size Minimum Value Maximum Value in Bytes char 2 character character encoded as 0 encoded as FFFF

Example declarations: char finalGrade = ‘A’; char newline, tab, doubleQuotes;

CSE 1301 (^) 1-

boolean Data Type

• Two values only:

true false

• Used for decision making or as "flag"

variables

• Example declarations:

bool isEmpty; bool passed, failed = false ;