SlideShare uma empresa Scribd logo
1 de 20
Baixar para ler offline
Chapter 2 :
Basic Element of Programming Language and
           Sequential Structure
Lecture Outline

Basic Element of Computer Program
  - General Overview

Data Manipulation
  - Standard data type (int, float, double, char, char[ ])
  - Variable

Computer Statement
  - Statement (constant, identifier)
  - Input/output statement
  - Arithmetic expression
  - Assignment concept
Basic Element of Computer Program

               Problem Solving using Computer



     Instruction/Steps                              2         Data

                                                                     Input /output

    Comp. Statements                                         Variable

                                                                      •Data type
                                                                      •Predefine/user define
2                3                       4                            •Identifier
Sequence             Selection               Iteration                •Size
                                                                      •Range
                                                                      •Lifespan
                                                                      •Scope
                                          •Independent
                                          •Method          Functions          5

                          Procedural Programming vs      Comp. Program
                          Object-oriented Programming
                                                                              6
Data Manipulation

Types of data :
1.   Input data – key in by the user (need to solve the problem)
2.   Output data – process by the program and display to the user
3.   Temporary data – used by the program in the process of obtaining
     the output data.

 Data is presented by a variable in a computer program.
 Variable is a location in the computer’s memory where a value can be stored
 for use by a program.

 Elements of the variable :
    Data Type (Pre Defined / User Defined)
    Lifespan – How long will the variable exist?
    Scope –Where can the variable being used?
    Identifier – Rules in using identifier
Data Manipulation (cont.)
Pre Define Data Type (Size, modifier and range)
        Example : int, float, char and double

    Data Type      Modifiers          Size (bytes)   Range
    int            Short, unsigned            2      0 to 65,535
                   Short, signed              2      -32,768 to 32, 767

                   Long, unsigned             4      0 to
                                                     4,294,967,295
                   Long, signed               4      -2,147,483,683 to
                                                     2,147,483,687
    char                                      1      256 values
    float                                     4      1.2e-38 to 3.4e38

    double                                    8      2.2e-308 to
                                                     1.8e308



 User Defined Data type – defined by user
        Example: object and array
Computer Statement

Program is a list of instruction that represented by the computer statement.

Language element than can be used in constructing high-level language programming form
computer statement is called as Token.

6 types of token in C++ programming language :
   Reserved word
   Identifier
   Constant
   String Literal
   Punctuation
   Operator
Computer Statement (cont.)
 Type of
 Token                         Description                          Example

Reserved     Word that has a special meaning to compiler        main( ), int,
word                                                            strcpy
             Must be typed in the correct location
             Must be spell correctly (lower case letter)
             Must be used in the right context


             Name which is used in a computer program
Identifier   Other than reserved word                           Salary, SUM,
             Mainly to name variables and function              PRO_10
             Rules :
                  consists of letters A..Z, a..z, 0..9, and _
                 1st character must be a letter
                  Case sensitive
                  length : < 32 character, recommended
                  3 – 8 characters
                   Meaningful name
Computer Statement (cont.)

Type of Token                  Description                        Example

Constants          Item with a fixed value (not be changed
                   in any statement)
                   Literal Constant – value typed directly,    int pie = 3.142
                  wherever it is needed
                   Symbolic Constant – represented by a        #define max 10.9
                   name using a preprocessor directive of      const int size = 5
                   keyword const


String Literals   Sequence of character surrounded by
                                                              “ n The minimum
                  double quotation marks                      value is :”
                  String literals may contain printable
                  characters as a, n.
Computer Statement (cont.)
 Type of
 Token                      Description                             Example

Punctuator   Separators in C++                                [ ], ( ), ;, *, #
              To limit the various syntactical units in
             programming language


             Result in computation or action when           +, -, *, <>, !=
