Most programs perform arithmetic calculations to manipulate numeric data. We use arithmetic operators for such calculations. All arithmetic operators are binary  operators i.e., they take two operands e.g.

number1 + number2

Here ‘+’ is the operator. Number1 and number2 are the operands. Only + and – are unary operators e.g.

x = -y


p = +x*y

Summary of arithmetic operators

C++ operations C++ arithmetic operators Algebraic expression C++ expression Examples
Addition + F + 7 F + 7 5 + 2 is 7
Subtraction - p – c p – c  5 – 2 is 3
Multiplication * bm or b.m b*m 5 * 2 is 10
Division / x/y or x


x / y 5 / 2 is 2.5
Modulus % r mod s r % s 5 % 2 is 1

Parenthesis can be used for grouping sub expressions

Data type of each variable is mandatory to specify in C++. What about the data type of an expression

Example:  number1 + number2

A C++ expression is of type int only and only if its operands are type int. A C++ expression is of type float only and only if any of its operands is of type float. An expression containing both integer and floating-point operands is called mixed-type expression.

When an assignment  statement is executed, the expression is first evaluated and then result is assigned to the variable on the left of the assignment. Consider the following example

float a;
a = 10 / 3;

What will the value of a?

Arithmetic expressions with multiple operators

Expressions can include both unary and binary operators. In order to write and understand expressions, we must know the rules followed by C++ compiler for evaluating expressions

Example: x + y / z
is + is performed before / or vice versa

Example: x / y + z

is evaluated as (x/y) *z or x/(y*z). These rules are based on familiar algebraic rules

Rules of Precedence

Operators expressions contained in parenthesis are evaluated first. They are used to force the order of evaluation to occur in any sequence desired by programmer. They are said to be at the “Highest level of precedence”. In case of nested or embedded parenthesis like:

( ( a + b ) * c)

The operators in the innermost parenthesis are applied first.

Following is the order of precedence is applied:

  • Unary operators are applied after the parenthesis
    • e.g. ( ( -a + b ) * c )
  • Multiplication, division, and modulus are applied next
    • e.g. ( ( -a + b * c ) * d )
  • Addition and subtraction operations are applied last.

What if the multiple operators of the same precedence are encountered in the expression?

Then Associativity rule comes in play. Binary operators in the same sub expression and at the same precedence are evaluated from left to right (left associativity). If an expression contains multiple multiplication, division, and modulus operators, they are applied from left to right e.g. (( -a + b * c / d ) * e ). If an expression contains multiple addition or division operators like (a + b – c). Unary operators are in same expression and at same precedence are evaluated from right to left (right associativity).

C++ Control Statements

Sequential execution is the default behavior of C++. Statements are processed one by one, one after the other in a program

Problem: We often want a mechanism to execute a statement that is next to the next one in the sequence.

This phenomenon is called transfer of control. Control statements or decision structures comes in play.

Early in 1960s, goto statement was used for the transfer of control. That was considered the biggest source of complexity. Then comes the advent of structure programming.
Types of control statements
  1. Sequence structures
  2. Selection structures
  3. Repetition structures

1. Sequence Structures in C++

The sequence structures is built into C++. Computer executes the statements in the order in which they are written i.e. sequence. This is default behavior of C++.

2. Selection Structures in C++

These structures facilitate the transfer of control based on certain condition for the selection of C++ statements.

C++ provides following selection structures

  • if selection structure
  • if  else selection structure
  • switch selection structure

What is a Condition?

A condition is a Boolean expression. It will always yields either True or False
Syntax: LHS  Operator  RHS
Example:  (salary > 10,000)
  • ==, != (equality operator)
  • >, <, >=, <= (relational operator)

IF Statement

It is a simple selection statement. It is a single selection statement. Because it selects or ignores a single action. It either performs an action if a condition is true or skips if a condition is false

if (condition) {
   //body comprises of action(s)
If student grade is greater or equal to 60. Then Print “Passed”. This pseudo-code can be written in C++ as

if ( grade = 60 ) {

Note that the if statement is a single-entry/single-exit statement.

IF..ELSE Statement

Double selection statements. It selects between two statements. It allows the programmer to specify  an action to perform when condition is true and a different  action to perform when the condition is false.

if (condition) {
   //body comprises of action(s)
} else {
   //body comprises of action(s)
If student grade is greater or equal to 60. Then Print “Passed”. Otherwise print “failed”.

if ( grade = 60 ) {
} else {

Note that the if statement is a single-entry/single-exit statement.

Nested IF .. ELSE statements

If student grade is greater or equal to 90
  Print “A”
  If student grade is greater or equal to 80
    Print “B”
    If student grade is greater or equal to 70
      ¨Print “C”
      ¨If student grade is greater or equal to 60
         ¨Print “D”
         Print “F”


A food of thought

if  (x > 5)
  If (y > 5)
  cout<<" x and y are greater than 5";
  cout<< " x is less or equal to 5";

What is the output of this code snippet for x = 6 and y = 4? :-)

Dangling Else problem

C++ compiler always associates an else with the immediately preceding if. This behavior leads to the dangling-else problem. To overcome this problem always use braces {}

if  (x > 5)
}If (y > 5)
¨cout<<" x and y are greater than 5";
}cout<< " x is less or equal to 5";

3. SWITCH Statement

Multi selection statement. Switch multiple selection statements to perform different action based on the values of variable or expression. Each action is associated with a constant integral expression. Known as base of the switch statement.

switch (controlling expression)
case 1:
set of statements
case 2:
set of statements
case 3:
set of statements
default statements

The switch statement consists of series of case labels and an optional default case. The value of controlling expression is checked against every case label break statement causes program control to proceed with the first statement after the switch in the program. Without break statement, each time a match occurs in the switch, subsequent cases also executes until switch statement ends or break statement encountered. If no match is found between controlling expression and case labels, default case executes. The default case can be placed every where in the switch statement. Common practice is to place default case last. The default clause does not necessarily requires break statement. For the clarity and symmetry of program it can be place in default clause also

Note: Specifying an expression including variables (e.g., a+b) in a switch statement is a syntax error

Programming Exercises

Task 1:

What is the output of the program below?

#include <iostream.h>
int main()
int n;
cout << (n = 4) << endl;
cout << (n == 4) << endl;
cout << (n > 3) << endl;
cout << (n < 4) << endl;
cout << (n = 0) << endl;
cout << (n == 0) << endl;
cout << (n > 0) << endl;
cout << (n && 4) << endl;
cout << (n || 4) << endl;
cout << (!n) << endl;
return 0;

Task 2:

Write a C++ program that rearranges any two values stored in x and y so that the smaller number is in x and the larger number is in y.

Task 3:

Write the following code with switch statement

If letter grade of student is A
  Print “Excellent Performance”
If letter grade of student is B
  Print “Good Performance”
If letter grade of student is C
  Print “Satisfactory Performance”
If letter grade of student is D
  Print “Unsatisfactory Performance”
If letter grade of student is F
  Print “Poor Performance”
  Print “Invalid Letter Grade”



Tagged with: C/C++ language

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.