SlideShare a Scribd company logo
FP 201 – Programming
Fundamentals
Operators & Expressions
Learning outcome
By the end of the course, students should be able to:
Define the following operators:
        a. Arithmetic operator
        b. Assignment operator
        c. Increment and decrement operator
        d. Relational operator
        e. Logical operator
Explain operators’ precedence
Explain type conversion
Write expression using operator
Use expression in program
Operators
   Operator                    Action
      -       subtraction (also unary minus)
     +        addition
      *       multiplication
      /       division
     %        modulus division
      --      decrement
     ++       increment
     &&       AND
     ||       OR
      !       NOT
Operators
Is a symbol that instructs compiler to perform an operation
  or action.
Arithmetic Operators
  Perform four fundamental operations which are addition,
        subtraction, multiplication and division.
Operator     Description                          Example   Result
   *         Multiplication                        2*8       16
   /         Division                              8/2        4
   +         Addition                              2+8       10
   -         Subtraction                            8-2       6
   ()        Parentheses. For grouping             (8+2)     10
   %         Modulus. Divides two number and       4%3        1
             returns just the remainder portion
Arithmetic Operators
arithmetic expressions are evaluated with some rule called
  operator precedence.
  multiplications (*) and divisions (/) are evaluated first
  additions (+) and subtractions (-) are performed last.
Activity 1

Write the resultant value of the following expressions.


       Expression             Result
       14-4
       14+4
       14* 4
       14/4
       14%4
ACTIVITY 2
     Calculate the total marks for the given five subjects.
     Declare appropriate variables, assign values as given
     below:
       English = 85
       Maths = 100
       History = 75
       Geography = 70
       Art = 85

         calculate the total marks
         calculate the average
#include<iostream>

using namespace std;
int main()
{
   int english = 85, maths = 100, history = 75, geography = 70, art = 85;
   int sum=0;

    sum=english+maths+history+geography+art;
    cout<<"Sum is: "<<sum<<endl;;

    return 0;
}
#include<iostream>

using namespace std;
int main()
{
    int english = 85, maths = 100, history = 75, geography = 70, art = 85;
    int sum=0, average=0;

    sum=english+maths+history+geography+art;
    average=sum/5;

    cout<<"Sum is: "<<sum<<endl;
    cout<<"Average is:"<<average<<endl;

    return 0;
}
Assignment Operator
variables are given a value through the use of assignment
 operator (=)
general form:
 variable = expression;
eg:
  int   x, y;
  x =   2;
  y =   5*x; // y = 10
  x =   x + 4; // x = 6
  y =   y/2; // y = 5
Assignment Operator
The value of an assignment can be used in another
  assignment.

This is a chained assignment.
Eg:
    m = (n = 66) + 9; // n = 66 and m = 75
    x = y = 22; // x = 22 and y = 22
Compound Assignment Operators
(+=), (-=), (*=), (/=), (%=)


allow us to use assignment and an arithmetic operator
  together.

general form is:
  variable operator= expression;
Compound Assignment Operators
Operator         Description          Example   Equivalent to

  +=            add and assign        x += 3     x=x+3

  -=         subtract and assign      x -= 5      x=x-5

  *=         multiply and assign      x *= 4     x=x*4

  /=          divide and assign       x /= 2     x=x/2



  %=       find reminder and assign   x %= 9     x=x%9
In Class Exercise
 Given the following declaration and initial assignment:


   int i = 4, j = 21, k;
   float x = 94.55, y;
   char a = ‘z’, b = ‘t’, c =‘ ‘;

   Determine the value for the following assignments:
    k=i*j
   y = x + i
   y = k = j
    a = b =c
    i += i
Increment & Decrement Operator
increment operator (++) and decrement operator (--)
 increase or reduce by one the value stored in a variable.
the following are equivalent in functionality.
  x++;
  x += 1;
  x = x + 1;
characteristic of this operator:
  can be used both as a prefix and as a postfix.
  eg: a++ or ++a
  the results is different
Prefix
When a prefix expression (++x or --x) is used as part
  of an expression, the value returned is the value
  calculated after the prefix operator is applied

  int x = 0;
  int y = 0;
  y = ++x;
  // result: y=1, x=1 x is incremented by 1 and the result is
  assigned to y
