Introduction To Programming 2, Lecture Slide - Computer Science, Slides of C programming

Programming Language, Assembly Code Fragment, Translation, Execution, Java Code, Compilation, Example, Identifiers, Structures, White Space, Comments, Namespaces, Program Structure, Method, Classes, Statements

Typology: Slides

2010/2011

Uploaded on 10/05/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to Programming
Lecture #2:
C# Program Structure
and Our First C# Programs
http://flint.cs.yale.edu/cs112
2
Outline
ØAdmin.
qProgramming language levels
rStructure of a C# program
rCompiling and running our first C# programs
3
Admin.: Workload In Last Year
Assignment 4: Check
0
5
10
15
20
25
30
35
40
1 2 3 4 5 6 7 8 9 1011 1213 14 15 16 17 18 19 2021
Assignment 5: Classes
0
5
10
15
20
25
30
35
40
123456789101112 13 14 15 16 17 18 19 20
Assignment 3: game, 2nd largest, triangle
0
5
10
15
20
25
30
35
40
1 3 5 7 9 1113 15 17 19 21 23 25 27 29 31 33
Assignment 6: Hangman
0
5
10
15
20
25
30
35
40
1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Assignment 7: GUI
0.00
5.00
10.00
15.00
20.00
25.00
30.00
35.00
40.00
1 2 3 4
Assignment: 8Queens
0
5
10
15
20
25
30
35
40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
The assignment this year will be different.
4
Outline
qAdmin.
ØProgramming language levels
rStructure of a C# program
rCompiling and running our first C# programs
5
Programming Language Levels
rEach type of CPU has its own specific machine
language
rOther levels were created to satisfy different
objectives, e.g., make it easier for a human being
to write programs
mmachine language
massembly language
mintermediate language
mhigh-level language
6
Example Machine Code Fragment
177312 137272 001400 026400 017400 000012 000007 004420
010400 011000 000010 005023 012000 012400 000010 003426
013400 000007 000430 003000 064474 064556 037164 000001
024003 053051 000001 041404 062157 000545 007400 064514
062556 072516 061155 071145 060524 066142 000545 002000
060555 067151 000001 024026 046133 060552 060566 066057
067141 027547 072123 064562 063556 024473 000526 005000
067523 071165 062543 064506 062554 000001 046014 067151
067543 067154 065056 073141 006141 004000 004400 000007
006031 015000 015400 000001 040433 070440 067565 062564
060552 060566 064457 027557 071120 067151 051564 071164
060545 000555 003400 071160 067151 066164 000556 012400
046050 060552 060566 066057 067141 027547 072123 064562
063556 024473 000126 000041 000006 000007 000000 000000
000002 000001 000010 000011 000001 000012 000000 000035
000001 000001000000 025005 000267 130401 000000 000400
005400 000000 003000 000400 000000 003400 004400 006000
006400 000400 005000 000000 030400 001000 000400 000000
010400 000262 011002 133003 002000 000262 011002 133005
002000 000261 000000 000001 000013 000000 000016 000003
000000 000016 000010 000020 000020000021 000001 000016
000000 000002 000017
A number specifies what action the computer should take.
pf3
pf4
pf5

Partial preview of the text

Download Introduction To Programming 2, Lecture Slide - Computer Science and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to Programming

Lecture #2:

C# Program Structure

and Our First C# Programs

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

Outline

ÿ Admin.

q Programming language levels

r Structure of a C# program

r Compiling and running our first C# programs

Admin.: Workload In Last Year

Assignment 4: Check

0

5

1 0

1 5

2 0

2 5

3 0

3 5

4 0

1 2 3 4 5 6 7 8 9 10 11 1 2 13 14 1 5 1 6 17 1 8 1 9 20 21

Assignment 5: Classes

0

5

10

15

20

25

30

35

40

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Assignment 3: game, 2nd largest, triangle

0

5

10

15

20

25

30

35

40

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33

Assignment 6: Hangman

0

5

10

15

20

25

30

35

40

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Assignment 7: GUI

1 2 3 4

Assignment: 8Queens

0

5

10

15

20

25

30

35

40

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

The assignment this year will be different.

Outline

q Admin.

ÿ Programming language levels

r Structure of a C# program

r Compiling and running our first C# programs

Programming Language Levels

r Each type of CPU has its own specific machine

language

r Other levels were created to satisfy different

objectives, e.g., make it easier for a human being

to write programs

m machine language

m assembly language

m intermediate language

m high-level language

Example Machine Code Fragment

A number specifies what action the computer should take.

Example Assembly Code Fragment

movl (%edx,%eax), %ecx

movl 12(%ebp), %eax

leal 0(,%eax,4), %edx

movl $nodes, %eax

movl (%edx,%eax), %eax

fldl (%ecx)

fsubl (%eax)

movl 8(%ebp), %eax

leal 0(,%eax,4), %edx

movl $nodes, %eax

movl (%edx,%eax), %ecx

movl 12(%ebp), %eax

leal 0(,%eax,4), %edx

