Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout << "Input the price: $"; cin >> p;

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
The If/Else Statement, Boolean Flags, and Menus Page 180
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
If Statements & Relational Operators, Part 2 Programming.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
1 LoopsBranching Condition Statement list T F Condition Statement list T F.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Programming Languages
Input & Output: Console
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Basic Of Computer Science
Chapter 02 (Part III) Introduction to C++ Programming.
CONTROLLING PROGRAM FLOW
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Control Structures Repetition or Iteration or Looping Part II.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Boolean Expressions and if-else Statements Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Parameters I string ask(string nz) { string thing; cout
Calvin College Controlling Behavior The if, switch and for Statements.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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)
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Variables, Identifiers, Assignments, Input/Output
Basic concepts of C++ Presented by Prof. Satyajit De
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4: Making Decisions.
مبانی برنامه‌سازی با C++ جلسه دوم
Compound Assignment Operators in C++
Introduction to C++ Programming
Counting Loops.
If Statements.
2.6 The if/else Selection Structure
Fundamental Programming
C++ Programming Language Lecture 4 C++ Basics – Part II
Presentation transcript:

Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout << "Input the price: $"; cin >> p; return p; }

More on cin and cout double buy_two() {double p1, p2; cout << "Input two prices: $"; cin >> p1 >> p2; cout << "You have input " << p1 << " and " << p2 << endl; cout << "After Tax, there are $" << p1+tax(p1, 0.075) << " and $" << p2+tax(p2, 0.075) << endl; return p1+p2; } int main() { const double tax_rate=0.075; double sum=0; sum = buy_two(); cout << "Total: $" << sum+tax(sum, tax_rate) << "\n"; return 0; } We can’t use tax_rate here All elements to be inserted must be ready before any of them sent to the standard out-stream (the screen). They will be evaluated from left to right. Input two prices: $30 70 You have input 30 and 70 After Tax, there are $32.25 and $75.25 Total: $107.5 Press any key to continue

Assignment operator = a = 12+1; The assignment also return a value, which is the value of the expression in the right hand side, i.e = is right-associative. a = 12+ buy_one(); a = b = c = d = 2; is same as a = ( b = ( c = (d=2))); d = c – b – a – a; is same as d = (((c – b)– a)– a); a = b + c = d – 2; is not allowed are valid a = b + (c = d) – 2; a = b = c = d + 2; cout << (a=b+3) << endl;

A little trick of using assignment sum = buy_one(); cout << "After Tax: $" << (sum = sum + tax(sum, tax_rate)) << endl; cout << "Variable sum = " << sum << endl; cout << "Total after Tax: $" << (sum = sum) + tax(sum=buy_one(), tax_rate) + sum << endl; cout << "Variable sum = " << sum << endl; Input the price: $100 After Tax: $107.5 Variable sum = Input the price: $200 Total after Tax: $322.5 Variable sum = 200 Press any key to continue The outer pair of () is needed

More tricks of using assignment sum=0; cout << "After Tax: $" << (sum = sum + tax(sum=buy_one(), tax_rate)) << "\n\n"; sum=0; cout << "After Tax: $" << (sum = (sum+0) + tax(sum=buy_one(), tax_rate)) << "\n\n"; sum=0; cout << "After Tax: $" << (sum = tax(sum=buy_one(), tax_rate) + sum) << "\n\n"; sum=0; cout << "After Tax: $" << (sum = tax(sum=buy_one(), tax_rate) + (sum+0)) << "\n\n"; Input the price: $10 After Tax: $10.75 Input the price: $10 After Tax: $0.75 Input the price: $10 After Tax: $10.75 Input the price: $10 After Tax: $10.75 Press any key to continue

Escape sequence cout << "To print \\, you need two \\" << endl; cout << "\\n \n starts a new line" << endl; cout << "\\t \t horizontal tab" << endl; cout << "\\\" print \"" << endl; cout << "\\\' print \'" << endl ; To print \, you need two \ \n starts a new line \t horizontal tab \" print " \' print ' Press any key to continue In a string, the letter follows backslash \ is to control the effect of the output. E.g. \n Originally, they are designed to control the line-printer.

