COMS 261 Computer Science I

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

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.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
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.
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';
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
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.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
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.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
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.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
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,
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
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 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Bill Tucker Austin Community College COSC 1315
The Ohio State University
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Selection Statements
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Control Statements
Chapter 7: Expressions and Assignment Statements
Chapter 1.2 Introduction to C++ Programming
Chapter 4 : Control Construct.
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
Selection (also known as Branching) Jumail Bin Taliba by
Computing Fundamentals
Basic Elements of C++.
A mechanism for deciding whether an action should be taken
Chapter 7: Expressions and Assignment Statements
Basic Elements of C++ Chapter 2.
COMS 261 Computer Science I
Expression Review what is the result and type of these expressions?
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
CS150 Introduction to Computer Science 1
COMS 261 Computer Science I
Chapter 2: Introduction to C++.
Summary of what we learned yesterday
Life is Full of Alternatives
COMS 261 Computer Science I
C++ Programming Basics
Modifying Objects Assignment operation Assignment conversions
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

COMS 261 Computer Science I Title: C++ Fundamentals Date: September 13, 2004 Lecture Number: 10

Announcements Read 4.1 – 4.6 Homework

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

Outline Assignment Statement const Non-fundamental Types string

Memory Depiction 1001 1002 1003 1004 1005 1006 1007 1008 1009 100a 100b . ffff

Memory Depiction float y = 12.5; float occupies 4 bytes int temperature = 32; char letter = 'c'; 1001 1002 y 1003 12.5 1004 1005 1006 1007 Temperature 32 1008 1009 Letter c 100a 100b . ffff

Assignment Statement Basic form Action object = expression ; celsius = (fahrenheit - 32) * 5 / 9; y = m * x + b; Action Expression is evaluated Expression value stored in object

Definition int NewStudents = 6;

Definition int newStudents = 6; int oldStudents = 21;

Definition int newStudents = 6; int oldStudents = 21; int totalStudents;

Assignment Statement int newStudents = 6; int oldStudents = 21; int totalStudents; totalStudents = newStudents + oldStudents;

Assignment Statement int newStudents = 6; int oldStudents = 21; int totalStudents; totalStudents = newStudents + oldStudents;

Assignment Statement int newStudents = 6; int oldStudents = 21; int totalStudents; totalStudents = newStudents + oldStudents; oldStudents = totalStudents;

Assignment Statement int newStudents = 6; int oldStudents = 21; int totalStudents; totalStudents = newStudents + oldStudents; oldStudents = totalStudents;

Consider int value1 = 10;

Consider int value1 = 10; int value2 = 20;

Consider int value1 = 10; int value2 = 20; int hold = value1;

Consider int value1 = 10; int value2 = 20; int hold = value1; value1 = value2;

Consider int value1 = 10; int value2 = 20; int hold = value1; value1 = value2;

Consider int value1 = 10; int value2 = 20; int hold = value1; value1 = value2; value2 = hold;

Consider int value1 = 10; int value2 = 20; int hold = value1; value1 = value2; value2 = hold; We swapped the values of objects Value1 and Value2 using Hold as temporary holder for Value1’s starting value!

Incrementing int i = 1; i = i + 1; Assign the value of expression i + 1 to i Evaluates to 2

Const Definitions Modifier const indicates that an object cannot be changed Object is read-only Useful when defining objects representing physical and mathematical constants const float Pi = 3.1415;

Const Definitions Value has a name that can be used throughout the program const int SampleSize = 100; Makes changing the constant easy Only need to change the definition and recompile

Assignment Conversions Floating-point expression assigned to an integer object is truncated Integer expression assigned to a floating-point object is converted to a floating-point value

Assignment Conversions float y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

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

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

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

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

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

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

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"

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

#include <iostream> 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; }

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

Boolean Algebra Truth tables Example Lists all combinations of operand values and the result of the operation for each combination Example P Q P and Q False False False False True False True False False True True True

Boolean Algebra Or truth table P Q P or Q False False False False True True True False True True True True

Boolean Algebra Not truth table P not P False True True False

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 P Q P and Q not (P and Q) False False False True False True False True True False False True True True True False

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

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

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

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

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

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

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

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

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 true false

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

Sorting Two Numbers cout << "Enter two integers: "; int Value1; cin >> Value1 >> Value2; if (Value1 > Value2) { int RememberValue1 = Value1; Value1 = Value2; Value2 = RememberValue1; } cout << "The input in sorted order: " << Value1 << " " << Value2 << endl;

Semantics

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

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

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

Finding the Max

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

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;

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

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