Introduction to c programming
Download
1 / 95

Introduction to C# Programming - PowerPoint PPT Presentation


  • 102 Views
  • Uploaded on

Introduction to C# Programming. ณภัทร สักทอง 1204452 Application Program Development. Outline. 2. Programming Languages C# Language Overview. Programming Languages. Programming Languages. 3. Program

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to C# Programming' - kedem


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
Introduction to c programming

Introduction to C# Programming

ณภัทร สักทอง

1204452 Application Program Development


Outline
Outline

2

  • Programming Languages

  • C# Language Overview


Programming languages

Programming Languages

Programming Languages

3

  • Program

    • A set of instructions for a computer to follow, written in specific programming language

  • Types of programming language

    • High-Level Language

    • Assembly Language

    • Machine Language


High level vs assembly vs machine language

Programming Languages

High-level VS Assembly VS Machine Language

4

  • High-level Language

    • Nearly like human word

    • SUM := A * 2 + ALPHA/3;

  • Assembly Language

    • Some key words are understandable

  • MULL3A, #2, R

  • ADDL3R6, R7, SUM

  • Machine Language

    • Only “0” and “1”

  • 00011000011

  • 00011001111

  • 10011000111

Computer itself understands only Machine language


Language translator

  • Assembly language

  • pushl %ebpmovl %esp, %ebpsubl $8, %espandl $-16, %esp

Programming Languages

  • Machine language

  • 000110001100011100011000111010111100011000110001110

Language translator

5

Interpreter

/ Compiler

Assembler

Machine

Hello World!

_


High level languages

Programming Languages

High-Level Languages

6

  • Functional Language

  • Lisp

  • Logic Language

  • Prolog

  • Procedural Language

    • Fortran

    • Cobol

    • Basic

    • C

    • Pascal

  • Object-Oriented Language

    • C++

    • Java

    • C#


Outline1
Outline

7

  • Programming Languages

  • C# Language Overview


A simple c program

C# Language Overview

A simple C# Program

8

Grouping using { }


A simple c program1

C# Language Overview

A simple C# Program

9

Statement ending with semicolon “;”


A simple c program2

C# Language Overview

A simple C# Program

10

C# syntax is case-sensitive

namespace

NAMEspace

Main()

main()


A simple c program3

C# Language Overview

A simple C# Program

11

White space means nothing

static void Main(string[] args) {

Console.WriteLine("Hello World!");

}

static void Main(string[] args){

Console.WriteLine("Hello World!");}


A simple c program4

C# Language Overview

A simple C# Program

12

Anything between /* */ or after // is considered a comment

Comments will not be translated

static void Main(string[] args) //comment here {

/* This is comment too. */

Console.WriteLine("Hello World!");

}


Program structure

C# Language Overview

Program Structure

13

  • The starting point of the program is:

  • This is known as the method Main

  • A method is put inside a class

  • A class may be put inside a namespace

static void Main ()

{

... starting point ... }


Program structure1

C# Language Overview

Class

Class

namespace

Program Structure

14

  • In C#

    • A program can contain several namespaces

    • A namespace can contain several classes

    • A class can contain several methods

  • In other words

    • Think of a namespace as a container of classes

    • Think of a class as a container of methods

method1

method2


Program structure2

C# Language Overview

Program Structure

15

  • For this 1204452 course

    • Program with only one class and at most one namespace

  • For now until sometime before midterm

    • Program with one method (i.e., Main)

namespace HelloW {

class HelloWClass {

static void Main () {

System.Console.WriteLine("Hello World!");

System.Console.ReadLine();

}

}

}


Naming rules

C# Language Overview

Naming Rules

16

  • Letters, digits and underscores(_)

  • First character letter or _

  • Up to 63 characters long

  • Must not be a reserved word

* Case Sensitive Example

MSU53 ≠ msU53 ≠msu53


Naming rules1

C# Language Overview

Naming Rules

