Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';

Slides:



Advertisements
Similar presentations
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Chapter 10.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Chapter 2: Introduction to C++.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
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.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Input & Output: Console
Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
2440: 211 Interactive Web Programming Expressions & Operators.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CSC 107 – Programming For Science. Announcements.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
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.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Bill Tucker Austin Community College COSC 1315
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Introduction to C++ October 2, 2017.
Chapter 2: Introduction to C++
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Wednesday 09/23/13.
COMS 261 Computer Science I
Chapter 2: Introduction to C++.
COMS 261 Computer Science I
Modifying Objects Assignment operation Assignment conversions
Chapter 1 c++ structure C++ Input / Output
The Fundamentals of C++
Presentation transcript:

Section 2 - More Basics

The char Data Type Data type of a single character Example char letter; letter = 'C';

A type char character is encapsulated by single quotes ‘ Characters are encoded in the computer using a scheme where an integer represents a particular character Examples ' ' encoded as 32'+' encoded as 43 'A' encoded as 65'Z' encoded as 90 'a' encoded as 97 'z' encoded as 122 This allows us to compare characters.

Character Operations Relational (aka comparison) operations are defined for characters types (as well as for other data types) The result is either true or false – 'a' < 'b' is true – '4' > '3' is true – '6' <= '2' is false

Example - Read Characters To read a character from the keyboard, use char ch; cout << "Enter a character: "; cin >> ch;

Character Constants Explicit (literal) characters within single quotes 'a','D','*' Special characters - delineated by a backslash \ Two character sequences (escape codes) Some important special escape codes \t denotes a tab \n denotes a new line \\ denotes a backslash \' denotes a single quote \" denotes a double quote To use, wrap up within single quotes '\t' for the tab '\n' for the new line

Character Strings (aka Text Strings) Can store a series of characters (aka string) in consecutive memory locations: "Hello“ Stored with the null terminator, \0, at the end Comprises the characters between the " " Hello\0

Literal String Constants A literal string constant is a sequence of zero or more characters enclosed in double quotes – “Walk, don’t run" – “Bulgaria" – "" String is not a fundamental type - It is a C++ defined class

To access a library use a preprocessor directive to add its definitions to your program file #include string s = "Sharp"; string t = “Dull";

Class string Used to represent a sequence of characters as a single text string Some definitions string Name = “Ivan"; string DecimalPoint = "."; string empty = ""; string copy = name; string Question = '?'; // illegal

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 – find() computes the position of a subsequence int j = Saying.find("it"); // 10 int k = Saying.find("its"); // ?

Class string Auxiliary operators – + string concatenation – join strings together string Part1 = "Me"; string Part2 = " and "; string Part3 = "You"; string All = Part1 + Part2 + Part3; – += compound concatenation assignment string ThePlace = "Blagoevgrad"; ThePlace += ", 2700";

Example string fname = “Ivan"; string lname = “Ivanov"; string name = fname + lname; cout << name << endl; name = fname + " " + lname; cout << name << endl; The output will be IvanIvanov

Represents values that are true or false false is represented by 0, and true by 1 bool allDone = true; bool finished = false; What happens here? bool b = true; int i = b; cout << b << endl; cout << i << endl; The bool Data Type

Logical Expressions Logical expressions have the one of two values - true or false - A rectangle has three sides - The instructor has a pleasant smile Three key logical operations And operations Or operation Not operation - negate

Type bool has two symbolic constants true false Logical (aka Boolean) operators The And operator is && The Or operator is || The Not operator is !

Example logical expressions bool P = true; bool Q = false; bool R = true; bool S = (P && Q); bool T = ((!Q) || R); bool U = !(R && (!Q));

Write a program that lets the user enter a year and checks whether it is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. So you can use the following Boolean expression to check whether a year is a leap year: (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

#include using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; if (number % 2 == 0 && number % 3 == 0) cout << number << " is divisible by 2 and 3." << endl; if (number % 2 == 0 || number % 3 == 0) cout << number << " is divisible by 2 or 3." << endl; if ((number % 2 == 0 || number % 3 == 0) && !(number % 2 == 0 && number % 3 == 0)) cout << number << " divisible by 2 or 3, but not both." << endl; return(0); }

How would you write this expression in C++ 1 <= numberOfDaysInAMonth <= 31 Need to create a compound expression using Boolean operators

Relational Operators Equality operators == test for equality !=test for inequality Examples int i = 32; int k = 45; bool q = (i == k); // false bool r = (i != k); // true

Comments Allow text commentary to be included in program Importance - Programs are read far more often than they are written - Programs need to be understood so that they can be maintained C++ has two conventions for comments // single line comment (preferred) /* long comment */ (save for debugging) Typical use Describing the working of parts of a program

Enumerated Types Allows the definition of programmer-defined types enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}; Once a type is defined, you can declare a variable of that type: Day day; The variable day can hold one of the values defined in the enumerated type. For example, the following statement assigns enumerated value MONDAY to variable day: day = MONDAY;

Enumerated Types As with any other type, you can declare and initialize a variable in one statement: Day day = MONDAY; Furthermore, C++ allows you to declare an enumerated type and variable in one statement. For example, enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day = MONDAY;