SlideShare a Scribd company logo
1 of 11
F1001 PROGRAMMING FUNDAMENTALS




    UNIT

      4

PROGRAMMING OPERATORS

 Assignment Operator
   Mathematical Operators
 Relational Operators
   Logical Operators




                                                59
F1001 PROGRAMMING FUNDAMENTALS


CONDITIONAL STATEMENT
•   After a program has received data from the users and the data is been held by a variable, the next step
    is to operate the variable.
•   The operation done to the variable can be one or more.
•   Specific instructions used in the variable operation are called operators.
•   Operators are symbols used to represent computer operations.
•   These symbols have one or more special characters defined by programming language.
•   The operations that can be operated on variables are:
    1.   Arithmetic operation.
             Example: Addition, multiplication, division, subtraction
    2.   Assignment operation.
             Example: assign or change value to variable.
    3.   Relational and Logical operation.
             Example: Testing truth for expression (example: a > b).


ASSIGNMENT OPERATOR
   In certain languages, for example in C language, equal (=) symbol is used as symbol in assignment
    operations.
   This operator is used to change variable’s value.
   Example:
         x=5
    So 5 are assigned to variable x.
   Example:
         x=y=0
    The result from the operations: variable x and variable y are assigned to the same value, which is 0.
   One type of assignment operator is the compound assignment operator.
   Compound assignment operator is the operator that is combined with             –, +, %, *, / and other
    operations.
   Examples:
                         Ungkapan / Expression                 Maksud / Meaning
                                a += b                             a=a+b
                                  a *= 5                             a=a*5
                                  a –= 2                             a=a–2
                                  a %= 3                            a=a%3




                                                                                                            60
F1001 PROGRAMMING FUNDAMENTALS


   Example:
        If a = 2, b = 4
        a += b (means a = a + b)
        Result: a = 6


        If b = 5
        b -= 2 (means b = b – 2)
        Result: b = 3


   Variable representative in memory:


            If a = 2, b = 4                                      If a = 2, b = 5
            a += b (means a = a + b)                             b –= a (means b + b – a)
            Result: a = 6                                        Result: b = 3
            Before:                                              Before:
                        a     2         b    4                              a      2           b   5
                              100            200                                   100             200
            After:                                               After:
                        a     6         b    4                              a      2           b   3
                            100              200                                 100               200




ARITHMETIC OPERATOR
   Arithmetic operation is the calculation operation that can be done on data in variables.
   In C language, the 5 arithmetic operators are:
                              Symbol                 Operators
                                +                      Add
                                    –                 Subtract
                                    *                 Multiply
                                    /                  Divide
                              %                       Modulus
   Modulus operator is used to get the balance of division of 2 numbers.
   Example:
        5 % 3 is 2
        10 % 6 is 4
   Arithmetic expressions are normally made of variables with arithmetic operator.


   Example:



                                                                                                         61
F1001 PROGRAMMING FUNDAMENTALS


        a+b/2
   This expression will be evaluated based on priority defined by the programming language.


Increment and Decrement Operation
   Variables usually can be increased or decreased by 1.
   For example, C language and Java has provided operator to add or subtract 1 from variable values.
   The symbols are:
                          Symbol                            Operation
                            ++                               Add 1
                           ––                               Subtract 1

   Example:
        x++ can also be written as x = x + 1
        y– – can also be written as y = y – 1
   However, this operation cannot be used for constants.
   For example: 5++ is invalid.
   Symbols can be written before or after the variable.
   Example:

              Statement     Flow of Execution                      Explanation
    a = 10;                      a = 10;           Variable b will be assigned with value a=10,
              b = a++;               b = a;        then the value of a will be added to 1
                                   a = a + 1;
               a = 10;              a = 10;        Variable b will be assigned with value a=11,
              b = ++a;             a = a + 1;      after the value of a had been added to 1
                                     b = a;




                                                                                                        62
F1001 PROGRAMMING FUNDAMENTALS




   Variable representative in memory:

                                                               Assume:
                                                                         a = 10;
                                                                         b = a++;



                                   10


                               A           B




                          Before Operation




                                                                   11        10


                                                               A             B




                                                           After Operation



             Variable b is assigned with value a = 10, then value of a will be increased by 1.




                                                                                                 63
