SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
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

More Related Content

What's hot (20)

PPTX
While , For , Do-While Loop
Abhishek Choksi
 
PPTX
Algorithms and Flowcharts
Deva Singh
 
PPTX
Presentation on Logical Operators
Sanjeev Budha
 
PPTX
C decision making and looping.
Haard Shah
 
PPTX
User defined functions in C
Harendra Singh
 
PPT
Structure of a C program
David Livingston J
 
PPTX
Computer software
RajniKashyap9
 
PPT
Looping statements in Java
Jin Castor
 
PPSX
Data types, Variables, Expressions & Arithmetic Operators in java
Javed Rashid
 
PPTX
Translators(Compiler, Assembler) and interpreter
baabtra.com - No. 1 supplier of quality freshers
 
PDF
C standard library functions
Vaishnavee Sharma
 
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
PPTX
Introduction to Operating System and its Types
sundas Shabbir
 
PPT
1. over view and history of c
Harish Kumawat
 
PPTX
Operators and expressions in C++
Neeru Mittal
 
PPTX
Control statements in c
Sathish Narayanan
 
PPT
Control structure C++
Anil Kumar
 
PPTX
Decision making and branching in c programming
Priyansh Thakar
 
PDF
Datatypes in python
eShikshak
 
PPTX
Looping statements in C
Jeya Lakshmi
 
While , For , Do-While Loop
Abhishek Choksi
 
Algorithms and Flowcharts
Deva Singh
 
Presentation on Logical Operators
Sanjeev Budha
 
C decision making and looping.
Haard Shah
 
User defined functions in C
Harendra Singh
 
Structure of a C program
David Livingston J
 
Computer software
RajniKashyap9
 
Looping statements in Java
Jin Castor
 
Data types, Variables, Expressions & Arithmetic Operators in java
Javed Rashid
 
Translators(Compiler, Assembler) and interpreter
baabtra.com - No. 1 supplier of quality freshers
 
C standard library functions
Vaishnavee Sharma
 
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
Introduction to Operating System and its Types
sundas Shabbir
 
1. over view and history of c
Harish Kumawat
 
Operators and expressions in C++
Neeru Mittal
 
Control statements in c
Sathish Narayanan
 
Control structure C++
Anil Kumar
 
Decision making and branching in c programming
Priyansh Thakar
 
Datatypes in python
eShikshak
 
Looping statements in C
Jeya Lakshmi
 

Viewers also liked (19)

PDF
Chap 2 c++
Widad Jamaluddin
 
PPT
User defined data type
Amit Kapoor
 
PDF
C++ Programming
Rounak Samdadia
 
PPT
Data type
myrajendra
 
PDF
Chapter 2 - Structure of C++ Program
Deepak Singh
 
PPT
C++ Language
Syed Zaid Irshad
 
PPTX
Data types
Syed Umair
 
PDF
+2 Computer Science - Volume II Notes
Andrew Raj
 
PPTX
Chapter 2 Body Coordination
Yuhana Ali
 
PPTX
Data and its types by adeel
Ayaan Adeel
 
PDF
Intro to C++ - language
Jussi Pohjolainen
 
PPTX
functions of C++
tarandeep_kaur
 
PPTX
Data types
Zahid Hussain
 
PPTX
Basic c++ programs
harman kaur
 
PPTX
Introduction to C Programming
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
PPTX
Concept Of C++ Data Types
k v
 
PPTX
Data Types and Variables In C Programming
Kamal Acharya
 
PDF
C++ programs
Mukund Gandrakota
 
PPT
Basics of c++ Programming Language
Ahmad Idrees
 
Chap 2 c++
Widad Jamaluddin
 
User defined data type
Amit Kapoor
 
C++ Programming
Rounak Samdadia
 
Data type
myrajendra
 
Chapter 2 - Structure of C++ Program
Deepak Singh
 
C++ Language
Syed Zaid Irshad
 
Data types
Syed Umair
 
+2 Computer Science - Volume II Notes
Andrew Raj
 
Chapter 2 Body Coordination
Yuhana Ali
 
Data and its types by adeel
Ayaan Adeel
 
Intro to C++ - language
Jussi Pohjolainen
 
functions of C++
tarandeep_kaur
 
Data types
Zahid Hussain
 
Basic c++ programs
harman kaur
 
Concept Of C++ Data Types
k v
 
Data Types and Variables In C Programming
Kamal Acharya
 
C++ programs
Mukund Gandrakota
 
Basics of c++ Programming Language
Ahmad Idrees
 
Ad

Similar to Chapter 2 basic element of programming (20)

PPTX
FUNDAMENTAL OF C
KRUNAL RAVAL
 
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
SowmyaJyothi3
 
PPTX
Chapter 2
abdulhalimnapiah
 
PDF
C basics
Learn By Watch
 
PPT
Computer Programming- Lecture 3
Dr. Md. Shohel Sayeed
 
PPT
Getting started with c++
K Durga Prasad
 
PPT
Getting started with c++
Bussines man badhrinadh
 
PPT
C the basic concepts
Abhinav Vatsa
 
PPT
Chtp408
giovanniveitch
 
PPTX
Introduction of Basics of c programming.pptx
priyankabonde1998
 
PDF
Lecture 4 constants_variables
eShikshak
 
PPTX
c programming session 1.pptx
RSathyaPriyaCSEKIOT
 
PDF
POLITEKNIK MALAYSIA
Aiman Hud
 
PPTX
Introduction to C language programming.pptx
OVIDMAMAH
 
PDF
Introduction of c_language
SINGH PROJECTS
 
PPTX
Module 1:Introduction
nikshaikh786
 
PDF
C Programming
Adil Jafri
 
PPT
Karakter dan String
Fernalia Halim
 
PPTX
Introduction to C Programming - R.D.Sivakumar
Sivakumar R D .
 
PDF
Chapter 13.1.3
patcha535
 
FUNDAMENTAL OF C
KRUNAL RAVAL
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
SowmyaJyothi3
 
Chapter 2
abdulhalimnapiah
 
C basics
Learn By Watch
 
Computer Programming- Lecture 3
Dr. Md. Shohel Sayeed
 
Getting started with c++
K Durga Prasad
 
Getting started with c++
Bussines man badhrinadh
 
C the basic concepts
Abhinav Vatsa
 
Introduction of Basics of c programming.pptx
priyankabonde1998
 
Lecture 4 constants_variables
eShikshak
 
c programming session 1.pptx
RSathyaPriyaCSEKIOT
 
POLITEKNIK MALAYSIA
Aiman Hud
 
Introduction to C language programming.pptx
OVIDMAMAH
 
Introduction of c_language
SINGH PROJECTS
 
Module 1:Introduction
nikshaikh786
 
C Programming
Adil Jafri
 
Karakter dan String
Fernalia Halim
 
Introduction to C Programming - R.D.Sivakumar
Sivakumar R D .
 
Chapter 13.1.3
patcha535
 
Ad

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