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

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Basic Logic – Chapter 3 IPC144 Week 3. Sequential Logic Statements executed sequentially – one after another Limited usefulness All programs shown so.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
1 CSC103: Introduction to Computer and Programming Lecture No 8.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
1 Fall 2007ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Chapter 6 Horstmann Programs that make decisions: the IF command.
1 Fall 2008ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
Announcements Quiz 1 Posted on blackboard Handed Back (and Gone Over)Next Monday “Only a Quiz” Counts 5% of Final Grade Exam 1 Counts 10%
true (any other value but zero) false (zero) expression Statement 2
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Unit 3 Lesson 8 Selection Structures Mr. Dave Clausen La Cañada High School.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Problem Solving with Decisions
B065: PROGRAMMING OPERATORS AND SELECTION 2. Starter  What do each of the following mean? Write an example of how each could be used. Symbol = <
Conditionals & boolean operators
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Flow of Control Part 1: Selection
Simple Control Structures IF, IF-ELSE statements in C.
CONTROLLING PROGRAM FLOW
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Decisions, Decisions, Decisions Conditional Statements In Java.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Lesson thirteen Conditional Statement "if- else" ©
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Introduction to Programming Using C Basic Logic. 2 Contents Conditional programming If statement Relational operators Compound statements Loops While.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CNG 140 C Programming (Lecture set 3)
Introduction to C++ Programming Language
Chapter 4 (Conditional Statements)
Flow of Control.
Week 4 – Chapter 3 Repetition.
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Pages:51-59 Section: Control1 : decisions
Visual Basic – Decision Statements
Selection Statements.
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Computer Programming Basics
Pages:51-59 Section: Control1 : decisions
Chapter 5: The Selection Structure
Chapter 5 – Decisions Big Java by Cay Horstmann
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Presentation transcript:

Decisions – Chapter 3 IPC144 Week 2

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

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

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!

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!

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; …

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!

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 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; }