More Related Content
Similar to Aae oop xp_04 (20)
Aae oop xp_04
- 1. Operators and Decision-Making Constructs
Objectives
In this lesson, you will learn to:
Use the following operators:
Arithmetic
Assignment
Unary
Comparison
Logical
Use decision-making constructs
if…else constructs
switch…case constructs
©NIIT OOPS/Lesson 4/Slide 1 of 31
- 2. Operators and Decision-Making Constructs
Objectives (Contd.)
Use loop constructs
while loop
do…while loop
for loop
Scope of variables
©NIIT OOPS/Lesson 4/Slide 2 of 31
- 3. Operators and Decision-Making Constructs
Types of Operators
Arithmetic operators
Assignment operators
Unary operators
Comparison operators
Logical operators
Conditional operators
©NIIT OOPS/Lesson 4/Slide 3 of 31
- 4. Operators and Decision-Making Constructs
Arithmetic Operators
Are used to perform arithmetic operations
Are used in an order according to the
precedence rules
Type Operators Associativity
Value construction () Innermost to outermost
Multiplicative * / % Left to right
Additive + - Left to right
©NIIT OOPS/Lesson 4/Slide 4 of 31
- 5. Operators and Decision-Making Constructs
Arithmetic Assignment Operators
Are used to assign the value of the right operand to
the left
Are as follows: =, +=, -=, /=, *=, %=
©NIIT OOPS/Lesson 4/Slide 5 of 31
- 6. Operators and Decision-Making Constructs
Unary Operators
Operate on one operand
Are of two types:
Increment operator (++)
Decrement operator (--)
©NIIT OOPS/Lesson 4/Slide 6 of 31
- 7. Operators and Decision-Making Constructs
Comparison Operators
Are also called relational operators
Are used to compare two values
Evaluate to true or false
©NIIT OOPS/Lesson 4/Slide 7 of 31
- 8. Operators and Decision-Making Constructs
Comparison Operators (Contd.)
Operator Description Example Explanation
== Evaluates whether or not the x == y Returns true if the values are equal and false
operands are equal. otherwise
Evaluates whether or not the x != y Returns true if the values are not equal and
!=
operands are not equal false otherwise
Evaluates whether or not the left x y
Returns true if x is greater than y and false
operand is greater than the right
otherwise
operand
Evaluates whether or not the left x y
Returns true if x is less than y and false
operand is less than the right
otherwise
operand
Evaluates whether or not the left x = y
Returns true if x is greater than or equal to y
= operand is greater than or equal to
and false otherwise
the right operand
Evaluates whether or not the left x = y
Returns true if x is less than or equal to y and
= operand is less than or equal to the
false otherwise
right operand
©NIIT OOPS/Lesson 4/Slide 8 of 31
- 9. Operators and Decision-Making Constructs
Logical Operators
Are used to combine two or more expressions
Are of three types:
AND ()
OR (||)
NOT (!)
©NIIT OOPS/Lesson 4/Slide 9 of 31
- 10. Operators and Decision-Making Constructs
Conditional Constructs
Control the flow of a program
Allow selective execution of statements
Use comparison operators for evaluating conditions
Are of two types:
The if…else construct
The switch…case construct
©NIIT OOPS/Lesson 4/Slide 10 of 31
- 11. Operators and Decision-Making Constructs
The if…else Construct
In an if…else block of statements:
Condition is evaluated first
If condition is true, statements in the immediate block
are executed
If condition is false, statements in the else block are
executed
©NIIT OOPS/Lesson 4/Slide 11 of 31
- 12. Operators and Decision-Making Constructs
The if…else Construct (Contd.)
Syntax:
if (expression)
{
statements;
}
else
{
statements;
}
©NIIT OOPS/Lesson 4/Slide 12 of 31
- 13. Operators and Decision-Making Constructs
Just a Minute…
Write a construct that assigns grades to students based
on their marks. Students who have scored marks
between 75 and 100 are to be given grade ‘A’, those who
have scored between 50 and 75 are to be given grade
‘B’, and the rest of them should be given grade ‘C’.
©NIIT OOPS/Lesson 4/Slide 13 of 31
- 14. Operators and Decision-Making Constructs
The switch…case Construct
Is used when there are multiple values for a variable
Evaluates condition-variable of the switch statement
and compares with each case constant
Requires a break statement to exit from its body
Uses default keyword for associating any other
statements other than mentioned in the case constant
©NIIT OOPS/Lesson 4/Slide 14 of 31
- 15. Operators and Decision-Making Constructs
Loop Constructs
Cause a section of a program to be repeated a certain
number of times
Are of three types:
while loop
do…while loop
for loop
©NIIT OOPS/Lesson 4/Slide 15 of 31
- 16. Operators and Decision-Making Constructs
The while Loop
Continues until the evaluating condition becomes
false
Syntax:
while (expression)
{
statements;
}
Requires the continue statement to return the
control to the beginning of while loop skipping any
other statements after the continue keyword
©NIIT OOPS/Lesson 4/Slide 16 of 31
- 17. Operators and Decision-Making Constructs
Just a Minute...
Write a function to display the sum of all numbers
between 1 and 100.
©NIIT OOPS/Lesson 4/Slide 17 of 31
- 18. Operators and Decision-Making Constructs
The do...while Loop
Continues until the evaluating condition becomes
false
Executes the body of the loop at least once
Syntax:
do
{
statements;
} while(boolean_expr);
©NIIT OOPS/Lesson 4/Slide 18 of 31
- 19. Operators and Decision-Making Constructs
The for Loop
Provides a compact way of specifying statements that
control the repetition of the steps within the loop
Contains three expressions:
The initialization expression
The test expression
The increment/decrement expression
©NIIT OOPS/Lesson 4/Slide 19 of 31
- 20. Operators and Decision-Making Constructs
The for Loop (Contd.)
Syntax:
for( initialization_expr; test_expr; change_expr)
{
statements;
}
©NIIT OOPS/Lesson 4/Slide 20 of 31
- 21. Operators and Decision-Making Constructs
The break and continue statement
The break statement is used to exit a loop before the
loop condition is re-evaluated after iteration
The continue statement is used to skip all the
subsequent instructions and take the control back to
the loop
©NIIT OOPS/Lesson 4/Slide 21 of 31
- 22. Operators and Decision-Making Constructs
Problem Statement 4.D.1
Write a program that will reverse an accepted string
and copy it into another string.
©NIIT OOPS/Lesson 4/Slide 22 of 31
- 23. Operators and Decision-Making Constructs
Problem Statement 4.D.2
Write a program that displays the amount outstanding for
all customers. The amount outstanding should be
displayed in an ascending order.
The amount outstanding is represented as an array of
float values:
float amounts[10] =
{200.5,323,0,100.7,314,523,256,10.90,
553.90,0};
©NIIT OOPS/Lesson 4/Slide 23 of 31
- 24. Operators and Decision-Making Constructs
Problem Statement 4.P.1
Write a program to accept the salaries of 10 employees
from the user and displays them in descending order for
all the employees. If the user enters zero, the program
should display the message “The amount should be
greater than zero” and accept the value again.
©NIIT OOPS/Lesson 4/Slide 24 of 31
- 25. Operators and Decision-Making Constructs
The Scope of a Variable
Falls under three heads:
File scope
Local scope
Class scope
©NIIT OOPS/Lesson 4/Slide 25 of 31
- 26. Operators and Decision-Making Constructs
File Scope
Is considered to be the outermost scope
Variables are accessible throughout the program file
Are called global variables
©NIIT OOPS/Lesson 4/Slide 26 of 31
- 27. Operators and Decision-Making Constructs
Local Scope
Is defined as being limited to the braces of a function
or a control structure like for, while, and if
Are called local variables
©NIIT OOPS/Lesson 4/Slide 27 of 31
- 28. Operators and Decision-Making Constructs
Class Scope
Variables are accessible only within the class
Variables have the flexibility of being accessed
outside the class by declaring them as public
©NIIT OOPS/Lesson 4/Slide 28 of 31
- 29. Operators and Decision-Making Constructs
Summary
In this lesson, you learned that:
Operators are used to compute and compare values
and test multiple conditions
You use arithmetic operators to perform arithmetic
operations, such as addition, subtraction,
multiplication, and division
You can also use arithmetic assignment operators to
perform arithmetic operations
The unary operators, such as the increment and
decrement operators operate on one operand
Comparison operators, which are also called relational
operators, are used to compare two values
©NIIT OOPS/Lesson 4/Slide 29 of 31
- 30. Operators and Decision-Making Constructs
Summary (Contd.)
Logical operators are used to combine two or more
expressions
Conditional constructs are used to allow the selective
execution of statements. The conditional constructs in
C++ are:
if...else
switch...case
Looping constructs are used when you want a section
of a program to be repeated a certain number of
times. C++ offers the following looping constructs:
while
do...while
for
©NIIT OOPS/Lesson 4/Slide 30 of 31
- 31. Operators and Decision-Making Constructs
Summary (Contd.)
The break and continue statements are used to
control the program flow within a loop
The scope of a variable specifies its accessibility
C++ features three types of scopes: file, class, and
local scope
©NIIT OOPS/Lesson 4/Slide 31 of 31