c++_python
c++_python
Syntax Comparison between Python and C++: Basics and Program Flow Control
The purpose of this set of notes is to help you quickly transfer your basic knowledge of Python to that of C++. Please
note that it is not a complete summary of our lecture notes. For all the C++ features discussed in COMP2011, you have
to carefully study the lecture notes on our course website.
In Python In C++
""" /*
File: hello_world.py * File: hello_world.cpp
A common program used to demo a new language * A common program used to demo a new language
""" */
print("Hello World!") #include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Basic Output
To print the word “abc” with a newline character: To print the word “abc” with a newline character:
print("abc") cout << "abc" << endl;
Comments
• for one or more lines of comments: • for one or more lines of comments:
""" ... """ /* ... */
• for one line of comment only: • for one line of comment only:
# ... // ...
Including a module/library
import random #include <iostream>
1
Statements
• A statement is a line of code. • Each statement ends in a semicolon “;”
• Only extra blanks and tabs are ignored. • Extra blanks, tabs, lines are ignored.
• If the line of the statement is too long, one may break • More than one statement can be on one line.
it into several lines using “\”.
• A statement may be spread over several lines.
For example:
For example:
print("Hello", \
" world") cout << "Hello" <<
print("!") " world" << endl; cout << "!" << endl;
Variables
• Basic Data Types: • Basic Data Types:
– Integer: – Integer: short, int, long, long long, etc.
Examples of values: 0, 1, 100, -101, ... Examples of values: 0, 1, 100, -101, ...
– Floating point: – Floating point: float, double, long double, etc.
Examples of values: 0.5, -123.908232 Examples of values: 0.5, -123.908232
– String: – Character: char
Examples of values: "A", 'abc', "comp 2011", ... Examples of values: 'A', 'a', 'B', 'b', ...
– Boolean: – Boolean: bool
Examples of values: True, False Examples of values: true, false
• Variables need not be declared and their data types are • Variables have to be declared and defined.
inferred from the assignments. For examples:
For examples:
int num1;
num1 = 100 # integer data type num1 = 100;
num2 = 0.05 # float data type double num2 = 0.05;
if Statement
2
Note: Blocks are identified by having the same inden- Note: Blocks are identified by pairs of braces ({}).
tation. For example:
For example:
int x = -5;
x = -5 if (x > 0)
if x > 0 : {
print("x is positive", end="") cout << "x is positive";
if x % 2 : if (x % 2)
print(" and odd.") cout << " and odd." << endl;
else : else
print(" and even.") cout << " and even." << endl;
elif (x < 0) and (x % 2) : } else if ((x < 0) && (x % 2)) {
print("x is negative and odd.") cout << "x is negative and odd." << endl;
elif (x < 0) and (not (x % 2)) : } else if ((x < 0) && !(x % 2)) {
print("x is negative and even.") cout << "x is negative and even." << endl;
else : } else {
print("x is zero.") cout << "x is zero." << endl;
}
if-else Operator
In C++, there are if-else expressions. The syntax is:
<condition> ? <result1> : <result2>
It means that if <condition> is true, the expression’s value
will be <result1>, otherwise it will be <result2>.
For example:
int x = 2, y = 3;
int z = (x > y) ? x : y;
cout << z << endl;
// the output will be 3
while Loop
while (<bool-expr>) : while (<bool-expr>)
<stmt(s)> <stmt>
Note: Blocks are identified by having the same indentation. while (<bool-expr>)
{
<stmt(s)>
}
do (<bool-expr>)
<stmt>
do
{
<stmt(s)>
} while (<bool-expr>);
3
for Loop
for <item> in <a list of item> : for (<for-initialization>; <bool-exp>;
<stmt(s)> <post-processing>) { <stmt(s)> }
Functions
A Python function need not specify the parameter types and A C++ function has to specify the parameter types and re-
return types. turn types.
For example, For example,
int main()
{
PrintNum(10);
PrintNum(AddOne(10));
return 0;
}
4
Some Operators in Python and C++
Python C++
Symbol Example Output Symbol Example Output
Addition + 1 + 2 3 Same
Subtraction - 1 - 2 -1 Same
Arithmetic Multiplication * 1 * 2 2 Same
Operators Division / 1 / 2 0.5 / 1.0 / 2 0.5
Integer Division // 1 // 2 0 / 1 / 2 0
Modulus (Remainder) % 9%4 1 Same
Power ** 2 ** 3 8 Nil
Assignment = x = y Same
Assignment Addition Assignment += x += y Same
Operators Subtraction -= x -= y Same
Assignment
Multiplication *= x *= y Same
Assignment
Division /= x /= y Same
Assignment
Relational And and True and False False && true && false false
Operators Or or True or False True || true || false true
Not not not False True ! !false true
Comparison Larger than > 20 > 10 True Same
Operators Larger than or >= 20 >= 10 True Same
equal to
Smaller than < 20 < 10 False Same
Smaller than or <= 20 <= 10 False Same
equal to
Equal to == 20 == 10 False Same
Not equal to != 20 != 10 True != 20 != 10 true
Post-increment Nil ++ x = 1; y = 2; 2 1
y = x++;
cout << x <<
Increment " " << y;
Operators Pre-increment Nil ++ x = 1; y = 2; 2 2
y = ++x;
cout << x <<
" " << y;
Post-decrement Nil -- x = 1; y = 2; 0 1
y = x--;
cout << x <<
Decrement " " << y;
Operators Pre-decrement Nil -- x = 1; y = 2; 0 0
y = --x;
cout << x <<
" " << y;
References: