Intro to the Syntax - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Intro to the Syntax

Description:

Intro to the Syntax – PowerPoint PPT presentation

Number of Views:164
Avg rating:3.0/5.0
Slides: 21
Provided by: bhi85
Category:

less

Transcript and Presenter's Notes

Title: Intro to the Syntax


1
Intro to the Syntax
2
Using Visual C
  • Create the Project .dsp a text file
    containing the names and locations of all files
    used for the program.
  • Create the Workspace the environment where all
    of the files in your project are held a folder
    containing one or more projects and .dsw file.
  • Create the Source File the file that contains
    the C code for your program (add to project) .

3
Comments
  • Two Styles
  • // - the remainder of the line will be ignored by
    the compiler
  • / / - all of the text between the / and /
    will be ignored by the compiler (may be more
    than a single line)

4
Preprocessor Directive
  • includeltiostreamgt - we will use this at the
    beginning of every source file that we create in
    which we would like to use the cin and cout
    objects to read input from the keyboard and
    output to the screen

5
The main Function
  • int main()
  • return 0
  • main the name of the function equivalent to
    CONTROL in C
  • int the return type of the main function
  • () the (empty) parameter list for the main
    function
  • return 0 the main function returns a 0 to
    signify successful program completion

6
The cout Object and Stream Insertion Operator
  • Stream Insertion Operator ltlt used to insert
    data into the output stream
  • cout object in the iostream header file used to
    output data to the screen
  • Example cout ltlt Hello
  • Place the following preprocessor directive in all
    programs which use the cout object.
  • includeltiostreamgt

7
The cout Object and Stream Insertion Operator
  • Place the following line in all programs which
    use the cout object.
  • using stdcout
  • You can use the following statement to include
    all members of the std namespace... however this
    will also include things that you dont need!
  • using namespace std

8
Escape Sequences
  • \n Go to the next line
  • \r Return to the beginning of the current
    line
  • \t Tab to the next default tab stop
  • \a Ring the system bell
  • \ Used to output a
  • \\ - Used to output a \

9
The endl stream manipulator
  • like \n, but different
  • not an escape sequence dont put in quotes
  • flushes the output stream output will be
    generated immediately
  • The endl stream manipulator is also a member of
    the std namespace , so place
  • using stdendl in programs that use it

10
Variable Data Types
  • int integers (positive and negative whole
    numbers)
  • float real numbers
  • double real numbers, more space than float
  • char any ASCII character
  • bool logical, true or false

11
Declaring a Variable
  • DataType variablename
  • This sets aside a memory location to hold the
    variable
  • The variable name must be a valid identifier
  • No value will be placed in the variable

12
Valid Identifiers
  • identifier name for a variable, function or
    constant. May be any combination of alphanumeric
    characters and the underscore that does NOT begin
    with a numeral and is not a C keyword.
  • Note C is CASE SENSITIVE!
  • Valid identifiers Joe, x, august1st,
    _underscore,OneMore
  • Invalid identifiers 4thOfJuly, c, int,
    one more

13
Initializing Variables
  • initialize giving a value before use.
    Example x 3
  • assignment operator () commutes from right to
    left. The value on the LHS will be changed.
  • cascading assignment operators commutes from
    right to left
  • initializing and declaring in one statement
  • Example int x 3

14
Arithmetic Operations
  • , /, , ,
  • Order of Operations
  • Operations are type dependent
  • 1 / 4 - 0
  • 1 / 4.0 - 0.25
  • 1.0 / 4 - 0.25
  • Note You can put an arithmetic operation in a
    cout statement. For example, cout ltlt x/2
    will print the value of x/2.

15
Assignment Statements
  • Commute right to left
  • Example x y z 4
  • Order of Operations must be followed

16
The cin Object and the Stream Extraction Operator
  • stream extraction operator - gtgt used to obtain
    data from the input stream
  • cin - object in the iostream header file. Used to
    obtain input from the keyboard. The value of the
    variable on the RHS will be replaced
  • Example cin gtgt x
  • The cin object is also a member of the std
    namespace, so place using stdcin in files that
    use this object.

17
If statements
  • IF selection structure
  • if ( logical expression )
  • True Action(s)
  • Ex if ( logical expression )
  • cout ltlt True ltlt endl
  • Braces are unnecessary if only one action
    occurs on the true side of the decision.

if (logical expression)
Print True
18
Relational operators
  • lt, lt, gt, gt, , !
  • Each relational operator returns a logical value
  • Example
  • if ( x gt 10 )
  • cout ltlt x is big ltlt endl

19
Terminology
  • Syntax Error error in usage of the language
    (like a grammatical error). Our compiler will
    catch our syntax errors.
  • Logic Error syntactically correct code that
    does not perform the desired task.
  • Run-time Error a mathematical error in your code

20
Additional Notes on Writing C Programs
  • Portability Tip Dont pass column 80 in your
    source code
  • When turning in your program attach the source
    code (.cpp file) to an email
Write a Comment
User Comments (0)
About PowerShow.com