SlideShare a Scribd company logo
1 of 7
F2037 PROGRAMMING FUNDAMENTAL OF C++


LAB 2: OPERATORS AND EXPRESSION

Learning Outcome:

By the end of this lab, students should be able to:
•   Understand operators, operator’s precedence and expression.


Theory/ Topics

•   A simple C++ program is similar to a C program. In C++ programs
    the statements to be executed are contained inside the function.

•   In operators and expressions, student must know about:
    a) Arithmetic operators ( +, -, *, /, % )
          The five arithmetical operations supported by the C++
             language are:
                               + addition
                               - subtraction
                               * multiplication
                               / division
                               % modulus

    b) Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |
       =)

    c) Increment and decrement (++, --)
        Shortening even more some expressions, the increase
        operator (++) and the decrease operator (--) increase or
        reduce by one the value stored in a variable. They are
        equivalent to +=1 and to -=1, respectively.

                    Example 1                     Example 2
           B=3;                          B=3;
           A=++B;                        A=B++;
           // A contains 4, B contains 4 // A contains 3, B contains 4


           d) Relational and equality operators (==, !=, >, <, >=, <= )

                                                                         1
F2037 PROGRAMMING FUNDAMENTAL OF C++


        In order to evaluate a comparison between two
        expressions we can use the relational and equality
        operators. The result of a relational operation is a
        Boolean value that can only be true or false, according
        to its Boolean result.

   e) Logical operators ( !, &&, || )
       The Operator ! is the C++ operator to perform the
       Boolean operation NOT, it has only one operand,
       located at its right, and the only thing that it does is to
       inverse the value of it, producing false if its operand is
       true and true if its operand is false. Basically, it returns
       the opposite Boolean value of evaluating its operand.

    Precedence of operators
When writing complex expressions with several operands, we
must follow the precedence which is what operand is evaluated
first and which later. For example, in this expression:

                         a=5+7%2

    7%2 is evaluated first, then followed by operator +

Activity 2A

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2A.cpp.

#include <iostream>
using namespace std;
int main()
{
      int class1 = 100;
      int class2 = 200;
      int class3 = 300;
      int class4 = 400;
      int class5 = 500;


                                                                 2
F2037 PROGRAMMING FUNDAMENTAL OF C++


      int sum = 0;
      double average;
      sum = class1 + class2 + class3 + class4 +
      class5;
      average = sum/5;
      cout << " Sum = " << sum << endl;
      cout << " Average = " << average << endl;
      return 0;
}

Activity 2B

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2B.cpp.

#include <iostream>
using namespace std;

void main()
{
      int x = 180, y = 200;
      y = x++;
     cout << " x : " << x << endl << " y : " << y <<
     endl;
}

Activity 2C

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2C.cpp.

#include <iostream>
using namespace std;



                                                         3
F2037 PROGRAMMING FUNDAMENTAL OF C++


void main()
{
      int x = 180, y = 200;
      y = ++ x;
      cout << " x : " << x << endl << " y : "<<
      y << endl;
}

Activity 2D

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2D.cpp.

#include <iostream>
using namespace std;

void main()
{
      double p     = 12.5;
      double q     = 3.234;
      p *= q -     1;
      q += p +     1;
cout << " p is     " << p << endl << " q is " <<
q << "n";
}

Lab 2E

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2E.cpp.

// Demonstrate the modulus operator.
#include <iostream>
using namespace std;


                                                         4
F2037 PROGRAMMING FUNDAMENTAL OF C++


int main()
{
      int x, y;
      x = 10;
      y = 3;
      cout << x << " / " << y << " is " << x / y <<
      " with a remainder of " << x % y << "n";
      x = 1;
      y = 2;
      cout << x << " / " << y << " is " << x / y <<
      "n" << x << " % " << y << " is " << x % y;
      return 0;
}

Lab 2F

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2F.cpp.