Postfix
When a postfix expression (x++ or x--) is used as part
  of an expression, the value returned is the value
  calculated before the postfix operator is applied

  int x = 0;
  int y = 0;
  y = x++;
  // result: y=0, x=1 original value of x is stored, x is
  incremented, original value of x is assigned to y
Increment & Decrement Operator
eg:
 a = 5; // a = 5
 b = a++; // b = a = 5, then a = 5+1 = 6
 c = ++a; // a = 6+1 = 7, then c = a = 7
Relational Operator
    Control statements use relation operators in order to compare two objects,
     which results in a value that is either true (1) or false (0).
    There are six relational operators as follows:



Operator            Description                         Example            Value
   <                   less than               x<y                6<9        1

  <=             less than or equal to        x <= y           12 <= 13      1

   >                 greater than              x>y                7 > 10     0

  >=           greater than or equal to       x >= y              9 >= 5     1

  ==                   equal to               x == y              7 ==5      0

   !=                not equal to              x != y             6 != 5     1
Logical Operator
Operator   Description                                    Example

           logical AND, conjunction.
  &&       Both sides must be true for the result to   x > 2 && y == 3
           be true


           logical OR, disjunction.
  ||       The result is true if either side or both   x > 2 || x <= 9
           sides are true.


   !       logical NOT, negation                           !(x>0)
Condition1   Condition2   &&       ||

   true         true      true    true

   true        false      false   true

  false         true      false   true

  false        false      false   false
Logical Operator
using these operators, we can form a Boolean Expression that
 can be either true or false.
Boolean expressions evaluate to integer values.
The value 0 means 'false' and all non-zero value means 'true'.
There are also two literal constants used with bool
 variables: true and false.
In Class Exercise
What is the difference between x = 3 and x == 3?
Given a = 2, b = 5, c = 7 determine the value for the
  following expressions either TRUE or FALSE: :
   1.   a < 2 && b > 5
   2.   c >= a
   3.   a < b || b > c
   4.   c != 10
   5.   a == 25
Operator Precedence
  Determines the order in which mathematical computations
    and operations are performed in an expression.
Operator Precedence
Operator Precedence

Given a=b = 5, c = 2; solve the following expression
 a+b*2/c–5
 =5+5*2/2–5
 = 5 + 10 / 2 – 5
 =5+5–5
 = 10 – 5
 =5
In Class Exercise
Given x = 10, y = 7, z = 2; solve the following expression
  x + 2y – z
  x / z – x * x + y
  x * y % z
  5 ( x + y + z ) – x / z
  ++x*y – y*z
Type conversion/Type Casting
Is the conversion of a value of one data type into another
 data type.
Typecasting consist of two types:
     Implicit conversion
     Explicit conversion
Implicit conversion
A value of data type with lower range is assigned to a variable
  of data type with higher range
Lower range to higher range
Example
#include<iostream>
using namespace std;
                                 OUTPUT
void main()                      Result: 4.2555
{
  double x; // Size is 8 bytes
  int y=2; //Size is 4 bytes
  float z=2.2555f;
  x=y+z; //Implicit conversion
  cout<<"Result: "<<x;
}
Explicit conversion
A value of data type with higher range is assigned to a
  variable of data type with lower range
Higher range to lower range

Explicit conversion leads to data loss.
Example
#include<iostream>

using namespace std;
                                    OUTPUT
void main()                         The total value is:300
{
  int total_value;
  double book1 = 300.46;
   total_value=(int)book1; // Explicit conversion
  cout<<"The total value is:"<<total_value;
}
Exercise
Write a program to declare a variable d of type double.
  Assign the value 102.5789 to d. Declare a float variable fl.
  Assign the value of d to fl by performing an explicit
  conversion and print the value on the screen.
Answer
#include<iostream>
using namespace std;
void main()            OUTPUT
{                      Value of the float variable fl = 102.579
        double d=102.5789;
        float fl;
        fl=(float)d; // Explicit conversion
        cout<<"Value of the float variable fl = "<<fl;
}
Summary
In this presentation, you learnt the following:
Operator is a symbol that instructs compiler to perform an
  operation or action.
Typecasting is the conversion of data type of the value of a
  variable into another.
There are two types of casting: implicit conversion and
  explicit conversion.
FP 201 – Programming
Fundamentals with C++
Input Output Statements
Learning outcome
By the end of the course, students should be able to:

