1 / 44

Decision Structures

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz. Chapter Topics. Relational Operators The if Statement The if - else Statement Nested if statements The if - else - if Statement. Relational Operators.

garth
Download Presentation

Decision Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Decision Structures Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

  2. Chapter Topics Relational Operators The if Statement The if-else Statement Nested if statements The if-else-if Statement MIT 12043, By S. Sabraz Nawaz

  3. Relational Operators • Relational operators in Java are used to compare two variables. • Java provides six relational operators: • The result of any relational operator is “true” or “false” MIT 12043, By S. Sabraz Nawaz

  4. Boolean Expressions int a = 3; b = 4; a == b will evaluate to false a != b will evaluate to true a > b will evaluate to false a < b will evaluate to true a >= b will evaluate to false a <= b will evaluate to true MIT 12043, By S. Sabraz Nawaz

  5. Sequence Structure MIT 12043, By S. Sabraz Nawaz

  6. Decision Structure • Programs often need more than one path of execution, however. Many algorithms require a program to execute some statements only under certain circumstances. This can be accomplished with a decision structure • In a decision structure’s simplest form, a specific action is taken only when a condition exists. If the condition does not exist, the action is not performed MIT 12043, By S. Sabraz Nawaz

  7. Decision Structure… • Simple decision structure logic MIT 12043, By S. Sabraz Nawaz

  8. Decision Structure… • Three-action decision structure logic MIT 12043, By S. Sabraz Nawaz

  9. Selection Statements • The control statement that allows alternative actions based upon conditions that are evaluated at run time are generally called selection statements • One way to code a decision structure in Java is with the if statement MIT 12043, By S. Sabraz Nawaz

  10. The if Statement The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a boolean expression is true. The if statement decides whether a section of executes or not. if (boolean expression is true) execute this statement; MIT 12043, By S. Sabraz Nawaz

  11. The if Statement… A block if statement may be modeled as: if (coldOutside){ wearCoat; wearHat; wearGloves; } Note the use of curly braces to block several statements together. MIT 12043, By S. Sabraz Nawaz

  12. if Statements and Boolean Expressions if (x > y) System.out.println("X is greater than Y"); if(x == y) System.out.println("X is equal to Y"); if(x != y) { System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is."); } MIT 12043, By S. Sabraz Nawaz

  13. Programming Style and if Statements An if statement can span more than one line; however, it is still one statement. • if (average > 95) • grade = ′A′; • is functionally equivalent to • if(average > 95) grade = ′A′; MIT 12043, By S. Sabraz Nawaz

  14. Block if Statements Conditionally executed statements can be grouped into a block by using curly braces {} to enclose them. If curly braces are used to group conditionally executed statements, the if statement is ended by the closing curly brace. if (expression) { statement1; statement2; } Curly brace ends the statement. MIT 12043, By S. Sabraz Nawaz

  15. Block if Statements Remember that when the curly braces are not used, then only the next statement after the if condition will be executed conditionally if (expression) statement1; statement2; statement3; Only this statement is conditionally executed. MIT 12043, By S. Sabraz Nawaz

  16. Example 01 • Write a Java program that takes a person’s age as input and decides whether that person is eligible to vote • If the person’s age is above 18, he or she is eligible to vote • The program should display a message whether he or she is eligible to vote MIT 12043, By S. Sabraz Nawaz

  17. Example 01… MIT 12043, By S. Sabraz Nawaz

  18. Example 02 • Write a Java program that takes a person’s age and his or her name as inputs and decides whether that person is eligible to vote • If the person’s age is above 18, he or she is eligible to vote • The program should display a message with the Name of the person and his or eligibility to vote MIT 12043, By S. Sabraz Nawaz

  19. Example 02 MIT 12043, By S. Sabraz Nawaz

  20. if-else Statements The if-else statement adds the ability to conditionally execute code when the if condition is false. if (expression) statementOrBlockIfTrue; else statementOrBlockIfFalse; It works the same way as the if statement except that, when the condition is false, the statement within the else clause executes MIT 12043, By S. Sabraz Nawaz

  21. if-else Statement Flowcharts Yes Is it cold outside? Wear shorts. Wear a coat. No MIT 12043, By S. Sabraz Nawaz

  22. Example 03 • Write a Java program that takes a person’s age and his or her name as inputs and decides whether that person is eligible tovote or not • If the person’s age is above 18, he or she is eligible to vote • The program should display a message with the Name of the person and his or eligibility to vote MIT 12043, By S. Sabraz Nawaz

  23. Example 03… MIT 12043, By S. Sabraz Nawaz

  24. Exercise • A company’s payment structure to its employees is as follows • If the Basic salary exceeds 30,000/=, the Dearness Allowance 40% the BS, otherwise 50% of the BS • If the BS exceeds 25,000/=, the House Rent Allowance is 20% of the BS, otherwise 3,000 of the BS • Income Tax is calculated at the rate of 12% if the Total Salary exceeds 50,000/= • Write a Java program that takes BS as input, calculate DA, HRA, IT, Total Salary, Net Salary, and display them all on screen MIT 12043, By S. Sabraz Nawaz

  25. Solution MIT 12043, By S. Sabraz Nawaz

  26. Solution… MIT 12043, By S. Sabraz Nawaz

  27. Nested if Statements To test more than one condition, an if statement can be nested inside another if statement If an if statement appears inside another if statement (single or block) it is called a nested ifstatement The nested if is executed only if the outer if statement results in a true condition MIT 12043, By S. Sabraz Nawaz

  28. Alignment and Nested if Statements • if (expression) • { • if (expression) • { • statement; • } • else • { • statement; • } • } • else • { • statement; • } This ifand else go together. This ifand else go together. MIT 12043, By S. Sabraz Nawaz

  29. Example 04 • For example, consider a banking program that determines whether a bank customer qualifies for a special, low interest rate on a loan • To qualify, two conditions must exist: • The customer’s salary must be at least Rs.30,000 • The customer must have held his or her current job for at least 02 years MIT 12043, By S. Sabraz Nawaz

  30. Example 04… MIT 12043, By S. Sabraz Nawaz

  31. if-else-if • The if-else-if statement tests a series of conditions • It is often simpler to test a series of conditions with the if-else-if statement than with a set of nested if-else statements MIT 12043, By S. Sabraz Nawaz

  32. if-else-ifSyntax if (expression_1) { statement; statement; etc. } else if (expression_2) { statement; statement; etc. } Insert as many else if clauses as necessary else { statement; statement; etc. } If expression_1 is true these statements are executed, and the rest of the structure is ignored. Otherwise, if expression_2 is true these statements are executed, and the rest of the structure is ignored. These statements are executed if none of the expressions above are true. MIT 12043, By S. Sabraz Nawaz

  33. Example 05 • Your lecturer has asked you to write a program that will allow a student to enter a test score and then display the grade for that score MIT 12043, By S. Sabraz Nawaz

  34. Logical Operators MIT 12043, By S. Sabraz Nawaz

  35. Logical Operators • Logical operators connect two or more relational expressions into one or reverse the logic of an expression. • Java provides two binary logical operators, • &&and || • used to combine two boolean expressions into a single expression • It also provides the unary !Operator • reverses the truth of a boolean expression MIT 12043, By S. Sabraz Nawaz

  36. Logical Operators… MIT 12043, By S. Sabraz Nawaz

  37. Logical Operators… MIT 12043, By S. Sabraz Nawaz

  38. Truth table for the && operator MIT 12043, By S. Sabraz Nawaz

  39. Truth table for the ||operator MIT 12043, By S. Sabraz Nawaz

  40. Truth table for the ! operator MIT 12043, By S. Sabraz Nawaz

  41. Example 06 – Improved Ex 05

  42. Will be continue in next lecture… MIT 12043, By S. Sabraz Nawaz

More Related