Operator     applied to variables or other elements in an
             expression
             Operator act on operand
             Unary operator – operator that require one      A++, C—
              operand                                         Total = sum + salary
             Binary operator – operator that require two     cout << (grade > 60 ?
              operand                                         “ Passed” : “Failed”)
             Trenary operator – operator that required 3
              operand (conditional operator (?:)
Computer Statement (cont.)

Several token will form a Statement.
Statement is an instructions to the computer.


Computer statement is a specification of an action to be executed by the
computer. It is cause the processor to do something.


Example :
   An input statement will input value and placed it to a variable
   An output statement will print message or result to the user on the computer
   screen

Compound statement is a list of statement enclosed in braces { } that can
contain declaration and any type of computer statement.
Computer Statement (cont.)
Types of computer statement
  Input statement
  Process statement
  Output statement

Input statement – read the value for one or more variable from the input/user.
      Format : Pre Defined data type variable
                cin >> variable1 >> variable2 …. >>variablen;


      Example :
                cin>> first >> middle >> last;
Input Statement
Input statement for user defined data type variable
  Example: array – use getline command
              string     name;
              char       name[20];
   Format :
   // I) use the string data type
              getline (cin, variable_name)
              or
   // II) user know the maximum number of characters will be used
              cin .getline (variable_name, number_of_characters);
   Example
              getline(cin, name)
              cin.getline(name,20)
Output Statement

Output statement - print the value of one or more expressions.

Format :
  cout << expression1 << expression2 << … << expressionn;

Example :
       cout << ringgit;
       cout << “ Total value : “ << ringgit<<endl;

 Print format :
             endl – cursor go to next line
             n - cursor go to next line
             t - the print out will tab the printout
             etc ..
Process Statement

Process statement – to ask processor to an action
Types of process statement :
   Assignment statement
   Function call statement

Assignment statement – to store the given value or value of an
expression in a variable

            Format :
                    variable = expression;
            Example :
                    sum = ringgit * 0.01 ;
                    ringgit = 100;
Assignment statement

Assignment statement also can use to the user defined data type such as
array of character (string copy – strcpy)


Purpose – to copy the content from one string variable to other string
variable (and make sure the size of the variable are same)


   Format:
             strcpy (new_variable_name,old_variable_name)


   Example:
             char      old[20], new[20];
             strcpy (new, old)
Function Call statement


Function call statement- used to execute the statement in a particular
function.
Purpose : The result of calling a function and supplying the values for the
function parameters.

    Format :
           function_name( expression1, expression2,…
                     expressionn);
    Example:
           cout << calculatepower( ); // with output statement
           cout<<pow ( z+y, n);                / /with parameter
           value =sqrt(x);
           total = calculate ( x, y ); // with assignment statement
Expression Concepts


Programmers should know how to construct the expression and how to get the
value.
The expression should syntactically correct and meaningful combination of
operators and operand.
Type of expression
        Regular expression
        Arithmetic expression
        Logical Expression (discuss in chapter 3)

Regular expression – describing numbers and denotes any digit
between 0 – 9.
   Example : x = 20;
Arithmetic Expression
Arithmetic expression – using arithmetic operator such as +, -, / etc
Example :
   sum = no1 + no2
   total = (no1 – 10) + ( no3 – 20)

    C++          Arithmetic    Algebraic expression    C++ expression
  Operation      Operator
Addition              +                   F+5              F+5
Subtraction           -                   P-C              P-C
Multiplication        x                  BxM               P*M

Division              /               X / Y or X ÷ Y       X/Y
Modulus              %                  R mod S            R%S
Modulus - operator must applied to integer only give the remainder
           of the integer division
Arithmetic Expression (cont.)

           Precedence of arithmetic operators

Operator        Operation             Order of evaluation

   ()          Parentheses     Evaluated first, left to right
*, /, or %    Multiplication Evaluated last, if there are
               Division      several, left to right.
               Modulus
  + or -        Addition       Evaluated last, if there are
               Subtraction     several, left to right.
Sequential Structure
 Sequence structure – the computer executes C++ statements one after the
 other in order in which they are written.


                       Sequential Structure



Input Statement        Process Statement           Output Statement




         Assignment                     Function Call
         statement                      Statement

Mais conteúdo relacionado

Mais procurados

Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
hermiraguilar
 

Mais procurados (20)

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
History of C Programming Language
History of C Programming LanguageHistory of C Programming Language
History of C Programming Language
 
Data Structure - Elementary Data Organization
Data Structure - Elementary  Data Organization Data Structure - Elementary  Data Organization
Data Structure - Elementary Data Organization
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Presentation on C programming language
Presentation on C programming languagePresentation on C programming language
Presentation on C programming language
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
 
History of c
History of cHistory of c
History of c
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program execution
 
Python Programming - Files & Exceptions
Python Programming - Files & ExceptionsPython Programming - Files & Exceptions
Python Programming - Files & Exceptions
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Rules for Variable Declaration
Rules for Variable DeclarationRules for Variable Declaration
Rules for Variable Declaration
 
pdf c programming language.pdf
pdf c programming language.pdfpdf c programming language.pdf
pdf c programming language.pdf
 

Destaque

User defined data type
User defined data typeUser defined data type
User defined data type
Amit Kapoor
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
Deepak Singh
 
Chapter 2 Body Coordination
Chapter 2 Body CoordinationChapter 2 Body Coordination
Chapter 2 Body Coordination
Yuhana Ali
 

Destaque (20)

Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
User defined data type
User defined data typeUser defined data type
User defined data type
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Data type
Data typeData type
Data type
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Data types
Data typesData types
Data types
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 
Chapter 2 Body Coordination
Chapter 2 Body CoordinationChapter 2 Body Coordination
Chapter 2 Body Coordination
 
Data and its types by adeel
Data and its types by adeelData and its types by adeel
Data and its types by adeel
 
Intro to C++ - language
Intro to C++ - languageIntro to C++ - language
Intro to C++ - language
 
functions of C++
functions of C++functions of C++
functions of C++
 
Data types
Data typesData types
Data types
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
C++ programs
C++ programsC++ programs
C++ programs
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 

Semelhante a Chapter 2 basic element of programming

C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
Abhinav Vatsa
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
valarpink
 

Semelhante a Chapter 2 basic element of programming (20)

Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
C intro
C introC intro
C intro
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
What is Data Types and Functions?
What is Data Types and Functions?What is Data Types and Functions?
What is Data Types and Functions?
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
Cpprm
CpprmCpprm
Cpprm
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Chap 2(const var-datatype)
Chap 2(const var-datatype)Chap 2(const var-datatype)
Chap 2(const var-datatype)
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
 
C programming language
C programming languageC programming language
C programming language
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 

Chapter 2 basic element of programming

  • 1. Chapter 2 : Basic Element of Programming Language and Sequential Structure
  • 2. Lecture Outline Basic Element of Computer Program - General Overview Data Manipulation - Standard data type (int, float, double, char, char[ ]) - Variable Computer Statement - Statement (constant, identifier) - Input/output statement - Arithmetic expression - Assignment concept
  • 3. Basic Element of Computer Program Problem Solving using Computer Instruction/Steps 2 Data Input /output Comp. Statements Variable •Data type •Predefine/user define 2 3 4 •Identifier Sequence Selection Iteration •Size •Range •Lifespan •Scope •Independent •Method Functions 5 Procedural Programming vs Comp. Program Object-oriented Programming 6
  • 4. Data Manipulation Types of data : 1. Input data – key in by the user (need to solve the problem) 2. Output data – process by the program and display to the user 3. Temporary data – used by the program in the process of obtaining the output data. Data is presented by a variable in a computer program. Variable is a location in the computer’s memory where a value can be stored for use by a program. Elements of the variable : Data Type (Pre Defined / User Defined) Lifespan – How long will the variable exist? Scope –Where can the variable being used? Identifier – Rules in using identifier
  • 5. Data Manipulation (cont.) Pre Define Data Type (Size, modifier and range) Example : int, float, char and double Data Type Modifiers Size (bytes) Range int Short, unsigned 2 0 to 65,535 Short, signed 2 -32,768 to 32, 767 Long, unsigned 4 0 to 4,294,967,295 Long, signed 4 -2,147,483,683 to 2,147,483,687 char 1 256 values float 4 1.2e-38 to 3.4e38 double 8 2.2e-308 to 1.8e308 User Defined Data type – defined by user Example: object and array
  • 6. Computer Statement Program is a list of instruction that represented by the computer statement. Language element than can be used in constructing high-level language programming form computer statement is called as Token. 6 types of token in C++ programming language : Reserved word Identifier Constant String Literal Punctuation Operator
  • 7. Computer Statement (cont.) Type of Token Description Example Reserved Word that has a special meaning to compiler main( ), int, word strcpy Must be typed in the correct location Must be spell correctly (lower case letter) Must be used in the right context Name which is used in a computer program Identifier Other than reserved word Salary, SUM, Mainly to name variables and function PRO_10 Rules : consists of letters A..Z, a..z, 0..9, and _ 1st character must be a letter Case sensitive length : < 32 character, recommended 3 – 8 characters Meaningful name
  • 8. Computer Statement (cont.) Type of Token Description Example Constants Item with a fixed value (not be changed in any statement) Literal Constant – value typed directly, int pie = 3.142 wherever it is needed Symbolic Constant – represented by a #define max 10.9 name using a preprocessor directive of const int size = 5 keyword const String Literals Sequence of character surrounded by “ n The minimum double quotation marks value is :” String literals may contain printable characters as a, n.
  • 9. Computer Statement (cont.) Type of Token Description Example Punctuator Separators in C++ [ ], ( ), ;, *, # To limit the various syntactical units in programming language Result in computation or action when +, -, *, <>, != Operator applied to variables or other elements in an expression Operator act on operand Unary operator – operator that require one A++, C— operand Total = sum + salary Binary operator – operator that require two cout << (grade > 60 ? operand “ Passed” : “Failed”) Trenary operator – operator that required 3 operand (conditional operator (?:)
  • 10. Computer Statement (cont.) Several token will form a Statement. Statement is an instructions to the computer. Computer statement is a specification of an action to be executed by the computer. It is cause the processor to do something. Example : An input statement will input value and placed it to a variable An output statement will print message or result to the user on the computer screen Compound statement is a list of statement enclosed in braces { } that can contain declaration and any type of computer statement.
  • 11. Computer Statement (cont.) Types of computer statement Input statement Process statement Output statement Input statement – read the value for one or more variable from the input/user. Format : Pre Defined data type variable cin >> variable1 >> variable2 …. >>variablen; Example : cin>> first >> middle >> last;
  • 12. Input Statement Input statement for user defined data type variable Example: array – use getline command string name; char name[20]; Format : // I) use the string data type getline (cin, variable_name) or // II) user know the maximum number of characters will be used cin .getline (variable_name, number_of_characters); Example getline(cin, name) cin.getline(name,20)
  • 13. Output Statement Output statement - print the value of one or more expressions. Format : cout << expression1 << expression2 << … << expressionn; Example : cout << ringgit; cout << “ Total value : “ << ringgit<<endl; Print format : endl – cursor go to next line n - cursor go to next line t - the print out will tab the printout etc ..
  • 14. Process Statement Process statement – to ask processor to an action Types of process statement : Assignment statement Function call statement Assignment statement – to store the given value or value of an expression in a variable Format : variable = expression; Example : sum = ringgit * 0.01 ; ringgit = 100;
  • 15. Assignment statement Assignment statement also can use to the user defined data type such as array of character (string copy – strcpy) Purpose – to copy the content from one string variable to other string variable (and make sure the size of the variable are same) Format: strcpy (new_variable_name,old_variable_name) Example: char old[20], new[20]; strcpy (new, old)
  • 16. Function Call statement Function call statement- used to execute the statement in a particular function. Purpose : The result of calling a function and supplying the values for the function parameters. Format : function_name( expression1, expression2,… expressionn); Example: cout << calculatepower( ); // with output statement cout<<pow ( z+y, n); / /with parameter value =sqrt(x); total = calculate ( x, y ); // with assignment statement
  • 17. Expression Concepts Programmers should know how to construct the expression and how to get the value. The expression should syntactically correct and meaningful combination of operators and operand. Type of expression Regular expression Arithmetic expression Logical Expression (discuss in chapter 3) Regular expression – describing numbers and denotes any digit between 0 – 9. Example : x = 20;
  • 18. Arithmetic Expression Arithmetic expression – using arithmetic operator such as +, -, / etc Example : sum = no1 + no2 total = (no1 – 10) + ( no3 – 20) C++ Arithmetic Algebraic expression C++ expression Operation Operator Addition + F+5 F+5 Subtraction - P-C P-C Multiplication x BxM P*M Division / X / Y or X ÷ Y X/Y Modulus % R mod S R%S Modulus - operator must applied to integer only give the remainder of the integer division
  • 19. Arithmetic Expression (cont.) Precedence of arithmetic operators Operator Operation Order of evaluation () Parentheses Evaluated first, left to right *, /, or % Multiplication Evaluated last, if there are Division several, left to right. Modulus + or - Addition Evaluated last, if there are Subtraction several, left to right.
  • 20. Sequential Structure Sequence structure – the computer executes C++ statements one after the other in order in which they are written. Sequential Structure Input Statement Process Statement Output Statement Assignment Function Call statement Statement