movl $nodes, %eax

Symbols to help programmers to remember the words.

Example C++/C#/Java Code Fragment

bool DetermineNeighbor(int i, int j)

double distanceX = (nodes[i].x - nodes[j].x);

double distanceY = (nodes[i].y - nodes[j].y);

double distanceSquare = disx * disx + disy * disy;

double distance = sqrt(distanceSquare);

if (distance < radius)

return true;

else

return false;

You do not need to understand the exact meaning of this program, just the feeling.

Programming Languages

r A program written in a high-level language must

be translated into machine language before it

can be executed on a particular type of CPU

r A compiler is a software tool which translates

source code into a specific target language

C# Translation and Execution

r The C# compiler translates C# source code (.cs

files) into a special representation called

Microsoft Intermediate Language (MSIL)

r MSIL is not the machine language for any

traditional CPU, but a virtual machine

r The Common Language Runtime (CLR) then

interprets the MSIL file

m It uses a just-in-time compiler to translate from MSIL

format to machine code on the fly

C# Compilation and Execution

C# source

code

MSIL

C#

compiler

Machine

code

Just in time

compiler

Outline

q Admin.

q Programming languages

ÿ Structure of a C# program

r Compiling and running our first C# programs

More on C# Program Structure

r In C#, a program is made up of

m Program specifications (a.k.a. header comments, optional)

m Library imports (optional)

m One or more class (and namespace ) definitions

• A class contains one or more methods

• A method contains program statements

r These terms will be explored in detail throughout

the course

C# Program Structure: Class

class HelloWorld

// comments about the class

class headerclass header

class bodyclass body

Comments can be added almost anywhereComments can be added almost anywhere

C# Classes

r Each class name is an identifier

  • Can contain letters, digits, and underscores (_)
  • Cannot start with digits
  • Can start with the at symbol (@)

r Convention: Class names are capitalized, with

each additional English word capitalized as well

(e.g., MyFirstProgram )

r Class bodies start with a left brace ({)

r Class bodies end with a right brace (})

C# Program Structure: Method

class HelloWorld

// comments about the class

static void Main (string[] args)

// comments about the method

Console.Write(“Hello World!”);

Console.WriteLine(“This is from CS112!”);

C# Method and Statements

r Methods

m Building blocks of a program

m The Main method

• Each console or windows application must have exactly

one (actually can have more, but it is unlikely that you

will see or use)

• All programs start by executing the Main method

m Braces are used to start ({) and end (}) a method

r Statements

m Every statement must end in a semicolon ;

Outline

Welcome1.cs

Program Output

1 // Welcome1.cs 2 // A program in C#. 3 4 using System; 5 6 class Welcome 7 { 8 static void Main( string[] args ) 9 { 10 Console.WriteLine( "Welcome to C# Programming!" ); 11 } 12 }

Welcome to C# Programming!

These are two single line comments.

They are ignored by the compiler and

are only used to aid other programmers.

They use the double slash (//)

This is the using directive. It lets

the compiler know that it should

include the System namespace.

This is a blank line. It means

nothing to the compiler and is only

used to add clarity to the program.

This is the beginning of the Welcome1 class

definition. It starts with the class

keyword and then the name of the class.

This is the start of the Main method.

It instructs the program to do what

you want.

This is a statement.

Console.WriteLine outputs a string.

Outline

q Admin.

q Programming languages

r Structure of a C# program

ÿ Compiling and running our first C# programs

Console Application vs. Window Application

r Console Application

m No visual component

m Only text input and output

m Run under Command Prompt or DOS Prompt

r Window Application

m Forms with many different input and output types

m Contains Graphical User Interfaces (GUI)

m GUIs make the input and output more user friendly!

m Message boxes

  • Within the System.Windows.Forms namespace
  • Used to prompt or display information to the user

Outline

Welcome4.cs

Program Output

1 // Welcome4.cs 2 // Printing multiple lines in a dialog Box. 3 4 using System; 5 using System.Windows.Forms; 6 7 class Welcome 8 { 9 static void Main( string[] args ) 10 { 11 MessageBox.Show( "Welcome\nto\nC#\nprogramming!" ); 12 } 13 }

The System.Windows.Forms

namespace allows the programmer

to use the MessageBox class.

This will display the contents in a message

box as opposed to in the console window.

Backup Slides

Syntax and Semantics

r The syntax rules of a language define how we

can put symbols, reserved words, and identifiers

together to make a valid program

r The semantics of a program statement define

what that statement means (its purpose or role in

a program)

r A program that is syntactically correct is not

necessarily logically (semantically) correct

r A program will always do what we tell it to do, not

what we meant to tell it to do

Errors

r A program can have three types of errors

r The compiler will find problems with syntax and

other basic issues ( compile-time errors )

m If compile-time errors exist, an executable version of

the program is not created

r A problem can occur during program execution,

such as trying to divide by zero, which causes a

program to terminate abnormally ( run-time

errors )

r A program may run, but produce incorrect results

( logical errors )