C - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

C

Description:

End statements with semi-colons; double equal signs to test ... Semi-colons at end of statements. Curley brackets define blocks. Functions called 'Methods' ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 28
Provided by: busi378
Category:

less

Transcript and Presenter's Notes

Title: C


1
C Language
  • BCS 2203
  • Web Application Development
  • Nor Azhar Bin Ahmad

2
Overview
  • C Basics
  • Variables
  • Datatype Conversion
  • Control Structures
  • Methods

3
C Basics
  • Many similarities to PHP
  • Case sensitive
  • Curly brackets
  • End statements with semi-colons
  • double equal signs to test equality
  • if (i 3)
  • Comments
  • // single line
  • / Multi-line
  • comment like this /

4
C Basics
  • Classes in class library all start with upper
    case
  • Properties methods too
  • Response.Write()
  • Request.Querystring()
  • Statements are lower case
  • if (
  • return
  • foreach (
  • false

5
Variables
  • Declaring Variables
  • .NET languages are strongly typed
  • PHP ASP loosely typed
  • Strongly typed
  • A variable accepts only one data type
  • Type is explicitly declared when variable is
    declared
  • Advantage
  • Less error prone
  • Compiler more likely to catch programming errors
  • Disadvantage
  • Many explicit data type conversions

6
Variables
  • Declaring Variables
  • string strName
  • int intAge
  • Declare assign value
  • string strName Joe
  • int intAge 6

7
Variables
  • Commonly Used C Data Types

8
Variables
  • Variables are objects
  • Have properties methods
  • Defined in System namespace
  • String documentation

9
Variables
  • Example
  • C declaration
  • string Fname joseph
  • Fname is string object
  • Use ToUpper method
  • Fname Fname.ToUpper()

10
Concatenation
  • Concatenate strings with
  • string Name lastName , firstName
  • End of statement
  • Semicolen

11
Datatype Conversion
  • C strongly typed language
  • Data types must be explicitly converted
  • Several ways to re-cast data types
  • Convert Class
  • most robust
  • easy to use

12
Datatype Conversion
  • Convert Class
  • decimal TaxRate Convert.ToDecimal(8.15)

13
Datatype Conversion
  • Server controls all return data type string
  • double TaxRate tbTaxRate.Text
  • Compiler Error Message CS0029 Cannot implicitly
    convert type 'string' to 'double'
  • Re-cast to type double
  • Double TaxRate Convert.ToDouble(tbTax.Text)
  • Example CSharpDataType.aspx (source)

14
Outline
  • C Basics
  • Variables
  • Datatype Conversion
  • Control Structures
  • Methods

15
Control Structures
  • Syntax similar to PHP, Java, C, C,
  • Curley brackets for statement blocks
  • if (condition)
  • //Statement block
  • else
  • //statement block

16
Control Structures
  • Curley brackets not needed for single
    statements
  • if (condition)
  • statement 1
  • else
  • statement 2

17
Control Structures
  • Testing conditions
  • Equality
  • double equal sign
  • if (age 3)
  • Inequality
  • if (age gt 3)
  • Negate
  • if (age ! 3)
  • if (! Page.IsPostBack)
  • AND / OR
  • and
  • or

18
Control Structures
  • Control Statements
  • if
  • for
  • foreach
  • while

19
Control Structures
  • 1. if else
  • if (condition)
  • //do something
  • else if (condition)
  • //do something
  • else
  • //do something else

20
Control Structures
  • 2. for statement
  • increments a counter variable
  • may declare within statement
  • for (int i 0 i lt 10 i)
  • lblMessage.Text i "ltbrgt"

21
Control Structures
  • 3. foreach loop
  • iterate through collections
  • arrays, controls, cookies, etc.
  • int intArray 3, 5, 7, 9, 11
  • foreach (int item in intArray)
  • Response.Write("Age" item "ltbrgt")

22
Control Structures
  • 4. while loop
  • int i 0
  • while (i lt 5)
  • i 1
  • Response.Write("i " i "ltbrgt")

23
Overview
  • C Basics
  • Variables
  • Datatype Conversion
  • Control Structures
  • Methods

24
Methods
  • Also known as
  • Subroutines in VB
  • Functions in PHP
  • Chunks of code that do something useful
  • Pass parameters in, return a result
  • int myDoubler(int intValue)
  • return intValue 2

25
Methods
  • return void if no value returned
  • void empty
  • void page_load()
  • Response.Write(Hi Mom)

26
Example
  • FontPickerCSharp.aspx
  • Output
  • Source

27
Summary
  • C syntax follows PHP (C-like) conventions
  • Case sensitive
  • Semi-colons at end of statements
  • Curley brackets define blocks
  • Functions called Methods
  • Explicitly convert data types
  • No changes to class library
  • Classes start with upper case
  • Pick up syntax quickly
  • Visual Studio big help
Write a Comment
User Comments (0)
About PowerShow.com