Lecture Ppt 8

download Lecture Ppt 8

of 30

Transcript of Lecture Ppt 8

  • 8/2/2019 Lecture Ppt 8

    1/30

    Click to edit Master subtitle style

    3/17/12

    Computer ProgrammingAn Introductory Training

  • 8/2/2019 Lecture Ppt 8

    2/30

    3/17/12

    Schema of Discussion

    Logical Operators

    The Equality Operator ==

    The Logical Not Operator !

    The Inequality Operator !=

    The Comparison for a Lower Value

    When two values of the same type aredistinct, one of them is usually higher

    than the other. C++ provides a logicaloperator that allows you to find out ifone of two values is greater than theother. The operator used for this

    operation uses the > symbol. Its syntaxis:

    Value1 > Value2

    Both operands, in this case Value1 and

  • 8/2/2019 Lecture Ppt 8

    20/30

    3/17/12

    The Comparison for aGreater Value >=

    The greater than and the equalityoperators can be combined to produce

    an operator as follows: >=. This is the"greater than or equal to" operator. Itssyntax is:

    Value1 >= Value2

    A comparison is performed on bothoperands: Value1 and Value2. If thevalue of Value1 and that of Value2 are

    the same, the comparison produces a

  • 8/2/2019 Lecture Ppt 8

    21/30

    3/17/12

    Boolean Variables

    The Boolean data type is used todeclare a variable whose value wouldbe set as true (1) or false (0). To declaresuch a value, you use the bool keyword.

    The variable can then be initialized witha starting value. The Boolean constant

    is used to check that the state of avariable (or a function) is true or false.

    You can declare such a variable as:

    bool GotThePassingGrade = true;

    http://www.functionx.com/cpp/Lesson08.htmhttp://www.functionx.com/cpp/Lesson08.htm
  • 8/2/2019 Lecture Ppt 8

    22/30

    3/17/12

    Boolean Variables

    Here is an example:

    #include

    using namespace std;

    int main()

    {

    bool MachineIsWorking = true;

  • 8/2/2019 Lecture Ppt 8

    23/30

    3/17/12

    Enumerations

    An enumeration provides a technique ofsetting a list of integers where eachitem of the list is ranked and has aspecific name. For example, instead ofnumbers that represent players of afootball (soccer) game such as 1, 2, 3,

    4, 5, you can use names instead. Thiswould produce GoalKeeper,RightDefender, LeftDefender, Stopper,Libero. The syntax of creating an

    enumeration is

  • 8/2/2019 Lecture Ppt 8

    24/30

    3/17/12

    Enumerations

    Each name in this list represents aconstant number. Since theenumeration list is created in thebeginning of the program, or at leastbefore using any of the values in thelist, each item in the list is assigned a

    constant number. The items arecounted starting at 0, then 1, etc. Bydefault, the first item in the list isassigned the number 0, the second is 1,

    etc. Just like in a game, you do not have

  • 8/2/2019 Lecture Ppt 8

    25/30

    3/17/12

    Enumerations

    You still can assign any value of yourchoice to any item in the list, or you canset different ranges of values to variousitems. You can create a list like this:

    enum Days { Mon, Tue, Wed, Thu, Fri,Sat, Sun = 0 };

    Since Sun is No. 0, Sat would be No. 1,Fri would be 2 and so on. On the otherhand, you can set values to your liking.

    Here is an example:

  • 8/2/2019 Lecture Ppt 8

    26/30

    3/17/12

    Enumerations

    Once the list has been created, thename you give to the list, such asPlayers, becomes an identifier of itsown, and its can be used to declare avariable. Therefore, a variable of theenumerated type would be declared as:

    Series_NameVariable;You can declaremore than one variable of such a type.An example of the type of our list of

    players would be:

  • 8/2/2019 Lecture Ppt 8

    27/30

    3/17/12

    Enumerations

    include

    using namespace std;

    main()

    {

    enum PizzaSize {psSmall, psMedium,psLarge, psJumbo};

  • 8/2/2019 Lecture Ppt 8

    28/30

    3/17/12

    Enumerations

    To assign your own values to the list,you could change the enumeration asfollows:

    enum PizzaSize {psSmall = 4,psMedium = 10, psLarge = 16, psJumbo= 24};

    To get the starting as you want, changethe enumeration as follows:

  • 8/2/2019 Lecture Ppt 8

    29/30

    3/17/12

    Enumerations

    #include

    using namespace std;

    main()

    {

    enum PizzaSize {psSmall = 4,psMedium, psLarge, psJumbo};

  • 8/2/2019 Lecture Ppt 8

    30/30

    3/17/12

    Enumerations

    To assign values at random, you couldchange the list of pizzas as follows:

    enum PizzaSize {psSmall = 2,psMedium, psLarge = 14, psJumbo =-5};

    This would produce:The small pizza has a value of 2 The

    medium pizza has a value of 3 The largepizza has a value of 14 The jumbo pizzahas a value of 5