17

  • Letters, digits and underscores(_)

  • First character letter or _

  • Up to 63 characters long

  • Must not be a reserved word

Example

name

Name

point9

9point

_data

class

class_A

class_”A”


C reserved words

C# Language Overview

C# Reserved Words

18




Outline2
Outline

22

  • C# Beginning

  • Variable and Constant

  • Expression

  • Statement

  • Modify-And-Assign

  • Math Class


C beginning

C# Beginning

C# Beginning

23

  • The starting point of the program is:

  • This is known as the method Main

  • A method is put inside a class

  • A class may be put inside a namespace

static void Main ()

{

... starting point ... }


Inside method main

C# Beginning

Inside method Main

24

  • Variable declarations

  • Statements

static void Main(string[] args)

{

const double pi = 3.1416;

int radius;

double area;

radius = int.Parse(Console.ReadLine());

area = pi*radius*radius;

Console.WriteLine(area);

}


Outline3
Outline

25

  • C# Beginning

  • Variable and Constant

  • Expression

  • Statement

  • Modify-And-Assign

  • Math Class


What is variable

Variable & Constant

What is Variable?

26

  • A variable is used to store “data.”

“It must be declared before used”


Data types

Variable & Constant

Data Types

27


C variable declaration

Variable & Constant

C# Variable Declaration

