C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA...

26
C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Transcript of C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA...

Page 1: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

C++ DATA TYPESBASIC CONTROL FLOWProblem Solving with Computers-I Chapter 1 and Chapter 2

Page 2: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Review: Program compilation What does it mean to “compile” a C++ program? A. Write the implementation of the program in a .cpp fileB. Convert the program into a form understandable by the processorC. Execute the program to get an outputD. None of the above

Page 3: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Kinds of errorsWhich of the following types of errors is produced if our program divides a number by 0? A. Compile-time errorB. Run-time errorC. Both A and BD. Neither A nor B

Lawton Nichols
Page 4: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Let’s play Fizzbuzz

4

We’ll play fizzbuzz and then code it up!

In the process we will learn about different ways of getting input into C++ programs: Standard input cin • Arguments to main• Reading from files (a later lecture)We will also learn about diffferent ways of showing output to C++ programs:

Page 5: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Let’s code Fizzbuzz -1.0$ Enter a number: 1 1 $ Enter a number: 2 2 $ Enter a number: 3 fizz $ Enter a number: 4 4

5

$Enter a number: 5 5 $Enter a number: 6 fizz $Enter a number: 7 7 $Enter a number: 15 fizz

Page 6: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Review: C++ Variables and Datatypes

• Variables are containers to store data • C++ variables must be “declared” before they are used by specifying a datatype •int: Integers •double: floating point numbers •char: characters

Page 7: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

C++ Uninitialized Variables• Value of uninitialized variables is “undefined” • Undefined means “anything goes” • Can be a source of tricky bugs

• What is the output of the code below?

int main() { int a, b; cout<<"The sum of "<< a << " and " << b<< " is:"<< a+b<<endl; }

Page 8: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Variable Assignment

• The values of variables can be initialized...

int myVariable = 0;-or-

int myVariable;myVariable = 0;

• ...or changed on the fly...

int myVariable = 0;myVariable = 5 + 2;

Page 9: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Variable Assignment

• ...or even be used to update the same variable!

int myVariable = 0;myVariable = 5 + 2;myVariable = 10 - myVariable;myVariable = myVariable==0;

Page 10: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Control flow: if statement

if ( Boolean expression) { // statement 1; // statement 2;}

• The condition is a Boolean expression• These can use relational operators

• In C++ 0 evaluates to false• Everything else evaluates to true

Page 11: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Examples of if statements

• The condition is a Boolean expression • These can use relational operators

if ( 1 < 2 ) { cout<< “foo” ;}

if ( 2 == 3) { cout<<“foo” ;}

Use the curly braces even if you have a single statement in your if

Page 12: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Fill in the ‘if’ condition to detect numbers divisible by 3

if ( ________ ) cout<< x << “is divisible by 3 \n” ;}

A. x/3 == 0 B. !(x%3) C. x%3 == 0 D. Either B or C E. None of the above

Lawton Nichols
Page 13: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

if (x > 0){ pet = dog; count++; } else { pet = cat; count++; }

13

Control Flow: if-else

• Can you write this code in a more compact way?

Page 14: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

if (x > 100){ pet = dog; count++; } else if (x > 90){ pet = cat; count++; } else { pet = owl; }

14

Control Flow: Multiway if-else

• Can you write this code in a more compact way?

Page 15: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Let’s code Fizzbuzz -2.0 (taking arguments from main)$ ./fizzbuzz 1 1

$ ./fizzbuzz 9 Fizz

$ ./fizzbuzz 15 Fizzbuzz

15

Page 16: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

What is git?

Git is a version control system (VCS). A VCS allows you to keep track of changes in a file (or groups of files) over time

Git allows you to store code on different computers and keep all these different copies in sync

Page 17: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Why are we learning git in this class?

• Collaborate • Share code ownership • Work on larger projects • Provide feedback on work in progress • Learn professional software

development tools

Page 18: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Git Concepts

repo (short for repository): a place where all your code and its history is stored

Page 19: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Creating a repo on the cloud (www.github.com)Navigate to www.github.com and create a repo on the internet

Remote repo

Make sure the repo is inside our class organization! (Otherwise you won't be able to make a private one)

Page 20: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Cloning a repo

Page 21: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Different “states” of a file in a local repo

Staging areaWorkspace

Any file that is modified (in an editor) is saved in the workspace

To inspect the state of a file use: git status

Saved in local repo

Remote repo

Page 22: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Saving a file (in the local repo)

git add <filename> git add .

git commit -m “message”

Remote repo

Staging areaWorkspace Saved in local repo

Page 23: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Syncing repos: pushing local updates to remote

Remote repoLocal repo

git push

Page 24: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Syncing repos: pulling the latest changes from remote

Remote repoLocal repo

git pull

Page 25: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Numbers

Page 26: C++ DATA TYPES BASIC CONTROL FLOW › m19-nichols › lectures › CS16_Lecture2.pdf · C++ DATA TYPES BASIC CONTROL FLOW Problem Solving with Computers-I Chapter 1 and Chapter 2

Comparison Operators