C++
Indentation has no effect on the program but make it more organize and reader friendly. The braces or curly brackets makes the statement blocks for the tasks.
if (10 < 20)
cout<<"10 is less than 20";
else
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
This is second line
Python
Indentation makes the statement blocks. No need to use the braces.
if 10 < 20:
cout<<"10 is less than 20";
else:
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
The output will be different if the indentation of the above python code is changed as follows.
if 10 < 20:
cout<<"10 is less than 20";
else:
cout<<"10 not greater than 20";
cout<<"This is second line";
Output
10 is less than 20
This is second line
Thursday, November 30, 2017
Comments in C++ and Python
Comments are put in a program to make it more readable and easy to understand when someone reads it.
C++
There are two commenting styles
1. Line comment
Put two slashes (//) and whatever is been typed will become a comment and will be ignored by the compiler.
E.g.
int total = 0; // integer variable to store the total value
2. Block comment
This consist two parts: Starting with slash and star (/*) and ending with the reverse order , which is star and a slash (*/). Everything typed between these two symbols become comments and ignored by the compiler. Multiple comment lines can be made with the block comment method.
E.g.
/*-----------------------------
Author : Randima
Date : 2017 January 01
-----------------------------*/
Python
A line starts with hash symbol (#) is a comment. Unlike block commenting in the C++, hash symbol (#) should be used in every comment line.
E.g.
# Author : Randima
# Date : 2017 January 01
C++
There are two commenting styles
1. Line comment
Put two slashes (//) and whatever is been typed will become a comment and will be ignored by the compiler.
E.g.
int total = 0; // integer variable to store the total value
2. Block comment
This consist two parts: Starting with slash and star (/*) and ending with the reverse order , which is star and a slash (*/). Everything typed between these two symbols become comments and ignored by the compiler. Multiple comment lines can be made with the block comment method.
E.g.
/*-----------------------------
Author : Randima
Date : 2017 January 01
-----------------------------*/
Python
A line starts with hash symbol (#) is a comment. Unlike block commenting in the C++, hash symbol (#) should be used in every comment line.
E.g.
# Author : Randima
# Date : 2017 January 01
First C++ and Python program
First program
Display Hello World in the terminal
C++
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World"<<endl;
return 0;
}
Python
print("Hello World")
Display Hello World in the terminal
C++
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World"<<endl;
return 0;
}
Python
print("Hello World")
Subscribe to:
Posts (Atom)
Iterative statements in C++ and Python (Part 2)
for loop iterative statements Executing set of statements using for iterative statement in C++ and Python is quite different. First, l...
-
First program Display Hello World in the terminal C++ #include <iostream> using namespace std; int main() { cout...
-
while iterative statement Set of statements can be executed repeatedly until a condition is met. C++ Python Synta...
-
if conditional statement A condition is checked and execute statement/block of statements if the condition is true. C++ ...