Vous êtes sur la page 1sur 36

Chapter 3

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:

Expressions always reduce to a single


value.

©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:

The left operand in an assignment


expression must be a single variable.

©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:

(++a) has the same effect as


(a = a + 1)

©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:

An expression statement is terminated


with a semicolon. The semicolon is a
terminator, and it tells the compiler
that the statement is finished.

©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.

Programming Assignment 10:


WAP to swap the values of two variables a and b.

©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

• float and double then float is converted to double

•float and int or char or short or long then they are


converted to float

•int and double then int is converted to double

•short and int then short is converted to int

•int and long then int is converted to long

•int and unsigned int then int is converted to


unsigned int
©Brooks/Cole, 2004
THE RULE IS ALWAYS TO PROMOTE THE
SMALLER TYPE TO THAT OF THE LARGER TYPE,
THERE BY PREVENTING ANY LOSS OF
PRECISION

©Brooks/Cole, 2004
TYPECASTING

Typecasting is used to convert the type of a variable,


function, object, expression or return value to
another type. This can be done in two ways:

•Implicit type conversion which is automatically done by


the compiler

•Explicit type conversion is of two types


•static cast
•dynamic cast

©Brooks/Cole, 2004
In static cast the compiler converts the casts at the
compile time. Once the program is compiled they
cannot be changed.

static_cast <type> (expression)

In dynamic cast the conversion is not determined


until the program is run.

©Brooks/Cole, 2004
C and C++ notations for typecasting

float x;
int i;

x = (float) i; // C cast notation


x = float(i); // C++ functional notation
x = static_cast<float>(i); // ANSI (standard) C++

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.

•even when the compiler could correctly cast for you, it is


sometimes better to code the cast explicitly to remind yourself
that cast is taking place.

©Brooks/Cole, 2004
BITWISE OPERATORS
char a = 60; // 60 = 0011 1100
char b = 13; // 13 = 0000 1101
int c = 0;

c = a & b; // 12 = 0000 1100


cout << "Line 1 - Value of c is : " << c << endl ;
c = a | b; // 61 = 0011 1101
cout << "Line 2 - Value of c is: " << c << endl ;
c = a ^ b; // 49 = 0011 0001
cout << "Line 3 - Value of c is: " << c << endl ;
c = ~a; // -61 = 1100 0011
cout << "Line 4 - Value of c is: " << c << endl ;
c = a << 2; // 240 = 1111 0000
cout << "Line 5 - Value of c is: " << c << endl ;
c = a >> 2; // 15 = 0000 1111
cout << "Line 6 - Value of c is: " << c << endl ;

©Brooks/Cole, 2004

Vous aimerez peut-être aussi