Thursday 9 July 2015

C++ Control Structures (Part 2)

Last time we discussed some control structures, today we will discuss switch control structures, assignment and increment decrement operators.

Rvalues and Lvalues:

  • Rvalues: these are the expressions that appear on left side of equation, they can be changed (i.e., variables) x = 4;
  • Lvalues: these are expressions that appear on right side of equation, they include constants such as numbers (i.e. cannot write 4 = x;)
  • Lvalues can be used as Rvalues, but not vice versa.
'Switch' Multiple-Selection Structure:

switch structure tests variable/expression for multiple possible values, a Specific action is to be performed based on the value of an ‘integral expression' (Combination of characters and integers that evaluate to a constant integer value)

Switch statement consists of two parts:

  • a controlling expression ie switch (…………)
  • and a series of case labels and optional default case, Case labels contain the possible values of the controlling expression
  • switch statement compares the value of controlling expression with each case label

flow chart of a switch selection structure (remember, we select one out of many choices) :
the following code example shows the usage of a switch structure:





Output example of this code:


Assignment Operators:
Assignment operators are used for abbreviating assignment expressions
–Addition assignment operator
 c = c + 3;      
c += 3; 
Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator = expression;
Other assignment operators are:
                                   d -= 4     (d = d - 4)
                                   e *= 5     (e = e * 5)
                                   f /= 3     (f = f / 3)
                                   g %= 9     (g = g % 9) 

Increment and Decrement Operator:
Increment operator (++) - can be used instead of
         c += 1 OR c = c + 1
Decrement operator (--) - can be used instead of
         c -= 1 OR c = c - 1
Preincrement:
         Whenever the operator is used before the variable (++c or ––c) then first the variable is changed, then the expression it is in is evaluated.
Postincrement:
         Whenever the operator is used after the variable (c++ or c--), the the expression the variable is in executes, then the variable is changed
for example, If c = 5, then
         cout << ++c;
       c is changed to 6, then printed out
      cout << c++;
       Prints out 5 (cout is executed before the increment, c then becomes 6)
When variables are not in expression:
then preincrementing and postincrementing have same effect
++c;
cout << c;
and 
c++;
cout << c;
are the same





About the Author

Unknown

Editorial Team

Post a Comment

 
Codexify © 2015 - Designed by Templateism.com