Identify the syntax use for input and output
Write program that use the input and output statements
Input/Output Statements
The standard C++ library includes the header file
  iostream, where the standard input and output stream
  objects are declared.

  cout to output data to the screen and


  cin to input data from the keyboard.
Standard Output
standard output:
  need to use cout, followed by the insertion operator (<<)
  eg:
    cout << "Hello World!"; // Outputs "Hello World!"
                          on the screen
    cout << 1299; // Outputs the number 1299 on the
        screen
    cout << x; // Outputs the content of x on the
      screen
insertion operator (<<) may be used more than once in a
  single statement:
  cout << "Hi, " << "this is " << "a simple C++ statement";
Standard Output
Note that, cout does not add a line break.
For example:
  cout << "Computer Programming ";
  cout << "is important for engineers.";
  will be output as:
       Computer Programming is important for
  engineers.
Standard Output
A line break can be added by inserting a 'n' character or a
  using a endl manipulator.

  cout << "Computer Programming n";
  cout << "is important for engineers.";


  cout << "Computer Programming " << endl;
  cout << "is important for engineers.";


These two produce the following (same) output: Computer
  Programming
  is important for engineers.
Standard Input
 Handling the standard input is done by applying the
  overloaded operator of extraction (>>) on the cin stream
 Value can be input from the keyboard and assigned to a
  variable x as follows:
  cin >> x;
 and two variables can assigned as any of the follows:
  cin >> x;
  cin >> y;
  cin >> x >> y;
Code Example
// Calculates the sum of two integers
#include <iostream>
using namespace std;

int main()
  {
  int a, b, total;
  cout << "Enter two integers to be added: ";
  cin >> a >> b;
  total = a + b;
  cout << "The sum is " << total << endl;
  return 0;
 }
Code Example
// Calculates the area of a triangle
// given an input of the triangle base and height.
#include <iostream>

int main()
  {
  float base, height, area;
  std::cout << "Enter the base and height of the
                triangle: ";
  std::cin >> base >> height;
  area = 0.5*base*height;
  std::cout << "The area of the triangle is "
            << area << std::endl;
}
Q&A
Develop a program that can calculate the total and average of
  four numbers that is input by user.
#include <iostream>
using namespace std;

int main()
  {
  int a, b, c, d, total, average;
  cout << "Enter four integers to be added: ";
  cin >> a >> b>>c>>d;
  total = a + b + c + d;
  average = total/4;
  cout << "The sum is " << total << endl;
  cout<< "The average is "<< average << endl;

 return 0;
 }
Summary
In this presentation, you learnt the following:

cout to output data to the screen
cin to input data from the keyboard.
cout use insertion operator (<<)
cin use overloaded operator of extraction (>>)
Ad

More Related Content

What's hot (20)

C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
Amit Kapoor
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
Tushar B Kute
 
C++ Language
C++ LanguageC++ Language
C++ Language
Syed Zaid Irshad
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
Neeru Mittal
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
Venkateswarlu Vuggam
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
Pointers
PointersPointers
Pointers
sanya6900
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
रमन सनौरिया
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
umar78600
 
C++ programming
C++ programmingC++ programming
C++ programming
viancagerone
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
Dr. Md. Shohel Sayeed
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
Dr. Md. Shohel Sayeed
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
harman kaur
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
Emertxe Information Technologies Pvt Ltd
 
Lecture04
Lecture04Lecture04
Lecture04
elearning_portal
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
Dr. Md. Shohel Sayeed
 

Viewers also liked (7)

Fp201 unit1 1
Fp201 unit1 1Fp201 unit1 1
Fp201 unit1 1
rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
Unit 3
Unit 3Unit 3
Unit 3
rohassanie
 
Unit 1
Unit 1Unit 1
Unit 1
rohassanie
 
Unit 2
Unit 2Unit 2
Unit 2
rohassanie
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
rohassanie
 
Ad

Similar to FP 201 Unit 2 - Part 3 (20)

Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
Genard Briane Ancero
 
Report Group 4 Constants and Variables(TLE)
Report Group 4 Constants and Variables(TLE)Report Group 4 Constants and Variables(TLE)
Report Group 4 Constants and Variables(TLE)
Genard Briane Ancero
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
PGT_13
 
Operators in C programming language.pptx
Operators in C programming language.pptxOperators in C programming language.pptx
Operators in C programming language.pptx
b221382
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
rinkugupta37
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
Tanmay Modi
 
Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01
Nurhidayah Mahmud
 