F1001 PROGRAMMING FUNDAMENTALS




Relational Operator
   Relational operator can be divided into 2 groups:
    1.   Not Equivalent Group
         -   Not equal is an expression that consists relational operator that will return the value of 1, if
             the relation is true and 0 if it is false.
         -   Symbols:
                  Symbol                            Operators
                    >                              Greater than
                      >=                  Greater than or equals to
                       <                             Less than
                      <=                       Less than or equals to


         -   Example:
                                         Variable c will have 1 as value
             a = 10, b = 2;
                                         because relation (10 > 2) is true,
             c = a > b;
                                         while variable d will have 0 because
             d = (b * 3) > a;
                                         relation (6 < 0) is false.




    2.       Equivalent Group
         -   Equivalent is an expression that consists of relational operator that will return 1 if the relation
             is true and 0 if false.
         -   Symbols:
                           Symbol                      Operators
                             ==                        Equal to
                             !=                       Not equal to



         -   Example:
                                       Variable c will have 1 as value
             a = 10;
                                       because relation (10! = 2) is true, and
             b = 2;
                                       variable d will have 1 as value
             c = a != b;
                                       because relation (2 * 5 == 10) is also
             d = (b * 5) == a;
                                       true.


                                                                                                            64
F1001 PROGRAMMING FUNDAMENTALS




Logical Operator
   In certain languages, for example C language, it has 3 types of logical operators:
                    Symbol                     Operators
                      &&                         AND
                          ||                      OR
                          !                      NOT



   Table below shows the definition of C languages for logical operator:
                          P                Q               P&&Q       P||Q    !P         !Q
                          0                0                 0         0       1          1
                          0             NOT 0                0         1       1          0
                    NOT 0                  0                 0         1       0          1
                    NOT 0               NOT 0                1         1       0          0



   Example 1:
        If x = 2 and y = 3
                Ungkapan / Expression             Nilai a / Value a
             a = x && y                                    1
             a = (x > 0) && (y > 0)                        1
             a = (x < y) && (y == 0)                       0
             a = x || y                                    1
             a = (x !0) || (y != 0)                        1
             a = (x == y) || (y == 0)                      0
             a = ! (x == y)                                1
             a = ! (x < y)                                 0


   Example 2:
        If a = 4, b = 6, c = 8, d = 4
            Ungkapan / Expression              Nilai x / Value x
          x = a == d                                    1
          x = a == b                                    0
          x=b<c                                         1
          x=b>c                                         0
          x=c>a                                         1
          x=d<a                                         0
          x = b >= a                                    1
          x=d>a                                         0




                                                                                              65
F1001 PROGRAMMING FUNDAMENTALS


          x = d <= b                                 1
          x = a != d                                 0
          x = b != c                                 1
          x = c <= b                                 0




OPERATOR COMPOUND
   All operators (for example in C language) can be combined into one expression.
   Notes below shows the priorities set for all operators, that has been discussed before:
                           Operations
                           ()                                  Highest priority
                           !         ++        ––
                           *         /         %
                           +         –
                           <         <=        >      >=
                           ==        !=
                           &&
                           ||
                           =                                   Lowest priority


   Example 1:
    Expression x + y == z && m – n will be defined as below:


             ( x + y ) == z && m – n


             ( x + y ) == z && ( m – n )


             ( ( x + y ) == z ) && ( m – n )


             ( ( ( x + y ) == z ) && ( m – n ) )


        + operation gets highest priority in the expression. Then, followed by – operation
        and then == operation. The last operation done is && because it has the lowest
        priority.




   Example 2:



                                                                                              66
F1001 PROGRAMMING FUNDAMENTALS


             4 + 2 * 3 – (5 / 2)
             4 + 2 * 3 – (2.5)
             4 + 6 – (2.5)
             10 – 2.5
             7.5


   Example 3:
             5*2+5%2
             10 + 5 % 2
             10 + 1
             11


   Example 4:
             10 / 5 + (9 / 3)
             10 / 5 + 3
             2+3
             5


