120 likes | 216 Views
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
E N D
Decisions – Chapter 3 IPC144 Week 2
Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook) IPC144
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
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!
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!
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!
Simple Statement - example … gstAmount = 0.0; if ( quantity < 6 ) gstAmount = subtotal * 0.05; total = subtotal + gstAMount; …
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!
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; …
/* 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); }
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;
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; • }