Types of Operators in C programming .pdf
Types of Operators in C programming  .pdfTypes of Operators in C programming  .pdf
Types of Operators in C programming .pdf
RichardMathengeSPASP
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
rohassanie
 
Basics of c++
Basics of c++ Basics of c++
Basics of c++
Gunjan Mathur
 
Operators in c++ programming types of variables
Operators in c++ programming types of variablesOperators in c++ programming types of variables
Operators in c++ programming types of variables
Ahmad177077
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
jahanullah
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
guest58c84c
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
Kaushal Patel
 
05 operators
05   operators05   operators
05 operators
dhrubo kayal
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 
Python programing
Python programingPython programing
Python programing
hamzagame
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
Genard Briane Ancero
 
Report Group 4 Constants and Variables(TLE)
Report Group 4 Constants and Variables(TLE)Report Group 4 Constants and Variables(TLE)
Report Group 4 Constants and Variables(TLE)
Genard Briane Ancero
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
PGT_13
 
Operators in C programming language.pptx
Operators in C programming language.pptxOperators in C programming language.pptx
Operators in C programming language.pptx
b221382
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
rinkugupta37
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
Tanmay Modi
 
Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01
Nurhidayah Mahmud
 
Types of Operators in C programming .pdf
Types of Operators in C programming  .pdfTypes of Operators in C programming  .pdf
Types of Operators in C programming .pdf
RichardMathengeSPASP
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
rohassanie
 
Operators in c++ programming types of variables
Operators in c++ programming types of variablesOperators in c++ programming types of variables
Operators in c++ programming types of variables
Ahmad177077
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
jahanullah
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
Kaushal Patel
 
Python programing
Python programingPython programing
Python programing
hamzagame
 
Ad

More from rohassanie (19)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
rohassanie
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
rohassanie
 
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 201
rohassanie
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
rohassanie
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
rohassanie
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
rohassanie
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
rohassanie
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
rohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
rohassanie
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1
rohassanie
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
rohassanie
 
Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
rohassanie
 
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 201
rohassanie
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
rohassanie
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
rohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
rohassanie
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1
rohassanie
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
rohassanie
 

