Successfully reported this slideshow.
Upcoming SlideShare
×

Aae oop xp_04

639 views

Published on

Published in: Technology, Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Aae oop xp_04

1. 1. Operators and Decision-Making ConstructsObjectivesIn 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. 2. Operators and Decision-Making ConstructsObjectives (Contd.) Use loop constructs while loop do…while loop for loop Scope of variables©NIIT OOPS/Lesson 4/Slide 2 of 31
3. 3. Operators and Decision-Making ConstructsTypes of Operators Arithmetic operators Assignment operators Unary operators Comparison operators Logical operators Conditional operators©NIIT OOPS/Lesson 4/Slide 3 of 31
4. 4. Operators and Decision-Making ConstructsArithmetic 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. 5. Operators and Decision-Making ConstructsArithmetic 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. 6. Operators and Decision-Making ConstructsUnary Operators Operate on one operand Are of two types: Increment operator (++) Decrement operator (--)©NIIT OOPS/Lesson 4/Slide 6 of 31
7. 7. Operators and Decision-Making ConstructsComparison 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. 8. Operators and Decision-Making ConstructsComparison 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. 9. Operators and Decision-Making ConstructsLogical 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. 10. Operators and Decision-Making ConstructsConditional 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. 11. Operators and Decision-Making ConstructsThe if…else ConstructIn 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. 12. Operators and Decision-Making ConstructsThe if…else Construct (Contd.)Syntax: if (expression) { statements; } else { statements; }©NIIT OOPS/Lesson 4/Slide 12 of 31
13. 13. Operators and Decision-Making ConstructsJust a Minute…Write a construct that assigns grades to students basedon their marks. Students who have scored marksbetween 75 and 100 are to be given grade ‘A’, those whohave 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. 14. Operators and Decision-Making ConstructsThe 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. 15. Operators and Decision-Making ConstructsLoop 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. 16. Operators and Decision-Making ConstructsThe while Loop Continues until the evaluating condition becomes falseSyntax: 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. 17. Operators and Decision-Making ConstructsJust a Minute...Write a function to display the sum of all numbersbetween 1 and 100.©NIIT OOPS/Lesson 4/Slide 17 of 31
18. 18. Operators and Decision-Making ConstructsThe do...while Loop Continues until the evaluating condition becomes false Executes the body of the loop at least onceSyntax: do { statements; } while(boolean_expr);©NIIT OOPS/Lesson 4/Slide 18 of 31
19. 19. Operators and Decision-Making ConstructsThe 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. 20. Operators and Decision-Making ConstructsThe for Loop (Contd.)Syntax: for( initialization_expr; test_expr; change_expr) { statements; }©NIIT OOPS/Lesson 4/Slide 20 of 31
21. 21. Operators and Decision-Making ConstructsThe 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. 22. Operators and Decision-Making ConstructsProblem 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. 23. Operators and Decision-Making ConstructsProblem Statement 4.D.2Write a program that displays the amount outstanding forall customers. The amount outstanding should bedisplayed in an ascending order.The amount outstanding is represented as an array offloat 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. 24. Operators and Decision-Making ConstructsProblem Statement 4.P.1Write a program to accept the salaries of 10 employeesfrom the user and displays them in descending order forall the employees. If the user enters zero, the programshould display the message “The amount should begreater than zero” and accept the value again.©NIIT OOPS/Lesson 4/Slide 24 of 31
25. 25. Operators and Decision-Making ConstructsThe Scope of a VariableFalls under three heads: File scope Local scope Class scope©NIIT OOPS/Lesson 4/Slide 25 of 31
26. 26. Operators and Decision-Making ConstructsFile 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. 27. Operators and Decision-Making ConstructsLocal 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. 28. Operators and Decision-Making ConstructsClass 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. 29. Operators and Decision-Making ConstructsSummaryIn 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 relationaloperators, are used to compare two values©NIIT OOPS/Lesson 4/Slide 29 of 31
30. 30. Operators and Decision-Making ConstructsSummary (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. 31. Operators and Decision-Making ConstructsSummary (Contd.) The break and continue statements are used to control the program flow within a loopThe 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