Introduction to C++ October 2, 2017.

Slides:



Advertisements
Similar presentations
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Advertisements

What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
Data types and variables
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.
Introduction to C++ Programming
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
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.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 2. Program Construction in Java. 01 Java basics.
LESSON 2 Basic of C++.
A Sample Program #include using namespace std; int main(void) { cout
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Programming Fundamental
Computing Fundamentals
Chapter 2: Introduction to C++
Quiz Next Monday.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
CISC 1600/1610 Computer Science I
CS150 Introduction to Computer Science 1
Wednesday 09/23/13.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Programs written in C and C++ can run on many different computers
Reading from and Writing to Files
Life is Full of Alternatives
Primitive Types and Expressions
C++ Programming Basics
Unit 3: Variables in Java
(Dreaded) Quiz 2 Next Monday.
Chapter 1 c++ structure C++ Input / Output
Variables and Constants
Reading from and Writing to Files
Presentation transcript:

Introduction to C++ October 2, 2017

Sample C++ Program #include <cstdlib> #include <iostream> using namespace std; int main() { cout << "Hello, world!\n"; return 0; }

Sample C++ Program The first two lines are known as include directives: #include <cstdlib> #include <iostream> These tell the compiler/IDE where to find information about certain items that are included in your program. The second directive tells the IDE how to interpret cout. The next line further explains the include directives: using namespace std; This says the names defined in the iostream library should be interpreted in the “standard” (std) way.

Sample C++ Program The next line, int main(), says that the main part of the program begins here: int main() { (main part of the program) } Everything between the curly braces { } is the main function. cout << "Hello, world!\n"; outputs “Hello, world!” return 0; tells the IDE “end the program when you get here.” Notice that every statement other than include directives ends with a semicolon!

Input and Output Use cout << to output text to the screen and cin >> to input something. Example: Suppose you want to ask the user to input a number. cout << "Please enter a number:\n"; cin >> …then what?

Variables A variable is a placeholder for a piece of information that can change, such as a number. A variable is declared with its type name followed by its identifier. To declare an integer with name (identifier) x: int x; Typically, variables should be initialized when declared. int x = 0; If not initialized, the variable will contain a “garbage value,” which may cause unexpected behavior in the program.

Variables In C++, = does NOT indicate equality!! It is used to assign a value to a variable. int x = 5; assigns the integer x the value of 5. x = y does not mean x and y are equal; rather, it assigns x the value of y. int x = 5; int y = 3; x = y; Now the value of x is 3. To indicate equality, use two equals signs: == (“equals equals”). On the AP exam, = does indicate equality;  is used to assign a value.

Variables When working with variables, each subsequent line of code gives the variable a new value. int x = 1; x = x + 1;  now the value of x is 2 x = x + 1;  now the value of x is 3 x = x + 3;  now the value of x is 6

Practice Question Consider the following code: What is the value of x? int x = 3; int y = 7; int z = 4; y = z; x = y; What is the value of x?

Practice Question Consider the following code: What is the value of x? int x = 3; int y = 7; int z = 4; z = y; x = z; What is the value of x?

Operations Addition, subtraction, multiplication, and division work as you would expect. x + y x – y x * y x / y There are some nuances with division that we will cover later. Parentheses also work as you would expect. (x + y) * z

Practice Question Consider the following code: What is the value of x? int x = 3; int y = 7; int z = 4; x = x + y; What is the value of x?

Modular Arithmetic x mod y tells you the remainder when x is divided by y. To find one number mod another, use the percent sign: x % y Example: int x = 3; int y = 8; y % x == 2 x % y == 3

Variable Types The most common variable types you will see are: Integer (int) Double (double), which can hold numbers with a fractional part (e.g., 1.5) String (string) Character (char) Boolean variable (bool), which is either true or false Less commonly used types include: Short integer (short or short int) Long integer (long or long int) Floating-point number (float)—like double, but with a smaller range Long double (long double)

The Don’ts of Variables Don’t put a variable in quotes! cout << x; displays the value of x. cout << "x"; displays the letter x. Text in quotes is considered a string—we’ll revisit this later… You do need to put non-variable text in quotes. Suppose the value of x is 5. cout << "x = " << x; displays x = 5. cout << x = << x; will give you an error.

Input and Output Returning to the original example: suppose you want to ask the user to input a number. Use a variable to store the number! int x = 0; cout << "Please enter a number:\n"; cin >> x;

Input and Output What if you want the user to enter, say, two numbers? Use cin >> first number >> second number! int x = 0, y = 0; cout << "Please enter two numbers with a space between them:\n"; cin >> x >> y; cout << "The sum of the two numbers is: " << x + y << endl;