Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decisions – Chapter 3 IPC144 Week 2. IPC144 Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook)

Similar presentations


Presentation on theme: "Decisions – Chapter 3 IPC144 Week 2. IPC144 Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook)"— Presentation transcript:

1 Decisions – Chapter 3 IPC144 Week 2

2 IPC144 Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook)

3 Sequential Logic So far, statements are executed sequentially – one after another Limited usefulness Need programs to make decisions –if something is true, perform some statements –if false, 2 options: don’t do anything : “if” statement perform some other statements “if-else” statement

4 Example : movie tickets Movie tickets are to be sold using the following prices: Adult: $10 Child under 6 years: $4 Student (6-19 years):$7 Senior citizen (65 yrs and over):$7 Express this using conditional logic!

5 Conditional Logic Condition is tested and one action or another will be performed A condition in C is either true or false uses comparisons (operators: == != >=) Example of condition: age > 19 One type of conditional logic in C: if statement Simple if statement syntax if ( condition ) single statement; indentation for programmer friendliness!

6 Simple Conditional Logic Example Calculate the cost of doughnuts bought at $0.60 per doughnut GST of 5% is charged if fewer than 6 doughnuts bought Display the amount of GST charged and the total amount owed Write a C program to do this!

7 Simple Statement - example … gstAmount = 0.0; if ( quantity < 6 ) gstAmount = subtotal * 0.05; total = subtotal + gstAMount; …

8 Compound Statement A group of C statements can be treated as one statement by enclosing within braces (curly brackets): { } if ( condition ) { statement; … statement; } alignment and indentation for programmer friendliness!

9 Compound Statement - example Assume PST of 8% is also to be charged if fewer than 6 doughnuts bought … gstAmount = 0.0; pstAmount = 0.0; if ( quantity < 6 ) { gstAmount = subtotal * 0.05; pstAmount = subtotal * 0.08; } total = subtotal + gstAmount + pstAmount; …

10 /* Calculate cost of doughnuts including GST & PST */ #include main() { int quantity; double subtotal, total, pstAmount, gstAmount; printf("Enter number of doughnuts: "); scanf("%d", &quantity); subtotal = quantity * 0.60; pstAmount = 0.0; gstAmount = 0.0; if ( quantity < 6 ) { gstAmount = subtotal * 0.05; pstAmount = subtotal * 0.08; } total = subtotal + gstAmount + pstAmount; printf("Total: $%.2lf\n", total); printf("GST : $%.2lf\n", gstAmount); printf("PST : $%.2lf\n", pstAmount); }

11 If-else Statement if ( condition ) single statement; else single statement; Example: /* toll road rate depends on peak, or not peak */ if ( peak == 1 ) rate = 0.30; else rate = 0.25;

12 If-else with Compound Statements statement can be single, or compound Example: if ( peak == 1 ) { printf( " peak rate applies\n " ); rate = 0.30; } else { printf( " non peak rate applies\n " ); rate = 0.25; }


Download ppt "Decisions – Chapter 3 IPC144 Week 2. IPC144 Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook)"

Similar presentations


Ads by Google