CAST OPERATOR
   Cast operator is an operator used to change the types of expression result.
   For example, to change integer type to float type.
   Example in C Language:
             int i = 9;
             float f;
             f = i / 2;
   From example, variable f will get 4.0. This happened because variable i is integer type, so dividing the
    integer number will cause the decimal part to be cut out of the answer.
   To get an accurate answer, the syntax must be changed to:
             int i = 9;
             float f;
             f = (float) i / 2;
   Therefore, the result 4.5 will be assigned to variable f.




TRACING VARIABLE’S VALUE IN MEMORY



                                                                                                         67
F1001 PROGRAMMING FUNDAMENTALS


•   Each data entered by the users for a variable is stored in memory.
•   Memory is the processing area for Central Processing Unit (CPU).
•   It is a small sequential location storing cells where each of them has a single byte of information.
•   The information may consist program instructions, arithmetic numbers, code that represents text
    character, variable, picture or other data.
•   Memory location is like a letterbox. Each letterbox represents an address and holds 1 byte of information.
•   Each byte has its own address to allow it to be recognised by the CPU.




                                      MEMORY ILLUSTRATION



•   Example:
    Assume that user stores data Aminah in variable Nama.


                                                                      Computer will allocate a special
                                                                      location for variable Name when
                                                                      it is declared by the user. Data
                                                                      Aminah will be stored to the
                                                                      variable Name at location 3 in
                                                                      memory.



•   Let’s assume that user wants to access a data in variable Town at location 37 in memory:



                                                                                                           68
F1001 PROGRAMMING FUNDAMENTALS




          When the user written an
          instruction to access the value in
          variable Town, CPU will detect
          the memory location according
          to the address of the variable.
          When the variable is found, the
          value will be accessed and
          displayed on the user’s screen.




                                               69

More Related Content

What's hot

Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++zeeshan turi
 
C operators
C operatorsC operators
C operatorsGPERI
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operatorsAnuja Lad
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming Kamal Acharya
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++Praveen M Jigajinni
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in CPrabhu Govind
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++Online
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressionsvinay arora
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++Pranav Ghildiyal
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATORrricky98
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in JavaAbhilash Nair
 

What's hot (20)

Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
 
C operators
C operatorsC operators
C operators
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
C Operators
C OperatorsC Operators
C Operators
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 
Labsheet1stud
Labsheet1studLabsheet1stud
Labsheet1stud
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATOR
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 

Similar to Unit 4

Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzationKaushal Patel
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++Neeru Mittal
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptxramanathan2006
 
C operators
C operators C operators
C operators AbiramiT9
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introductionAnanda Kumar HN
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c languageTanmay Modi
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdfMaryJacob24
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tourSwarup Boro
 
Cse lecture-4.1-c operators and expression
Cse lecture-4.1-c operators and expressionCse lecture-4.1-c operators and expression
Cse lecture-4.1-c operators and expressionFarshidKhan
 
C programming operators
C programming operatorsC programming operators
C programming operatorsSuneel Dogra
 

Similar to Unit 4 (20)

Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
operator ppt.ppt
operator ppt.pptoperator ppt.ppt
operator ppt.ppt
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 
C operators
C operators C operators
C operators
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introduction
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
What are operators?
What are operators? What are operators?
What are operators?
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
C++
C++ C++
C++
 
Cse lecture-4.1-c operators and expression
Cse lecture-4.1-c operators and expressionCse lecture-4.1-c operators and expression
Cse lecture-4.1-c operators and expression
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
Report on c
Report on cReport on c
Report on c
 
What is c
What is cWhat is c
What is c
 
C programming operators
C programming operatorsC programming operators
C programming operators
 

More from rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2) rohassanie
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201rohassanie
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1rohassanie
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 

More from rohassanie (20)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 

Recently uploaded

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 

