Basic Logic – Chapter 3 IPC144 Week 3. Sequential Logic Statements executed sequentially – one after another Limited usefulness All programs shown so.

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

UNIT IV.
Introduction to C Programming
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.
1 A Simple C Program /* Take a number multiply it by 10 and display it */ #include main() { int number, result; printf("Type in a number \n"); scanf("%d",
BNF <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Spring Semester 2013 Lecture 5
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.
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Decisions – Chapter 3 IPC144 Week 2. IPC144 Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook)
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Problem Solving with Decisions
Flow of Control Part 1: Selection
Fundamental Programming: Fundamental Programming Introduction to C++
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
© The McGraw-Hill Companies, 2006 Chapter 2 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.
Iterations Very Useful: Ability to repeat a block of code Example:
Agenda Basic Logic Purpose if statement if / else statement
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Decisions, Decisions, Decisions Conditional Statements In Java.
Chapter#3 Part1 Structured Program Development in C++
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Looking inside classes Conditional Statements Week 4.
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Percentages Your task is to produce a mindmap for the following aspects of percentages;  Express one quantity as a percentage of another  Find a percentage.
Chapter three Conditional Statements ©
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
ALGORITHMS CONDITIONAL BRANCH CONTROL STRUCTURE
Chapter 4 (Conditional Statements)
Systems of Equations.
Week 4 – Chapter 3 Repetition.
Expression Review what is the result and type of these expressions?
Functions I Creating a programming with small logical units of code.
1) C program development 2) Selection structure
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Chapter 5 – Decisions Big Java by Cay Horstmann
Unit 1 – Section 9 “Applications of Systems of Equations”
Functions I Creating a programming with small logical units of code.
statement. Another decision statement, , creates branches for multi-
Week 3 – Repetition (ctd.)
Presentation transcript:

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

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!

If Statement Example Write C program for this problem!

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

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

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