Lecture7
Lecture7
Programming
Steve James
Lecture 7
Python to C++
RECAP
Summary so far
• C++ is compiled
– Python is interpreted
• C++ variables have fixed types (static)
– Python variables can change types (dynamic)
• Whitespace in C++ is not important
– Indentation is important in Python
• C++ starts executing code in the main function
– Python executes the first line of code it sees
Type Examples
Python
Values
Name Keyword Kind Example Literals Equivale
Allowed
nt
Boolean bool Built-in True or true, false bool
false
Character char Built-in A single ‘a’, ‘x’, ‘9’, ‘\n’ str
character
Integer int Built-in Integers 0, -1, 2145, 0xa3 int
(also within
short/long) some range
Floating- double Built-in Real-valued 1.2, -0.6, .3, .6F, float
point (also float) numbers 1.4e10
within
some range
and to
some
precision
String string Standard Sequence “SaltBae”, str
library of “Hello there\n”
characters
C++
VARIABLES
Declaration
• For variables, need type and name
– int x;
– double y; USE AT DECLARATION
– string someMeaningfulName; TIME ONLY
– int x;
– x = 42;
– int y = 23;
– x = y;
Finally
• Must declare a variable before we use it
int y;
int x = y + 2; //y has an undefined value. What's undefined + 2?!
y = 20; //Too late! We needed it for the previous line
C++
BASIC INPUT/OUTPUT
Input/Output
• Consider code that inputs a user’s first name and then displays the user’s first
name with a message
int main(){
string firstName; //string variable declaration
cin >> firstName;
cout << "Hello, " << firstName << endl; //output message and name
return 0;
}
#include <iostream>
#include <string>
int main(){
string firstName;
string surname;
cout << "Please enter your first and surname" << endl;
cin >> firstName >> surname; //input first name and surname
string fullName = firstName + " " + surname; //concatenate strings
cout << "Hello, " << fullName << endl; //output message and name
return 0;
}
Integer Input
• Consider code that asks the user to input their first name and year of birth and
then displays the user’s first name and age
#include <iostream>
#include <string>
int main(){
string firstName;
cout << "Please enter your first name" << endl;
cin >> firstName; //input first name
int yearOfBirth;
cout << "Please enter your year of birth" << endl;
cin >> yearOfBirth;
int age = 2018 - yearOfBirth;
cout << "Hello, " << firstName << endl; //output message and name
cout << "Your age is " << age << endl; //output message and age
return 0;
}
Integers and Strings
Integers Strings
• cin >> reads a number • cin >> reads a string
• cout << write • cout << write
• + adds • + concatenates
• - subtracts • - is an error
• … • …
The type of a variable determines which operations are valid and what their meanings are
for that type (“overloading” or “operator overloading”).
Aside: namespace
• A way to differentiate names of
variables/functions
• All functions provided by C++ lives in the standard
namespace (std)
– e.g. sqrt
OPERATORS
Built-in Operators
Add/subtract
{
} Arithmetic
Input/output
{
} x += n is
shorthand
for
x=x+n
} Equalities/in-
equalities
Built-in Operators
bool x = true; //x assigned to true
string s = "Hello"; //s assigned to Hello
s = s + " world"; //s assigned to Hello world (concat)
int x = 5;
x++; //x now equals 6
int y = 7;
bool isEqual = (x == y);
int rem = y % x; //rem is the remainder of y divided by x
int number;
cin >> number; // read an integer into a number
cout << rem; //print out remainder
double z = y; //assign z the current value of y
z *= 3; //equivalent to z = z * 3;
More Built-in Operators
AND/OR Operators
• e.g. 10 ≤ 𝑥 ≤ 20 can be written as
10 <= x && x <=20
7 || logical OR Left-to-right
8 = assignment Right-to-left
C++
IF-STATEMENTS
General Form
• Python
if <exp>:
# do something
else:
# do something else
Must have round brackets
LOOPS
While Loop General Form
• Python
while <exp>:
# statement 1
# statement 2
while (<exp>){
// statement 1
// statement 2
}
While Loops
#include <iostream>
using namespace std;
int main(){
int passes = 0;
passes = 0 int fails = 0;
failures = 0 int n_students = 0;
n_students = 0 int mark;
while n_students < 10: while (n_students < 10){
mark = int(input()) cin >> mark;
if mark >= 50: if (mark >= 50){
passes = passes + 1 passes = passes + 1;
else: }
failures = failures + 1 else{
n_students = n_students + 1 fails = fails + 1;
print("Passes", passes) }
print("Failures", failures) n_students = n_students + 1;
}
cout << "Passes " << passes << endl;
cout << "Fails " << fails << endl;
return 0;
}
For Loop General Form
• Python
for i in range(start, stop, step):
# statement 1
# statement 2
For
goto While
loop loop
Do/While Loop
• First execute the loop body, then check the
condition!
do{
while (<exp>){
//statement 1
// statement 1
//statement 2
// statement 2
}
}
while(<exp>);
Semicolon!