Unit 4

  • 1. F1001 PROGRAMMING FUNDAMENTALS UNIT 4 PROGRAMMING OPERATORS  Assignment Operator  Mathematical Operators  Relational Operators  Logical Operators 59
  • 2. F1001 PROGRAMMING FUNDAMENTALS CONDITIONAL STATEMENT • After a program has received data from the users and the data is been held by a variable, the next step is to operate the variable. • The operation done to the variable can be one or more. • Specific instructions used in the variable operation are called operators. • Operators are symbols used to represent computer operations. • These symbols have one or more special characters defined by programming language. • The operations that can be operated on variables are: 1. Arithmetic operation. Example: Addition, multiplication, division, subtraction 2. Assignment operation. Example: assign or change value to variable. 3. Relational and Logical operation. Example: Testing truth for expression (example: a > b). ASSIGNMENT OPERATOR  In certain languages, for example in C language, equal (=) symbol is used as symbol in assignment operations.  This operator is used to change variable’s value.  Example: x=5 So 5 are assigned to variable x.  Example: x=y=0 The result from the operations: variable x and variable y are assigned to the same value, which is 0.  One type of assignment operator is the compound assignment operator.  Compound assignment operator is the operator that is combined with –, +, %, *, / and other operations.  Examples: Ungkapan / Expression Maksud / Meaning a += b a=a+b a *= 5 a=a*5 a –= 2 a=a–2 a %= 3 a=a%3 60
  • 3. F1001 PROGRAMMING FUNDAMENTALS  Example: If a = 2, b = 4 a += b (means a = a + b) Result: a = 6 If b = 5 b -= 2 (means b = b – 2) Result: b = 3  Variable representative in memory: If a = 2, b = 4 If a = 2, b = 5 a += b (means a = a + b) b –= a (means b + b – a) Result: a = 6 Result: b = 3 Before: Before: a 2 b 4 a 2 b 5 100 200 100 200 After: After: a 6 b 4 a 2 b 3 100 200 100 200 ARITHMETIC OPERATOR  Arithmetic operation is the calculation operation that can be done on data in variables.  In C language, the 5 arithmetic operators are: Symbol Operators + Add – Subtract * Multiply / Divide % Modulus  Modulus operator is used to get the balance of division of 2 numbers.  Example: 5 % 3 is 2 10 % 6 is 4  Arithmetic expressions are normally made of variables with arithmetic operator.  Example: 61
  • 4. F1001 PROGRAMMING FUNDAMENTALS a+b/2  This expression will be evaluated based on priority defined by the programming language. Increment and Decrement Operation  Variables usually can be increased or decreased by 1.  For example, C language and Java has provided operator to add or subtract 1 from variable values.  The symbols are: Symbol Operation ++ Add 1 –– Subtract 1  Example: x++ can also be written as x = x + 1 y– – can also be written as y = y – 1  However, this operation cannot be used for constants.  For example: 5++ is invalid.  Symbols can be written before or after the variable.  Example: Statement Flow of Execution Explanation a = 10; a = 10; Variable b will be assigned with value a=10, b = a++; b = a; then the value of a will be added to 1 a = a + 1; a = 10; a = 10; Variable b will be assigned with value a=11, b = ++a; a = a + 1; after the value of a had been added to 1 b = a; 62
  • 5. F1001 PROGRAMMING FUNDAMENTALS  Variable representative in memory: Assume: a = 10; b = a++; 10 A B Before Operation 11 10 A B After Operation Variable b is assigned with value a = 10, then value of a will be increased by 1. 63
  • 6. F1001 PROGRAMMING FUNDAMENTALS Relational Operator  Relational operator can be divided into 2 groups: 1. Not Equivalent Group - Not equal is an expression that consists relational operator that will return the value of 1, if the relation is true and 0 if it is false. - Symbols: Symbol Operators > Greater than >= Greater than or equals to < Less than <= Less than or equals to - Example: Variable c will have 1 as value a = 10, b = 2; because relation (10 > 2) is true, c = a > b; while variable d will have 0 because d = (b * 3) > a; relation (6 < 0) is false. 2. Equivalent Group - Equivalent is an expression that consists of relational operator that will return 1 if the relation is true and 0 if false. - Symbols: Symbol Operators == Equal to != Not equal to - Example: Variable c will have 1 as value a = 10; because relation (10! = 2) is true, and b = 2; variable d will have 1 as value c = a != b; because relation (2 * 5 == 10) is also d = (b * 5) == a; true. 64
  • 7. F1001 PROGRAMMING FUNDAMENTALS Logical Operator  In certain languages, for example C language, it has 3 types of logical operators: Symbol Operators && AND || OR ! NOT  Table below shows the definition of C languages for logical operator: P Q P&&Q P||Q !P !Q 0 0 0 0 1 1 0 NOT 0 0 1 1 0 NOT 0 0 0 1 0 1 NOT 0 NOT 0 1 1 0 0  Example 1: If x = 2 and y = 3 Ungkapan / Expression Nilai a / Value a a = x && y 1 a = (x > 0) && (y > 0) 1 a = (x < y) && (y == 0) 0 a = x || y 1 a = (x !0) || (y != 0) 1 a = (x == y) || (y == 0) 0 a = ! (x == y) 1 a = ! (x < y) 0  Example 2: If a = 4, b = 6, c = 8, d = 4 Ungkapan / Expression Nilai x / Value x x = a == d 1 x = a == b 0 x=b<c 1 x=b>c 0 x=c>a 1 x=d<a 0 x = b >= a 1 x=d>a 0 65
  • 8. F1001 PROGRAMMING FUNDAMENTALS x = d <= b 1 x = a != d 0 x = b != c 1 x = c <= b 0 OPERATOR COMPOUND  All operators (for example in C language) can be combined into one expression.  Notes below shows the priorities set for all operators, that has been discussed before: Operations () Highest priority ! ++ –– * / % + – < <= > >= == != && || = Lowest priority  Example 1: Expression x + y == z && m – n will be defined as below: ( x + y ) == z && m – n ( x + y ) == z && ( m – n ) ( ( x + y ) == z ) && ( m – n ) ( ( ( x + y ) == z ) && ( m – n ) ) + operation gets highest priority in the expression. Then, followed by – operation and then == operation. The last operation done is && because it has the lowest priority.  Example 2: 66
  • 9. F1001 PROGRAMMING FUNDAMENTALS 4 + 2 * 3 – (5 / 2) 4 + 2 * 3 – (2.5) 4 + 6 – (2.5) 10 – 2.5 7.5  Example 3: 5*2+5%2 10 + 5 % 2 10 + 1 11  Example 4: 10 / 5 + (9 / 3) 10 / 5 + 3 2+3 5 CAST OPERATOR  Cast operator is an operator used to change the types of expression result.  For example, to change integer type to float type.  Example in C Language: int i = 9; float f; f = i / 2;  From example, variable f will get 4.0. This happened because variable i is integer type, so dividing the integer number will cause the decimal part to be cut out of the answer.  To get an accurate answer, the syntax must be changed to: int i = 9; float f; f = (float) i / 2;  Therefore, the result 4.5 will be assigned to variable f. TRACING VARIABLE’S VALUE IN MEMORY 67
  • 10. F1001 PROGRAMMING FUNDAMENTALS • Each data entered by the users for a variable is stored in memory. • Memory is the processing area for Central Processing Unit (CPU). • It is a small sequential location storing cells where each of them has a single byte of information. • The information may consist program instructions, arithmetic numbers, code that represents text character, variable, picture or other data. • Memory location is like a letterbox. Each letterbox represents an address and holds 1 byte of information. • Each byte has its own address to allow it to be recognised by the CPU. MEMORY ILLUSTRATION • Example: Assume that user stores data Aminah in variable Nama. Computer will allocate a special location for variable Name when it is declared by the user. Data Aminah will be stored to the variable Name at location 3 in memory. • Let’s assume that user wants to access a data in variable Town at location 37 in memory: 68
  • 11. F1001 PROGRAMMING FUNDAMENTALS When the user written an instruction to access the value in variable Town, CPU will detect the memory location according to the address of the variable. When the variable is found, the value will be accessed and displayed on the user’s screen. 69