// Demonstrate the relational and logical operators.
#include <iostream>
using namespace std;
int main()
{
      int i, j;
      bool b1, b2;
      i = 10;
      j = 11;
      if(i < j) cout << "i < jn";
      if(i <= j) cout << "i <= jn";
      if(i != j) cout << "i != jn";
      if(i == j) cout << "this won't executen";
      if(i >= j) cout << "this won't executen";
      if(i > j) cout << "this won't executen";

         b1 = true; b2 = false;
         if(b1 && b2) cout << "this won't executen";



                                                         5
F2037 PROGRAMMING FUNDAMENTAL OF C++


       if(!(b1 && b2)) cout << "!(b1 && b2) is
       truen";
       if(b1 || b2) cout << "b1 || b2 is
       truen";
       return 0;
}

Lab 2G

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2G.cpp.

#include<iostream>
using namespace std;

void main()
{
      int i;
      for(i=1;i<=10;i++)
      cout<<i<<" /2 is: " << (float) i/2 << "n";
}


LAB EXERCISE

1. Write the C++ expression for the following
    mathematical statement

    a. y=(x-2)(x+3)
    b. min = a + b + c + d + e
                       5
                                                  (2 marks)

2. Given the values x=5, y=5 and c=3. Write a program
    to calculate the value of z and display the output of z



                                                              6
F2037 PROGRAMMING FUNDAMENTAL OF C++


         z = xy % c + 10 / 2y + 5;                   (4 marks)

3. Based on the flowchart, find the values for a and b. Write a
   program to calculate the values and display the output
                                                       (4
   marks)


                           start

                      x=12, y=8,z=5


                         a=x*y-z


                     b = (6*a/2+3-z)/2



                        print a,b


                            end

CONCLUSION
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
___________________________________________________



                                                             7

More Related Content

What's hot

Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
rohassanie
 

What's hot (20)

C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
C++ book
C++ bookC++ book
C++ book
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
 
C language
C languageC language
C language
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C++
C++C++
C++
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in C
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 

Viewers also liked

Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
farhan amjad
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
dheva B
 

Viewers also liked (20)

Unit i
Unit iUnit i
Unit i
 
Oops
OopsOops
Oops
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
SSRP Self Learning Guide Maths Class 10 - In Hindi
SSRP Self Learning Guide Maths Class 10 - In HindiSSRP Self Learning Guide Maths Class 10 - In Hindi
SSRP Self Learning Guide Maths Class 10 - In Hindi
 
Oops Concepts
Oops ConceptsOops Concepts
Oops Concepts
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Cbse class 10 hindi course b model answers by candidates 2015
Cbse class 10 hindi course b model answers by candidates 2015Cbse class 10 hindi course b model answers by candidates 2015
Cbse class 10 hindi course b model answers by candidates 2015
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
हिन्दी व्याकरण
हिन्दी व्याकरणहिन्दी व्याकरण
हिन्दी व्याकरण
 
OOPs concept and implementation
OOPs concept and implementationOOPs concept and implementation
OOPs concept and implementation
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programming
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
कारक
कारककारक
कारक
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Vakya parichay
Vakya parichayVakya parichay
Vakya parichay
 
कारक(karak)
कारक(karak)कारक(karak)
कारक(karak)
 

Similar to Labsheet2

Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
rohassanie
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
Prabhu D
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
ranaibrahim453
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
IIUM
 

Similar to Labsheet2 (20)

Lab 1
Lab 1Lab 1
Lab 1
 
C++ Question
C++ QuestionC++ Question
C++ Question
 
901131 examples
901131 examples901131 examples
901131 examples
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
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
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
Python programming workshop session 4
Python programming workshop session 4Python programming workshop session 4
Python programming workshop session 4
 
C++ Programming.pdf
C++ Programming.pdfC++ Programming.pdf
C++ Programming.pdf
 
Esg111 midterm review
Esg111 midterm reviewEsg111 midterm review
Esg111 midterm review
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 

