Thursday, November 30, 2017

Indentation in C++ and Python

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

No comments:

Post a Comment

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...