1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.

Slides:



Advertisements
Similar presentations
1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc.
Advertisements

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)
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Control Constructs Mechanisms for deciding when and how often an action should be taken.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 6 Control Structures.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
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 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Basic Elements of C++ Chapter 2.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Modifying objects Operators and Expressions.
Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc.
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Basic Of Computer Science
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Boolean values Gateway to decision making. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements.
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,
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decisions. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements for every run of the program.
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)
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Decisions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Decision Structures Michele Co CS 101-E. 2 UVa CS101E Spring 2007 Today if statements Logical expressions –Truth tables –Boolean algebra –DeMorgan’s laws.
The Ohio State University
Chapter 3 Selection Statements
Chapter Topics The Basics of a C++ Program Data Types
Chapter 4 : Control Construct.
Selection (also known as Branching) Jumail Bin Taliba by
Basic Elements of C++.
A mechanism for deciding whether an action should be taken
Basic Elements of C++ Chapter 2.
COMS 261 Computer Science I
Expression Review what is the result and type of these expressions?
Chapter 4: Control Structures I (Selection)
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
COMS 261 Computer Science I
Life is Full of Alternatives
Life is Full of Alternatives
COMS 261 Computer Science I
Modifying Objects Assignment operation Assignment conversions
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11

2 Announcements Read 4.1 – 4.6 Homework

3 Review Standard data types –Strings (not part of C++, but are common) –Real (float) Names Precedence

4 Outline Assignment Statement const Non-fundamental Types –string

Nonfundamental Types Nonfundamental as they are additions to the language C++ permits definition of new types and classes –A class is a special kind of type Class objects typically have –Data members that represent attributes and values –Member functions for object inspection and manipulation –Members are accessed using the selection operator j = s.size(); selection operator

Nonfundamental Types Libraries often provide special-purpose types and classes Programmers can also define their own types and classes Examples –Standard Template Library (STL) provides class string –Class string Used to represent a sequence of characters as a single object

7 Class string Some definitions string name = "Joanne"; string decimalPoint = "."; string empty = ""; string copy = name; string question = '?'; // illegal

8 Nonfundamental Types To access a library use a preprocessor directive to add its definitions to your program file #include

9 Nonfundamental Types The using statement makes syntax less clumsy –Without it std::string s = "Sharp"; std::string t = "Spiffy "; –With it using namespace std; //std contains string string s = "Sharp"; string t = "Spiffy";

10 Class string Some string member functions –size() determines number of characters in the string string Saying = "Rambling with Gambling"; cout << Saying.size() << endl; // 22 –substr() determines a substring (Note first position has index 0) string Word = Saying.substr(9, 4); // with

11 Class string –find() computes the position of a subsequence int j = Saying.find("it"); // 10 int k = Saying.find("its"); // ?

12 Class string Auxiliary functions and operators –getline() extracts the next input line string Response; cout << "Enter text: "; getline(cin, Response, '\n'); cout << "Response is \"" << Response << "\"” << endl; – Example run Enter text: Want what you do Response is "Want what you do"

13 Class string Auxiliary operators –+ string concatenation string part1 = "Me"; string part2 = " and "; string part3 = "You"; string all = part1 + part2 + part3; –+= compound concatenation assignment string thePlace = "Brooklyn"; thePlace += ", NY";

14 #include using namespace std; int main() { cout << "Enter the date in American format: " << "(e.g., January 1, 2001) : "; string Date; getline(cin, Date, '\n'); int i = Date.find(" "); string Month = Date.substr(0, i); int k = Date.find(","); string Day = Date.substr(i + 1, k - i - 1); string Year = Date.substr(k + 2, Date.size() - 1); string NewDate = Day + " " + Month + " " + Year; cout << "Original date: " << Date << endl; cout << "Converted date: " << NewDate << endl; return 0; }

15 Boolean Algebra Logical expressions have the one of two values - true or false –A rectangle has three sides –The instructor has a pleasant smile The branch of mathematics is called Boolean algebra –Developed by the British mathematician George Boole in the 19th century Three key logical operators –And, Or, Not (unary operator)

16 Boolean Algebra Truth tables –Lists all combinations of operand values and the result of the operation for each combination Example PQ P and Q False FalseFalse False TrueFalse True FalseFalse True TrueTrue

17 Boolean Algebra Or truth table Not truth table (unary operator) PQ P or Q False FalseFalse False TrueTrue True FalseTrue True TrueTrue Pnot P False True True False

18 Boolean Algebra Can create complex logical expressions by combining simple logical expressions –not (P and Q) A truth table can be used to determine when a logical expression is true PQ P and Qnot (P and Q) False FalseFalseTrue False TrueFalseTrue True FalseFalseTrue True TrueTrueFalse

19 A Boolean Type C++ contains a type named bool Type bool has two symbolic constants –true, false Boolean operators –The and operator is && –The or operator is || –The not operator is ! Warning –& and | are also operators so be careful what you type

20 A Boolean Type Example logical expressions bool p = true; bool q = false; bool r = true; bool s = (P && Q); bool t = ((!Q) || R); bool u = !(R && (!Q));

21 Relational Operators Equality operators – == – != Examples –int i = 32; –int k = 45; –bool q = (i == k); –bool r = (i != k);

22 Relational Operators Ordering operators – < – > – >= – <= Examples int i = 5; int k = 12; bool p = (i < 10); bool q = (k > i); bool r = (i >= k); bool s = (k <= 12);

23 Operator Precedence Revisited Precedence of operators (from highest to lowest) Parentheses Unary operators Multiplicative operators Additive operators Relational ordering Relational equality Logical and Logical or Assignment

24 Operator Precedence Revisited Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 Yuck! Do not write expressions like this!

25 Operator Precedence Revisited Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 Is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || (!false) == (5 < 24))

26 Conditional Constructs Provide –Ability to control whether a statement list is executed Two constructs –If statement if if-else if-else-if –Switch statement Left for reading

27 The Basic If Statement Syntax if (Expression) Action If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Expression Action truefalse

28 Example if (Value < 0) { Value = -Value; }

29 Sorting Two Numbers cout << "Enter two integers: "; int value1; int value2; cin >> value1 >> value2; if (value1 > value2) { int rememberValue1 = value1; value1 = value2; value2 = rememberValue1; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;

30 Semantics

31 What is the Output? int m = 5; int n = 10; if (m < n) ++m; ++n; cout << " m = " << m << " n = " n << endl;

32 The If-Else Statement Syntax if (Expression) Action 1 else Action 2 If Expression is true then execute Action 1 otherwise execute Action 2 if (v == 0) { cout << "v is 0"; } else { cout << "v is not 0"; } Expression Action 1 Action 2 true false

33 Finding the Max cout << "Enter two integers: "; int value1; int value2; cin >> value1 >> value2; int max; if (value1 < value2) { max = value2; } else { max = value1; } cout << "Maximum of inputs is: " << max << endl;

34 Finding the Max

35 Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice –if-else-if statement if-else statements “glued” together –Switch statement An advanced construct

36 An If-Else-If Statement if (nbr < 0){ cout << nbr << " is negative" << endl; } else if (nbr > 0) { cout << nbr << " is positive" << endl; } else { cout << nbr << " is zero" << endl; }

37 A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

38 cout << "Enter simple expression: "; int left; int right; char operator; cin >> left >> operator >> right; cout << left << " " << operator << " " << right << " = "; switch (Operator) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; } Example