The Syntax of if and if/else statements if ( condition ) { statement list; } if ( condition ) { statement list; } else { statement list; } Indentation indicates that the statements in the statement list are at the level next to the if/else statement. Reserved words Logical expression. In C++, 0 is false, any non-zero values will be considered as true.

Example of if/else statement cout << "How many items do you want to buy? "; cin >> a; if (a = = 1) { sum = buy_one(); sum = tax(sum,tax_rate)+sum; cout << "You need to pay $" << sum; } else { sum = buy_two(); sum = tax(sum,tax_rate)+sum; cout << "You need to pay $" << sum; } cout << endl; How many items do you want to buy? 1 Input the price: $20 You need to pay $21.5 Press any key to continue How many items do you want to buy? 2 Input two prices: $ You have input 100 and 20 After Tax, there are $107.5 and $21.5 You need to pay $129 Press any key to continue How many items do you want to buy? 3 Input two prices: $ You have input 100 and 20 After Tax, there are $107.5 and $21.5 You need to pay $129 Press any key to continue

Nested if/else statement if ( condition 1 ) { if ( condition 2 ) { statement list; } else { statement list; }; statement list; } else { statement list; } Indentation indicates the level of statements.

Example of nested if/else statement cout << "How many items do you want to buy? "; cin >> a; if (a = = 1) { sum = buy_one(); sum = tax(sum,tax_rate)+sum; cout << "You need to pay $" << sum; } else { if (a = = 2) { sum = buy_two(); sum = tax(sum,tax_rate)+sum; cout << "You need to pay $" << sum; } else { cout << "At most two items!!"; } How many items do you want to buy? 4 At most two items!! Press any key to continue

Another form of Nested if/else statement (Cascaded) if (condition_1) statement_1; else if (condition_2) statement_2; else if (condition_3) statement_3; else if (condition_4) statement_4; else if (condition_5) statement_5; else if (condition_6) statement_6; if (condition_1) if (condition_2) if (condition_3) if (condition_4) if (condition_5) if (condition_6) statement_1; else statement_2; if (condition_1 && condition_2 && condition_3 && condition_4 && condition_5 && condition_6 && ) statement_1; else statement_2;

Example of cascaded if/else statement cout << "Input an integer as the day of the week:"; cin >> i; if (i = = 1) cout << "\n Sunday"; else if (i = = 2) cout << "\n Monday"; else if (i = = 3) cout << "\n Tuesday"; else if (i = = 4) cout << "\n Wednesday"; else if (i = = 5) cout << "\n Thursday"; else if (i = = 6) cout << "\n Friday"; else if (i = = 7) cout << "\n Saturday"; Input an integer as the day of the week:3 Tuesday Input an integer as the day of the week:4 Wednesday Input an integer as the day of the week:7 Saturday Input an integer as the day of the week:4 Wednesday

switch vs. cascaded if/else if (i == 1) statement_1; else if (i == 2) statement_2; else if (i == 3) statement_3; else if (i == 4) statement_4; else if (i == 5) statement_5; else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; break; case 3: statement_3; break; case 4: statement_4; break; case 5: statement_5; break; case 6: statement_6; break; }

Example of switch cout << "Input an integer as the day of the week:"; cin >> i; switch (i) { case 1 :cout << "\n Sunday"; break; case 2 : cout << "\n Monday"; break; case 3 : cout << "\n Tuesday"; break; case 4 : cout << "\n Wednesday"; break; case 5 : cout << "\n Thursday"; break; case 6 : cout << "\n Friday"; break; case 7 : cout << "\n Saturday"; break; }

Function that returns a string string WeekDay(int day) { if (0 == day) { return "Sunday"; } else if (1 == day) { return "Monday"; } else if (2 == day) { return "Tuesday"; } else if (3 == day) { return "Wednesday"; } … }