Cs 112 introduction to programming
Advertisement
This presentation is the property of its rightful owner.
1 / 26

CS 112 Introduction to Programming PowerPoint PPT Presentation

CS 112 Introduction to Programming. Lecture #3: Computer Hardware/Software; Variables and C# Data Types http://zoo.cs.yale.edu/classes/cs112/. Outline. Admin. and review Computer hardware/software Variables and C# data types. Admin. Please test whether or not your net id works

Download Presentation

CS 112 Introduction to Programming

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cs 112 introduction to programming

CS 112 Introduction to Programming

Lecture #3:

Computer Hardware/Software; Variables and C# Data Types

http://zoo.cs.yale.edu/classes/cs112/


Cs 112 introduction to programming

Outline

  • Admin. and review

  • Computer hardware/software

  • Variables and C# data types


Admin

Admin.

  • Please test whether or not your net id works

    • If you have not sent me your net id, please do so as soon as possible.

  • Lab and office hours

    • Time: http://zoo.cs.yale.edu/classes/cs112/labs.html

    • Lab hour Location:Dunham Garage computer lab (Dunham 120)

    • TAs and I can meet with you at other times as well; please send email to make an appointment


Review

Review

  • Different programming language levels

    • Machine code, assembly code, intermediate language, high level language

    • A compiler translates a program from a higher level language to a lower level language

  • C# compiler compiles a C# program to MSIL

  • Structure of a C# program

    • A program consists of one or more classes

    • A class consists of one or more methods

    • A method consists of one or more statements

    • White space and comments, identifiers, keywords, namespace

  • Our first C# programs


Cs 112 introduction to programming

C# Program Structure (Console)

  • Program specifications (optional)

    //==========================================================

    //

    // File: HelloWorld.cs CS112 Demo

    //

    // Author: Richard Yang Email: [email protected]

    //

    // Classes: HelloWorld

    // --------------------

    // This program prints the string "Hello World!”

    //

    //==========================================================

  • Library imports (optional)

    using System;

  • Class and namespace definitions

    class HelloWorld

    {

    static void Main(string[] args)

    {

    Console.WriteLine(“Hello World!”);

    }

    }


Cs 112 introduction to programming

C# Program Structure (Window)

  • Program specifications (optional)

    //==========================================================

    //

    // File: HelloWorld.cs CS112 Demo

    //

    // Author: Richard Yang Email: [email protected]

    //

    // Classes: HelloWorld

    // --------------------

    // This program shows a message box.

    //

    //==========================================================

  • Library imports (optional)

    using System;

    using System.Windows.Forms;

  • Class and namespace definitions

    class HelloWorld

    {

    static void Main(string[] args)

    {

    MessageBox.Show(“Hello World!”);

    }

    }


Cs 112 introduction to programming

Outline

  • Admin. and review

  • Computer hardware/software

  • Variables and C# data types


Cs 112 introduction to programming

Monitor

Keyboard

Central

Processing

Unit

Hard Disk

CD ROM

Main

Memory

Computer Environment: Hardware

  • Hardware

    • the physical, tangible parts of a computer

    • E.g., CPU, storage, keyboard, monitor

chip that executes program commands

e.g.,

Intel Pentium IV

Sun Sparc

Transmeta

primary storage area for programs and data

also called RAM


Cs 112 introduction to programming

H i , H e a t h e r .

72 105 44 32 72 101 97 116 104 101 114 46

Storing Information

  • Computers store all information digitally:

    • E.g. numbers, program instructions, text, audio, and video

  • Information is stored in binary numbers

    • A single binary digit (0 or 1) is called a bit

    • A single bit can represent two possible states, like a light bulb that is either on (1) or off (0)

    • Combinations of bits are used to store values


Cs 112 introduction to programming

1 bit

2 bits

3 bits

4 bits

0

1

00

01

10

11

000

001

010

011

100

101

110

111

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

Binary Bit Combinations

Each additional bit doubles the number of possible combinations


Cs 112 introduction to programming

Each memory cell stores a set number of bits (usually 8 bits, or one byte)

10011010

Large values are

stored in consecutive

memory locations

Information is Stored in Memory

Each memory cell has a numeric address, which uniquely identifies it

9278

9279

9280

9281

9282

9283

9284

9285

9286


Cs 112 introduction to programming

performs calculations and decisions

Arithmetic / Logic Unit

coordinates processing steps

Control Unit

small, fast storage areas, e.g., r1, r2

Registers

The Central Processing Unit

  • A CPU is also called a microprocessor


Cs 112 introduction to programming

The Central Processing Unit

  • A CPU continuously follows the fetch-decode-execute cycle:

9278

9279

9280

9281

retrieve an instruction from main memory

a = b + c + d

fetch

load [9279], r1

load [9280], r2

add r1, r2, r3

load [9281], r4