More from rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
rohassanie
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
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
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
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
 
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
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
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
 

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
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
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
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
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...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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, ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.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
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Labsheet2

  • 1. F2037 PROGRAMMING FUNDAMENTAL OF C++ LAB 2: OPERATORS AND EXPRESSION Learning Outcome: By the end of this lab, students should be able to: • Understand operators, operator’s precedence and expression. Theory/ Topics • A simple C++ program is similar to a C program. In C++ programs the statements to be executed are contained inside the function. • In operators and expressions, student must know about: a) Arithmetic operators ( +, -, *, /, % )  The five arithmetical operations supported by the C++ language are: + addition - subtraction * multiplication / division % modulus b) Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, | =) c) Increment and decrement (++, --) Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Example 1 Example 2 B=3; B=3; A=++B; A=B++; // A contains 4, B contains 4 // A contains 3, B contains 4 d) Relational and equality operators (==, !=, >, <, >=, <= ) 1
  • 2. F2037 PROGRAMMING FUNDAMENTAL OF C++ In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. e) Logical operators ( !, &&, || ) The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. Precedence of operators When writing complex expressions with several operands, we must follow the precedence which is what operand is evaluated first and which later. For example, in this expression: a=5+7%2 7%2 is evaluated first, then followed by operator + Activity 2A Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2A.cpp. #include <iostream> using namespace std; int main() { int class1 = 100; int class2 = 200; int class3 = 300; int class4 = 400; int class5 = 500; 2
  • 3. F2037 PROGRAMMING FUNDAMENTAL OF C++ int sum = 0; double average; sum = class1 + class2 + class3 + class4 + class5; average = sum/5; cout << " Sum = " << sum << endl; cout << " Average = " << average << endl; return 0; } Activity 2B Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2B.cpp. #include <iostream> using namespace std; void main() { int x = 180, y = 200; y = x++; cout << " x : " << x << endl << " y : " << y << endl; } Activity 2C Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2C.cpp. #include <iostream> using namespace std; 3
  • 4. F2037 PROGRAMMING FUNDAMENTAL OF C++ void main() { int x = 180, y = 200; y = ++ x; cout << " x : " << x << endl << " y : "<< y << endl; } Activity 2D Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2D.cpp. #include <iostream> using namespace std; void main() { double p = 12.5; double q = 3.234; p *= q - 1; q += p + 1; cout << " p is " << p << endl << " q is " << q << "n"; } Lab 2E Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2E.cpp. // Demonstrate the modulus operator. #include <iostream> using namespace std; 4
  • 5. F2037 PROGRAMMING FUNDAMENTAL OF C++ int main() { int x, y; x = 10; y = 3; cout << x << " / " << y << " is " << x / y << " with a remainder of " << x % y << "n"; x = 1; y = 2; cout << x << " / " << y << " is " << x / y << "n" << x << " % " << y << " is " << x % y; return 0; } Lab 2F Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2F.cpp. // Demonstrate the relational and logical operators. #include <iostream> using namespace std; int main() { int i, j; bool b1, b2; i = 10; j = 11; if(i < j) cout << "i < jn"; if(i <= j) cout << "i <= jn"; if(i != j) cout << "i != jn"; if(i == j) cout << "this won't executen"; if(i >= j) cout << "this won't executen"; if(i > j) cout << "this won't executen"; b1 = true; b2 = false; if(b1 && b2) cout << "this won't executen"; 5
  • 6. F2037 PROGRAMMING FUNDAMENTAL OF C++ if(!(b1 && b2)) cout << "!(b1 && b2) is truen"; if(b1 || b2) cout << "b1 || b2 is truen"; return 0; } Lab 2G Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2G.cpp. #include<iostream> using namespace std; void main() { int i; for(i=1;i<=10;i++) cout<<i<<" /2 is: " << (float) i/2 << "n"; } LAB EXERCISE 1. Write the C++ expression for the following mathematical statement a. y=(x-2)(x+3) b. min = a + b + c + d + e 5 (2 marks) 2. Given the values x=5, y=5 and c=3. Write a program to calculate the value of z and display the output of z 6
  • 7. F2037 PROGRAMMING FUNDAMENTAL OF C++ z = xy % c + 10 / 2y + 5; (4 marks) 3. Based on the flowchart, find the values for a and b. Write a program to calculate the values and display the output (4 marks) start x=12, y=8,z=5 a=x*y-z b = (6*a/2+3-z)/2 print a,b end CONCLUSION ____________________________________________________ ____________________________________________________ ____________________________________________________ ____________________________________________________ ____________________________________________________ ___________________________________________________ 7