Presentation is loading. Please wait.

Presentation is loading. Please wait.

The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy.

Similar presentations


Presentation on theme: "The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy."— Presentation transcript:

1 The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy

2 Draw a flowchart and write a program that simulates the distance and fare computation for a car based upon its state. For this problem, we define the state of the car to be the quantity of gas the car contains. Inputs to the program The number of gallons of gas in the tank The fuel efficiency in miles per gallon The price of gas per gallon Outputs from the program How far the car can go with the gas in the tank The cost of gas per 100 miles Ensure that the input and output prompts are as user- friendly as possible. Project Name testsubmission2

3 Further Analysis Validations on inputs Ensure that all inputs are non- negative (greater than or equal to zero) Can you think of any other validation? Provide more if you can.

4 The pseudo-code – first cut 1.Define g = Number of Gallons in tank, e = Fuel efficiency (miles / gallon), p = Price / gallon, d = Distance and p_100 = Price / 100 miles 2.Input g, e, p 3.IF e is non-zero THEN 4.Compute d ← g × e 5.Compute p_100 ← (100 × p) / e 6.Display d, p_100 7.ELSE 8.Display error message 9.ENDIF 10.Stop

5 The pseudo-code – second cut 1.Define g = Number of Gallons in tank, e = Fuel efficiency (miles / gallon), p = Price / gallon, d = Distance and p_100 = Price / 100 miles 2.Input g, e, p 3.IF (e is zero) THEN { 4.Display error message 5.} 6.ELSE { 7.IF (g is negative) THEN { 8.Display error message 9.} 10.ELSE { 11.IF (p is negative) THEN { 12.Display error message 13.} 14.} 15.} 16.Compute d ← g × e 17.Compute p_100 ← (100 × p) / e 18.Display d, p_100 19.Stop

6 Structure of the If-statement if ( logical-expression ) { C++ statement-set-1 ; } else { C++ statement-set-2 ; }

7 More on the if-statement The else part is not mandatory if ( logical-expression ) { C++ statement-set-1 ; } C++ statement-set-2 ;

8 Logical Expressions Operands –These operands are variables Logical Operators – connectives –These are logical operators

9 Types of logical expressions These evaluate to exactly one of the following: –TRUE –FALSE Simple –Operand1 logical–operator Operand2 Compound –Many simple expressions –Connected by connectives such as AND, OR

10 The exercise – using the if- statement int main() { if (efficiency != 0) { Execute the lines in your code } else { Display error message “one of your inputs is invalid” } return (0) ; }

11 The exercise - variant 1 input gasquantity, price and efficiency if (gasquantity <= 0) { display error } else { if (price < 0) { display error } else { if (efficiency == 0) { display error } else { Execute the calculations Execute the output display statements }

12 The exercise - variant 2 input gasquantity if (gasquantity <= 0) { display error “invalid gas quantity” } else { input price if (price < 0) { display error “invalid price” } else { input efficiency if (efficiency == 0) { display error “invalid efficiency” } else { Execute the calculations Execute the output display statements }

13 Multiple If’s and Else’s if (logical-expression1) { C++ statement-set-1 ; } else if (logical-expression2) { C++ statement-set-2 ; } else if (logical-expression3) { C++ statement-set-3 ; } … else { C++ statement-set-default ; }

14 Acknowledgments These exercises have been taken from "Computing Concepts with C++ Essentials" by Cay Horstmann, John Wiley & Sons, Inc., 1997


Download ppt "The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy."

Similar presentations


Ads by Google