add r3, r4, r5

store r5, [9278]

execute

decode

carry out the

instruction

determine what the

instruction is


Cs 112 introduction to programming

Computer Environment: Software

  • Operating System

    • E.g., Linux, Mac OS X, Windows 2000, Windows XP

    • manages resources such as CPU, memory, and disk

    • controls all machine activities

  • Application programs

    • generic term for any other kind of software

    • compiler, word processors, missile control systems, games


Cs 112 introduction to programming

User

Program

Operating

System

CPU

Disk

Network

Operating System

  • What does an OS do?

    • hides low level details of bare machine

    • arbitrates competing resource demands

  • Useful attributes

    • multi-user

    • multi-tasking


Cs 112 introduction to programming

File System

  • Hierarchical (directories and files)

  • Filename: sequence of directory names ending with a file name

C:

WINDOWS

Documents and Settings

yry3

My Documents


Cs 112 introduction to programming

Some Useful Commands

  • File system

    • mkdir as0 // creates a directory named as0

    • cd as0 // changes current directory to as0

    • cd .. // changes current directory one level up

    • dir // list the files of current directory

    • del <filename> // delete the file

    • Note 1: you can always do the above using Windows GUI

    • Note 2: you can even access the directory remotely by typing \\plucky.cs.yale.edu\yry3$\MyDocsin the Address field of your browser (replace yry3 with your net id)

  • Editing

    • notepad <filename> // edit a file using notepad

      • Note: notepad insists on adding .txt after the file name. If you do not want the .txt suffix, choose “All Files” as “Save as type”

    • scite <filename> // edit file using SciTE, a code editor


Cs 112 introduction to programming

Preview

  • Data types (3 classes)

    • Variables and C# data types

    • Arithmetic expressions and conversion

    • Using predefined data types

  • Program statements (3 classes)

    • Program statements

    • Loop statements

    • Examples


Cs 112 introduction to programming

Outline

  • Admin. and review

  • Computer hardware/software

  • Variables and C# data types


Variables

variable name

9200

9204

9208

9212

9216

9220

9224

9228

9232

numberOfStudents:

data type

total:

int numberOfStudents;

average:

max:

int total;

int average, max;

Variables

  • A variable is a typed name for a location in memory

  • A variable must be declared, specifying the variable's name and the type of information that will be held in it

Which ones are valid variable names?

  • myBigVar VAR1 _test @test

  • 99bottles namespace It’s-all-over


Assignment

Assignment

  • An assignment statement changes the value of a variable

  • The assignment operator is the = sign

int total;

total = 55;

  • The value on the right is stored in the variable on the left

    • 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 (more later)

  • You can declare and assign initial value to a variable at the same time, e.g.,int total = 55;


Cs 112 introduction to programming

Example

static void Main(string[] args)

{

int total;

total = 15;

System.Console.Write(“total = “);

System.Console.WriteLine(total);

total = 55 + 5;

System.Console.Write(“total = “);

System.Console.WriteLine(total);

}


Constants

Constants

  • A constant is similar to a variable except that it holds one value for its entire existence

  • The compiler will issue an error if you try to change a constant

  • In C#, we use the constant modifier to declare a constant

    constant int numberOfStudents = 42;

  • Why constants?

    • give names to otherwise unclear literal values

    • facilitate changes to the code

    • prevent inadvertent errors


C data types

C# Data Types

  • There are 15 data types in C#

  • Eight of them represent integers:

    • byte, sbyte, short, ushort, int, uint, long,ulong

  • Two of them represent floating point numbers

    • float, double

  • One of them represents decimals:

    • decimal

  • One of them represents boolean values:

    • bool

  • One of them represents characters:

    • char

  • One of them represents strings:

    • string

  • One of them represents objects:

    • object


Numeric data types

Numeric Data Types

  • The difference between the various numeric types is their size, and therefore the values they can store:

Type

byte

sbyte

short

ushort

int

uint

long

ulong

decimal

float

double

Storage

8 bits

8 bits

16 bits

16 bits

32 bits

32 bits

64 bits

64 bits

128 bits

32 bits

64 bits

Range

0 - 255

-128 - 127

-32,768 - 32767

0 - 65537

-2,147,483,648 – 2,147,483,647

0 – 4,294,967,295

-91018 to 91018

0 – 1.81019

1.010-28; 7.91028 with 28-29 significant digits

1.510-45; 3.41038 with 7 significant digits

5.010-324; 1.710308 with 15-16 significant digits

Question: you need a variable to represent world population. Which type do you use?


Examples of numeric variables

Examples of Numeric Variables

int x = 1;

short y = 10;

float pi = 3.14;

float f2 = 9.81f;

float f3 = 7E-02; // 0.07

double d1 = 7E-100;

decimal microsoftStockPrice = 28.38m;


  • Login