28

  • Syntax:

    • <datatype> <name>;

  • Example:

  • We can also assign its initial value. Example:

  • int radius;

    double area;

    bool isokay;

    int k = 200;

    bool done = false;


    C variable declaration1
    C# Variable Declaration

    29

    • Syntax:

      • <datatype> <name_1>, <name_2>,..., <name_n>;

    • Example:

    • We can also assign its initial value. Example:

    int width, length, height;

    double mean, sd, max, min;

    bool isokay, isright, check;

    int width=5, length, height=4;


    Test i variable declaration
    Test I - Variable Declaration

    30

    • Declare variable3,4,5

    • Name3 : u

    • Name4 : tName5 : a

    • Type : double

    • Initial value3 : 5.0

    • Initial value4 : nothing

    • Initial value5 : 9.8

    • Declare variable1

      • Name : num_Student

      • Type : interger

      • Initial value : nothing

    • Declare variable2

      • Name : gender

      • Type : character

      • Initial value : m


    C constant declaration

    Variable & Constant

    C# Constant Declaration

    31

    • Syntax:

      • const <datatype> <name> = <value>;

  • Example:

  • const int radius = 15;

    const double area=1.5;

    const bool isokay=true;

    const string movie=”StarWarIII”;

    const char mckazine=‘m’;


    C constant declaration1

    Variable & Constant

    C# Constant Declaration

    32

    • Syntax:

      • const <datatype> <name_1> = <value_1>, <name_2> = <value_2>, ... , <name_n> = <value_n>;

  • Example:

  • const int radius = 15, height = 5;

    const double area=1.5, wide=3.2, lenght = 4.1;


    Test ii constant declaration
    Test II - Constant Declaration

    33

    • Declare Constant

      • Name : e

      • Type : double

      • Initial value : 2.71828


    Outline4
    Outline

    34

    • C# Beginning

    • Variable and Constant

    • Expression

    • Statement

    • Modify-And-Assign

    • Math Class


    C expression

    Expression

    Expression

    Arithmetic Expression

    Boolean Expression

    C# Expression

    35


    Arithmetic expression

    Expression

    Arithmetic Expression

    36

    • Operators

      • + - * /

      • % (remainder after division)

    • Example

      • 11 + 5 

      • 39 / 5 

      • 39.0/5 

      • 39 % 5 

      • 5.0 % 2.2 

    16

    7

    7.8

    4

    0.6


    Piority of arithmetic operators

    Expression

    Piority of Arithmetic Operators

    37

    Answer

    int a, b;

    a = 2-10*3/5+(6%4);

    b = 5*(15%4+(2-3))/9;

    a = -2

    b = 1


    Calculation priority

    Expression

    Calculation Priority

    38

    static void Main(){

    int a,b,c,d;

    double e,f,g;

    a=2; b=7; c=5;

    d=c/a;

    e=5/b;

    f=5.0/2;

    g=5/2.0;

    }

    Answer

    d = 2

    e = 0

    f = 2.5

    g = 2.5


    Boolean expression

    Expression

    Boolean Expression

    39

    • Operators

      • Comparison

        • Equal ==

        • Not equal !=

        • Less <

        • Greater >

        • Less than or equal to <=

        • Greater than or equal to >=

      • Boolean

        • And &&

        • Or ||

        • Not!

    0 and 0 = 0

    0 and 1 = 0

    1 and 0 = 0

    1 and 1 = 1

    0 or 0 = 0

    0 or 1 = 1

    1 or 0 = 1

    1 or 1 = 1

    not 0 = 1

    not 1 = 0


    Example boolean expression

    Expression

    Example: Boolean Expression

    40

    • 10 > 50 

    • ’A’ < ’B’ 

    • (3<2) || (2+5 > 6) 

    • (’a’ != ’z’) && !(9==0) 

    false

    true

    true

    true


    Outline5
    Outline

    41

    • C# Beginning

    • Variable and Constant

    • Expression

    • Statement

    • Modify-And-Assign

    • Math Class


    Statements

    Statement

    Statement#1

    Statement#2

    Statements

    42

    • A statement is a unit of command to instruct your program

    • A method consists of one or more statements

    class Hello {

    static void Main () {

    Console.WriteLine("Hello World!");

    Console.ReadLine();

    }

    }


    C statement types

    C# Statement Types

    Statement

    Assignment Statement

    Input Statement

    Output Statement

    C# Statement Types

    43


    Assignment statement

    Statement

    Assignment Statement

    44

    • Assigning value to variable

    • Use the equal sign (=) when making assignments.

    • Syntax:

      • <variable> = <expression>;

    int Width,High;

    Width=10;

    High=20+Width;


    Input statement

    Statement

    Input Statement

    45

    • Console.ReadLine() Return string

      • Use to get the input from user

    • Convert string to other data type

      • int.Parse()

      • Convert string to integer

      • double.Parse()

      • Convert string to double

    Example

    string st;

    st = Console.ReadLine();


    Example input statement

    Statement

    Example: Input Statement

    46

    • Ex1:

    • string myname;

    • myname = Console.ReadLine();

    Ex2:

    int Width;

    string temp1;

    temp1 = Console.ReadLine();

    Width = int.Parse(temp1);


    Output statements

    Statement

    Output Statements

    47

    • Use the method Write or WriteLine in the Console class (which is in System namespace)

    • Basic usage:

    • Advanced usage:

    • Even more advanced usage:

    Console.WriteLine("Hello");

    Console.WriteLine(area);

    Console.WriteLine(”Size {0}x{1}”, width, height);

    double salary=12000;

    Console.WriteLine("My salary is {0:f2}.", salary);


    Outline6
    Outline

    48

    • C# Beginning

    • Variable and Constant

    • Expression

    • Statement

    • Math Class


    The math class

    Math Class

    The Math Class

    49


    Test iii
    Test III

    50

    • Write the program which

      • Input : Your name

      • Output : Your name is <your name>.


    Test iv
    Test IV

    51

    • Write the program which

      • Input : 3 number

      • Output : average of 3 input number


    Test vi
    Test VI

    52

    • Write the program which

      • Input : lenght of radius of circle

      • Output : Area of circle




    Outline7
    Outline

    *

    • Boolean expression

    • if statement

    • nested if statement

    • Flowchart


    Boolean expression1

    Boolean Expression

    Boolean Expression

    *

    • Operators

      • Comparison

        • Equal ==

        • Not equal !=

        • Less <

        • Greater >

        • Less than or equal to <=

        • Greater than or equal to >=

    • Boolean

      • And &&

      • Or ||

      • Not !

    0 and 0 = 0

    0 and 1 = 0

    1 and 0 = 0

    1 and 1 = 1

    0 or 0 = 0

    0 or 1 = 1

    1 or 0 = 1

    1 or 1 = 1

    not 0 = 1

    not 1 = 0


    Boolean expression example

    Boolean Expression

    Boolean Expression Example

    *

    • From the equation: X2+9X+10 = 0

      • How can we check that value of X is the answer for above equation?

    • Condition: Is value Y even number?

    ((X*X +9*X +10) == 0) //true if X is the answer

    (Y%2 == 0) //true if Y is even

    OR

    (Y%2 != 1) //true if Y is even


    Example boolean expressions

    Boolean Expression

    Example: Boolean Expressions

    *

    • double x = 4.0;

    • Expression Value

    • x < 5.0 ___________

    • x > 5.0 ___________

    • x <= 5.0 ___________

    • 5.0 == x ___________

    • x != 5.0 ___________

    • (3!=4)&&(7<5) ___________

    • (4>4)||(5<=10) ___________

    true

    false

    true

    false

    true

    false

    true


    Outline8
    Outline

    *

    • Boolean expression

    • if statement

    • nested if statement

    • switch case statement

    • Flowchart


    If statement
    if statement

    *

    ที่มาของภาพ http://www.ryt9.com/s/prg/774090

    10


    If statement1

    if statement

    if statement

    *

    • Execute the specific statement when the ”condition” becomes true

    • Syntax:

    true

    true

    if (condition) {

    statement1;

    statement2;

    }

    if (condition) {

    statement;

    }


    If statement2

    if statement

    if statement

    *

    • Execute the specific statement when the ”condition” becomes true

    • Syntax:

    true

    if (condition)

    statement;

    if (condition) {

    statement1;

    statement2;

    }

    true

    if (condition){

    statement;

    }


    If statement3

    if statement

    if statement

    condition

    False

    *

    True

    Statement

    if (condition){

    statement;

    }


    If statement4

    if statement

    if statement

    condition

    False

    *

    True

    if (condition)

    statement;

    Statement

    if (condition){

    statement;

    }


    If statement5

    if statement

    if statement

    condition

    *

    True

    False

    if (condition){

    statement1;

    statement2;

    }

    Statement1

    Statement2


    If statement6
    if statement

    *

    • price = 40;

    • if (height <= 140) {

    • Console.WriteLine (“Hello Children!”);

    • price = 0;

    • }

    • Console.WriteLine(“price ={0}”,price);

    price = 40;

    height<=140

    false

    true

    Console.WriteLine(“Hello children”);

    price = 0;

    Console.WriteLine(“price = {0}”, price)

    12


    Introduction to c programming
    การควบคุมการไหลของโปรแกรมการควบคุมการไหลของโปรแกรม

    • โปรแกรมทั่วไปจะทำงานเป็นเส้นตรง

    x = int.Parse(Console.ReadLine());

    y = int.Parse(Console.ReadLine());

    Console.WriteLine(x+y)

    Console.WriteLine("Hello {0}",x)

    z = x * y + 10;

    Console.WriteLine(z)

    14


    Introduction to c programming
    การควบคุมการไหลของโปรแกรมการควบคุมการไหลของโปรแกรม

    • โปรแกรมที่ใช้คำสั่ง if

    height <= 140

    True

    False

    price = 40;

    if (height <= 140) {

    Console.WriteLine("Hello kids!");

    price = 0;

    }

    Console.WriteLine("price={0}",price);

    เมื่อ height = 120

    เมื่อ height = 160

    15


    If else statements
    if – else statementsการควบคุมการไหลของโปรแกรม

    ที่มาของภาพ http://splinedoctors.com/2009/02/hurry-up-and-choose/

    25


    If else
    คำสั่ง if-elseการควบคุมการไหลของโปรแกรม

    • คำสั่ง if

    • คำสั่ง if-else

    26


    If else statement

    if statementการควบคุมการไหลของโปรแกรม

    if…else… statement

    *

    • If condition is true→ execute statement1

    • If condition isfalse→ execute statement2

    • Syntax:

    if (condition)

    statement1; //true

    else

    statement2; //false

    if (condition)

    statement1; //true

    else{

    statement2; //false

    statement3; //false

    }


    If else statement1

    if statementการควบคุมการไหลของโปรแกรม

    if…else… statement

    condition

    False

    True

    *

    Statement2

    Statement1

    if (condition) {

    statement1; //true

    }else {

    statement2; //false

    }


    If else statement2

    if statementการควบคุมการไหลของโปรแกรม

    if…else… statement

    condition

    False

    True

    *

    Statement2

    Statement1

    Statement3

    if (condition){

    statement1; //true

    } else{

    statement2; //false

    statement3; //false

    }


    If else statement example

    if statementการควบคุมการไหลของโปรแกรม

    if…else… statement example

    *

    • Write the program which check input number.

      • input : integer number

      • output : message to inform that number is odd or even.


    If else statement example1

    if statementการควบคุมการไหลของโปรแกรม

    if…else… statement example

    *

    if(n%2 == 0) {

    Console.WriteLine(“It’s even number”);

    } else {

    Console.WriteLine(“It’s odd number”);

    }


    Introduction to c programming
    การควบคุมการไหลของโปรแกรมการควบคุมการไหลของโปรแกรม

    Console.WriteLine("It is an even number");

    n % 2 == 0

    True

    False

    Console.WriteLine("It is an odd number");

    30


    Test i

    if statementการควบคุมการไหลของโปรแกรม

    Test I

    *

    • Write the program which decide result of the examination from student’s score

      • input : number

      • output :


    Test ii

    if statementการควบคุมการไหลของโปรแกรม

    Test II

    *

    • Write the program which find the value of function

      • input : number

      • output :


    Thinking corner
    Thinking cornerการควบคุมการไหลของโปรแกรม

    *

    • Write the program which decide type of integer input - positive, zero or negative integer.

    x > 0

    x == 0

    x < 0


    Outline9
    Outlineการควบคุมการไหลของโปรแกรม

    *

    • Boolean expression

    • if statement

    • nested if statement

    • switch case statement

    • FlowChart


    Introduction to c programming
    มุมนักคิดการควบคุมการไหลของโปรแกรม

    x > 0

    x > 0

    True

    x < 0

    x < 0

    True

    False

    x == 0

    False

    34


    Nested if statement

    Nested if statementการควบคุมการไหลของโปรแกรม

    Nested if statement

    *

    int N;

    N = int.Parse(Console.ReadLine());

    if (N > 0) {

    Console.WriteLine(“N is positive number”);

    else

    if (N==0)

    Console.WriteLine(“N is zero number”);

    else

    Console.WriteLine(“N is negative number”);

    if#1

    if#2


    Nested if statement1

    Nested if statementการควบคุมการไหลของโปรแกรม

    Nested if statement

    *

    int N;

    N = int.Parse(Console.ReadLine());

    if (N >= 0)

    {

    if (N==0)

    Console.WriteLine(“N is zero number”);

    else

    Console.WriteLine(“N is positive number”);

    }

    else

    Console.WriteLine(“N is negative number”);

    if#1

    if#2


    Exercise 1 separated if simple
    Exercise 1: Separated IF (simple)การควบคุมการไหลของโปรแกรม

    *

    1. Have you eaten lunch?

    2. Do you like noodle?

    • if (eaten==true)

    • {Console.WriteLine(“ I’ve already eaten”);}

    • else

    • {Console.WriteLine(“ I’ve not eat yet”);}

    • if (like_noodle==true)

    • {Console.WriteLine(“I like noodle”);}

    • else

    • {Console.WriteLine(“I don’t like noodle”);}


    Exercise 2 related two if full version
    Exercise 2: Related two IF การควบคุมการไหลของโปรแกรม(full version)

    *

    1. Do you like noodle?

    2. If you don’t like noodle, do you like fried rice?

    • if (like_noodle==true)

    • {Console.WriteLine(“I like noodle”);}

    • else

    • { Console.WriteLine(“I don’t like noodle”);

    • if (like_friedrice==true)

    • {Console.WriteLine(“I like friedrice”);}

    • else

    • {Console.WriteLine(“I don’t like friedrice”);}

    • }


    Exercise 2 related two if short version
    Exercise 2: Related two IF การควบคุมการไหลของโปรแกรม(short version)

    *

    • if (like_noodle==true)

    • {Console.WriteLine(“I like noodle”);}

    • else if (like_friedrice==true)

    • {Console.WriteLine(“I like friedrice”);}

    • else

    • {Console.WriteLine(“I don’t like friedrice”);}

    1. Do you like noodle?

    2. If you don’t like noodle, do you like fried rice?


    Exercise 3 nested two if short version
    Exercise 3: Nested two IF การควบคุมการไหลของโปรแกรม(short version)

    *

    1. Do you like noodle?

    1.1 If you like noodle, do you love Sen-Yai?

    1.2 If you don’t like noodle, do you like fried rice?

    • if (like_noodle==true)

    • {Console.WriteLine(“I like noodle”);

    • if (love_SenYai==true)

    • {Console.WriteLine(“I love Sen-Yai”);}

    • else

    • {Console.WriteLine(“I don’t love Sen-Yai”);}

    • }

    • else if (like_friedrice==true)

    • {Console.WriteLine(“I like fried rice”);}

    • else

    • {Console.WriteLine(“I don’t like fried rice”);}


    Nested if statement example

    Nested if statementการควบคุมการไหลของโปรแกรม

    Nested if statement example

    *

    • Write the program which show student’s grade

      • input : score of student

      • output :


    Test iii1
    Test IIIการควบคุมการไหลของโปรแกรม

    Recieving

    if statement

    Selfish Ratio =

    Giving

    • Selfish Ratio


    Test iii2

    2x+10, x ≤ 5การควบคุมการไหลของโปรแกรม

    Nested if statement

    Test III

    f(x) =

    x2+10, 5 < x ≤ 20

    x3+10, x > 20

    *

    • Write the program which calculation value of following function

      • input : value of x

      • output : function value of x according with ...


    Outline10
    Outlineการควบคุมการไหลของโปรแกรม

    *

    • Boolean expression

    • if statement

    • nested if statement

    • Flowchart


    Flowchart symbols overview

    Flowchartการควบคุมการไหลของโปรแกรม

    Flowchart Symbols Overview

    *

    • Graphical representation

    Terminator

    Process

    Input/output

    Condition

    Connector

    Flow line


    Program flowchart example

    Flowchartการควบคุมการไหลของโปรแกรม

    Start

    End

    Program Flowchart Example

    *

    Statement1

    Statement2

    Statement3

    Statement4


    If statement flowchart

    Flowchartการควบคุมการไหลของโปรแกรม

    Start

    End

    Condition

    if statement flowchart

    *

    statement1;

    if (condition)

    statement2; //true

    else{

    statement3; //false

    }

    statement4;

    Statement1

    true

    false

    Statement2

    Statement3

    Statement4


    If statement flowchart1

    Console.การควบคุมการไหลของโปรแกรมWrite(“Go”);

    Start

    End

    n=int.Parse(Console.ReadLine());

    n>0

    if statement flowchart

    *

    n= int.Parse(Console.ReadLine());

    if (n>0)

    n= 2*n+5;

    else{

    Console.Write(“Go”);

    n = n%4;

    }

    false

    true

    n=2*n+5;

    n=n%4;


    Any question2
    Any question?การควบคุมการไหลของโปรแกรม