Académique Documents
Professionnel Documents
Culture Documents
Structure
of a
C++ Program
©Brooks/Cole, 2004
OBJECTIVES
After studying this chapter you will be able to:
q Understand the concept of and use expressions.
q Identify the seven types of C++ expressions.
q Use basic expressions in a program.
q Assign expression values to a variable.
q Evaluate expressions using precedence and associativity.
q Understand and use expression side effects.
q Understand and use compound statements.
q Understand that good functions are simple and short (KISS).
q Use parentheses to clarify code.
q Communicate clearly with the user through well written prompts.
©Brooks/Cole, 2004
3.1
Expressions
©Brooks/Cole, 2004
Note:
©Brooks/Cole, 2004
Figure 3-1 C++ expression format
©Brooks/Cole, 2004
Figure 3-2 Primary expressions
©Brooks/Cole, 2004
Figure 3-3 Binary expressions
©Brooks/Cole, 2004
Figure 3-4 Assignment expression
©Brooks/Cole, 2004
Note:
©Brooks/Cole, 2004
Note:
Assignment Expression
The assignment expression has a value and
a result.
• The value of the total expression is the value
of the expression on the right of the assignment
operator (=).
• The result places the expression value in the
operator on the left of the assignment operator.
©Brooks/Cole, 2004
COMPOUND ASSIGNMENT
Compound Expression Equivalent Simple Expression
x*=y x=x*y
x/=y x=x/y
x+=y x=x+y
x-=y x=x-y
x%=y x=x%y
©Brooks/Cole, 2004
Figure 3-5 Postfix expressions
©Brooks/Cole, 2004
Figure 3-6 Result of postfix a++
©Brooks/Cole, 2004
Figure 3-7 Unary expressions
©Brooks/Cole, 2004
Figure 3-8 Result of prefix ++a
©Brooks/Cole, 2004
Note:
©Brooks/Cole, 2004
3.2
Precedence and
Associativity
©Brooks/Cole, 2004
Figure 3-9 Associativity
©Brooks/Cole, 2004
Figure 3-10 Left associativity
©Brooks/Cole, 2004
Figure 3-11 Right associativity
©Brooks/Cole, 2004
Figure 3-12 Types of statements
©Brooks/Cole, 2004
Note:
©Brooks/Cole, 2004
Figure 3-13 Compound statement
©Brooks/Cole, 2004
3.7
Programs
©Brooks/Cole, 2004
Programming Assignment 5:
WAP that extracts and print the rightmost second digit of an integer.
Programming Assignment 6:
WAP to print the sum and product of digits of a 5 digit integer
Input by the user.
Programming Assignment 7:
WAP to reverse a five digit number input by the user.
Programming Assignment 8:
WAP to convert temperature from Fahrenheit to degree Celsius.
Programming Assignment 9:
A Fibonacci number is a member of a set in which each number is the
sum of the previous two numbers. E.g
0, 1, 1, 2, 3, 5, 8, 13, 21,___, ____, _____ WAP that calculates and
print the next three numbers in the Fibonacci series. You are to use only
three variables fib1, fib2, fib3.
©Brooks/Cole, 2004
Operator Name Associativity Operators
Primary scope resolution left to right ::
() [ ] . -> dynamic_cast
Primary left to right
typeid
Unary right to left ++ -- + - ! ~ & * (type_
name)
sizeof new delete
C++ Pointer to Member left to right .*->*
Multiplicative left to right * / %
Additive left to right + -
Bitwise Shift left to right << >>
Relational left to right < > <= >=
Equality left to right == !=
Bitwise AND left to right &
Bitwise Exclusive OR left to right ^
Bitwise Inclusive OR left to right |
Logical AND left to right &&
Logical OR left to right ||
Conditional right to left ?:
Assignment right to left = +=
-= *= /= <<= >>= %=
&= ^= |=
Comma left to right ,
©Brooks/Cole, 2004
EXERCISES :
1.Evaluate : i=2 * 3 / 4 + 4 / 4 + 8 – 2 + 5 / 8
2.--a * (3+b) /2 – c++ * b a=3, b=4, c=5
3. int a=10,b=20;
int c;
c=a++ + ++b + a + b;
cout<<a<<" "<<b<<" "<<c;
4. int a=10;
int c;
c=a++ + a++;
cout<<a<<" "<<c;
5. int a=10;
int b=11;
a=a+=b+=a++;
cout<<a;
©Brooks/Cole, 2004
Anwers :
1.8
2.-13
3.62
4.12 20
5.32
©Brooks/Cole, 2004
MIXED TYPE EXPRESSIONS
©Brooks/Cole, 2004
TYPECASTING
©Brooks/Cole, 2004
In static cast the compiler converts the casts at the
compile time. Once the program is compiled they
cannot be changed.
©Brooks/Cole, 2004
C and C++ notations for typecasting
float x;
int i;
159.234 OO Programming *
©Brooks/Cole, 2004
int a=10,b=3;
float c;
c=a/b;
cout<<c; Ans 3
int a=10,b=3;
float c;
c=static_cast<float>(a)/b;
cout<<c; Ans 3.333333
©Brooks/Cole, 2004
•casts are technically operators. As an operator, it is unary and
has the same precedence as any other operator.
©Brooks/Cole, 2004
BITWISE OPERATORS
char a = 60; // 60 = 0011 1100
char b = 13; // 13 = 0000 1101
int c = 0;
©Brooks/Cole, 2004