FP 201 Unit 2 - Part 3

  • 1. FP 201 – Programming Fundamentals Operators & Expressions
  • 2. Learning outcome By the end of the course, students should be able to: Define the following operators: a. Arithmetic operator b. Assignment operator c. Increment and decrement operator d. Relational operator e. Logical operator Explain operators’ precedence Explain type conversion Write expression using operator Use expression in program
  • 3. Operators Operator Action - subtraction (also unary minus) + addition * multiplication / division % modulus division -- decrement ++ increment && AND || OR ! NOT
  • 4. Operators Is a symbol that instructs compiler to perform an operation or action.
  • 5. Arithmetic Operators Perform four fundamental operations which are addition, subtraction, multiplication and division. Operator Description Example Result * Multiplication 2*8 16 / Division 8/2 4 + Addition 2+8 10 - Subtraction 8-2 6 () Parentheses. For grouping (8+2) 10 % Modulus. Divides two number and 4%3 1 returns just the remainder portion
  • 6. Arithmetic Operators arithmetic expressions are evaluated with some rule called operator precedence. multiplications (*) and divisions (/) are evaluated first additions (+) and subtractions (-) are performed last.
  • 7. Activity 1 Write the resultant value of the following expressions. Expression Result 14-4 14+4 14* 4 14/4 14%4
  • 8. ACTIVITY 2 Calculate the total marks for the given five subjects. Declare appropriate variables, assign values as given below: English = 85 Maths = 100 History = 75 Geography = 70 Art = 85  calculate the total marks  calculate the average
  • 9. #include<iostream> using namespace std; int main() { int english = 85, maths = 100, history = 75, geography = 70, art = 85; int sum=0; sum=english+maths+history+geography+art; cout<<"Sum is: "<<sum<<endl;; return 0; }
  • 10. #include<iostream> using namespace std; int main() { int english = 85, maths = 100, history = 75, geography = 70, art = 85; int sum=0, average=0; sum=english+maths+history+geography+art; average=sum/5; cout<<"Sum is: "<<sum<<endl; cout<<"Average is:"<<average<<endl; return 0; }
  • 11. Assignment Operator variables are given a value through the use of assignment operator (=) general form: variable = expression; eg: int x, y; x = 2; y = 5*x; // y = 10 x = x + 4; // x = 6 y = y/2; // y = 5
  • 12. Assignment Operator The value of an assignment can be used in another assignment. This is a chained assignment. Eg: m = (n = 66) + 9; // n = 66 and m = 75 x = y = 22; // x = 22 and y = 22
  • 13. Compound Assignment Operators (+=), (-=), (*=), (/=), (%=) allow us to use assignment and an arithmetic operator together. general form is: variable operator= expression;
  • 14. Compound Assignment Operators Operator Description Example Equivalent to += add and assign x += 3 x=x+3 -= subtract and assign x -= 5 x=x-5 *= multiply and assign x *= 4 x=x*4 /= divide and assign x /= 2 x=x/2 %= find reminder and assign x %= 9 x=x%9
  • 15. In Class Exercise  Given the following declaration and initial assignment: int i = 4, j = 21, k; float x = 94.55, y; char a = ‘z’, b = ‘t’, c =‘ ‘; Determine the value for the following assignments:  k=i*j y = x + i y = k = j  a = b =c  i += i
  • 16. Increment & Decrement Operator increment operator (++) and decrement operator (--) increase or reduce by one the value stored in a variable. the following are equivalent in functionality. x++; x += 1; x = x + 1; characteristic of this operator: can be used both as a prefix and as a postfix. eg: a++ or ++a the results is different
  • 17. Prefix When a prefix expression (++x or --x) is used as part of an expression, the value returned is the value calculated after the prefix operator is applied int x = 0; int y = 0; y = ++x; // result: y=1, x=1 x is incremented by 1 and the result is assigned to y
  • 18. Postfix When a postfix expression (x++ or x--) is used as part of an expression, the value returned is the value calculated before the postfix operator is applied int x = 0; int y = 0; y = x++; // result: y=0, x=1 original value of x is stored, x is incremented, original value of x is assigned to y
  • 19. Increment & Decrement Operator eg: a = 5; // a = 5 b = a++; // b = a = 5, then a = 5+1 = 6 c = ++a; // a = 6+1 = 7, then c = a = 7
  • 20. Relational Operator  Control statements use relation operators in order to compare two objects, which results in a value that is either true (1) or false (0).  There are six relational operators as follows: Operator Description Example Value < less than x<y 6<9 1 <= less than or equal to x <= y 12 <= 13 1 > greater than x>y 7 > 10 0 >= greater than or equal to x >= y 9 >= 5 1 == equal to x == y 7 ==5 0 != not equal to x != y 6 != 5 1
  • 21. Logical Operator Operator Description Example logical AND, conjunction. && Both sides must be true for the result to x > 2 && y == 3 be true logical OR, disjunction. || The result is true if either side or both x > 2 || x <= 9 sides are true. ! logical NOT, negation !(x>0)
  • 22. Condition1 Condition2 && || true true true true true false false true false true false true false false false false
  • 23. Logical Operator using these operators, we can form a Boolean Expression that can be either true or false. Boolean expressions evaluate to integer values. The value 0 means 'false' and all non-zero value means 'true'. There are also two literal constants used with bool variables: true and false.
  • 24. In Class Exercise What is the difference between x = 3 and x == 3? Given a = 2, b = 5, c = 7 determine the value for the following expressions either TRUE or FALSE: : 1. a < 2 && b > 5 2. c >= a 3. a < b || b > c 4. c != 10 5. a == 25
  • 25. Operator Precedence Determines the order in which mathematical computations and operations are performed in an expression.
  • 27. Operator Precedence Given a=b = 5, c = 2; solve the following expression a+b*2/c–5 =5+5*2/2–5 = 5 + 10 / 2 – 5 =5+5–5 = 10 – 5 =5
  • 28. In Class Exercise Given x = 10, y = 7, z = 2; solve the following expression x + 2y – z x / z – x * x + y x * y % z 5 ( x + y + z ) – x / z ++x*y – y*z
  • 29. Type conversion/Type Casting Is the conversion of a value of one data type into another data type. Typecasting consist of two types:  Implicit conversion  Explicit conversion
  • 30. Implicit conversion A value of data type with lower range is assigned to a variable of data type with higher range Lower range to higher range
  • 31. Example #include<iostream> using namespace std; OUTPUT void main() Result: 4.2555 { double x; // Size is 8 bytes int y=2; //Size is 4 bytes float z=2.2555f; x=y+z; //Implicit conversion cout<<"Result: "<<x; }
  • 32. Explicit conversion A value of data type with higher range is assigned to a variable of data type with lower range Higher range to lower range Explicit conversion leads to data loss.
  • 33. Example #include<iostream> using namespace std; OUTPUT void main() The total value is:300 { int total_value; double book1 = 300.46; total_value=(int)book1; // Explicit conversion cout<<"The total value is:"<<total_value; }
  • 34. Exercise Write a program to declare a variable d of type double. Assign the value 102.5789 to d. Declare a float variable fl. Assign the value of d to fl by performing an explicit conversion and print the value on the screen.
  • 35. Answer #include<iostream> using namespace std; void main() OUTPUT { Value of the float variable fl = 102.579 double d=102.5789; float fl; fl=(float)d; // Explicit conversion cout<<"Value of the float variable fl = "<<fl; }
  • 36. Summary In this presentation, you learnt the following: Operator is a symbol that instructs compiler to perform an operation or action. Typecasting is the conversion of data type of the value of a variable into another. There are two types of casting: implicit conversion and explicit conversion.
  • 37. FP 201 – Programming Fundamentals with C++ Input Output Statements
  • 38. Learning outcome By the end of the course, students should be able to: Identify the syntax use for input and output Write program that use the input and output statements
  • 39. Input/Output Statements The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared. cout to output data to the screen and cin to input data from the keyboard.
  • 40. Standard Output standard output: need to use cout, followed by the insertion operator (<<) eg: cout << "Hello World!"; // Outputs "Hello World!" on the screen cout << 1299; // Outputs the number 1299 on the screen cout << x; // Outputs the content of x on the screen insertion operator (<<) may be used more than once in a single statement: cout << "Hi, " << "this is " << "a simple C++ statement";
  • 41. Standard Output Note that, cout does not add a line break. For example: cout << "Computer Programming "; cout << "is important for engineers."; will be output as: Computer Programming is important for engineers.
  • 42. Standard Output A line break can be added by inserting a 'n' character or a using a endl manipulator. cout << "Computer Programming n"; cout << "is important for engineers."; cout << "Computer Programming " << endl; cout << "is important for engineers."; These two produce the following (same) output: Computer Programming is important for engineers.
  • 43. Standard Input  Handling the standard input is done by applying the overloaded operator of extraction (>>) on the cin stream  Value can be input from the keyboard and assigned to a variable x as follows: cin >> x;  and two variables can assigned as any of the follows: cin >> x; cin >> y; cin >> x >> y;
  • 44. Code Example // Calculates the sum of two integers #include <iostream> using namespace std; int main() { int a, b, total; cout << "Enter two integers to be added: "; cin >> a >> b; total = a + b; cout << "The sum is " << total << endl; return 0; }
  • 45. Code Example // Calculates the area of a triangle // given an input of the triangle base and height. #include <iostream> int main() { float base, height, area; std::cout << "Enter the base and height of the triangle: "; std::cin >> base >> height; area = 0.5*base*height; std::cout << "The area of the triangle is " << area << std::endl; }
  • 46. Q&A Develop a program that can calculate the total and average of four numbers that is input by user.
  • 47. #include <iostream> using namespace std; int main() { int a, b, c, d, total, average; cout << "Enter four integers to be added: "; cin >> a >> b>>c>>d; total = a + b + c + d; average = total/4; cout << "The sum is " << total << endl; cout<< "The average is "<< average << endl; return 0; }
  • 48. Summary In this presentation, you learnt the following: cout to output data to the screen cin to input data from the keyboard. cout use insertion operator (<<) cin use overloaded operator of extraction (>>)

Editor's Notes

  • #16: k=84 y=98.55 y=21 a= i=8
  • #25: a &lt; 2 &amp;&amp; b &gt; 5 : FALSE c &gt;= a : TRUE a &lt; b || b &gt; c : FALSE c != 10: TRUE a == 25: FALSE
  • #29: x + 2y – z=22 x / z – x * x + y=-88 x * y % z=0 5 ( x + y + z ) – x / z:90 ++x*y – y*z=63
  翻译: