1 / 9

Basic Logic – Chapter 3

Basic Logic – Chapter 3. IPC144 Week 3. Sequential Logic. Statements executed sequentially – one after another Limited usefulness All programs shown so far have been sequential Often need to perform logic based on conditions  conditional logic. Conditional example : movie tickets.

sean-morrow
Download Presentation

Basic Logic – 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. Basic Logic – Chapter 3 IPC144 Week 3

  2. Sequential Logic • Statements executed sequentially – one after another • Limited usefulness • All programs shown so far have been sequential • Often need to perform logic based on conditions  conditional logic

  3. Conditional 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!

  4. Conditional Logic • Condition tested and one action or another will be performed • A condition in C can include variables, literal values (eg 3, "a", "$", …), relational operators (> < == >= <= !=), arithmetic operators (= - * / …), … • Example of condition: age > 19 • One method of implementing conditional logic in C: if statement • Simple if statement syntax if (condition) statement;

  5. Simple Conditional Logic Example • Calculate the cost of doughnuts bought at $0.60 per doughnut • GST of 7% 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!

  6. If Statement Example Write C program for this problem!

  7. Code blocks • Groups of C statements can be treated as one logical statement by enclosing within brace {} brackets • Group of statements within main are treated as a big block of code • Can use additional {} within main to group statements, eg within an if statement

  8. Code blocks example • Assume PST of 8% is also to be charged if fewer than 6 doughnuts bought … if (qty < 6) { GSTamt = subtotal * 0.07; PSTamt = subtotal * 0.08; } total = subtotal + GSTamt + PSTamt; …

  9. main() { int qty; double subtotal, total, PSTamt, GSTamt =0; printf("Enter number of dougnuts: ") scanf("%d", &qty); subtotal = qty * 0.60; if (qty < 6) { GSTamt = subtotal * 0.07; PSTamt = subtotal * 0.08; } total = subtotal + GSTamt + PSTamt; printf("Total owed: $%.2lf , GST:$%.2lf, PST:$.2lf\n", total, GSTamt, PSTamt); }

More Related