1 / 12

Decisions – Chapter 3

Decisions – Chapter 3. IPC144 Week 2. Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook). IPC144. Sequential Logic. So far, statements are executed sequentially – one after another Limited usefulness Need programs to make decisions

Download Presentation

Decisions – Chapter 3

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. Decisions – Chapter 3 IPC144 Week 2

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

  3. SequentialLogic • 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. ConditionalLogic • 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. CompoundStatement • 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 <stdio.h> 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; • }

More Related