General Concepts of Programming Methodology

Indentation

Concepts of Programming Methodology

Programming is basically solving a particular problem by giving coded instructions to the computer. Furthermore, the whole scenario of the programming cycle involves writing, testing, troubleshooting, debugging, and maintaining a computer program. Moreover, a good program should have clarity and simplicity of expressions, should make use of proper names for identifiers, contain comments, and have a proper indentation.

Programming Process Cycle

The steps of programming are as follows:

Writing

This step involves writing the desired output of the code. besides, drawing a flowchart depicting all the steps from beginning to end. Then the programmer writes the first copy of the code on the programming software.

Testing

This step is about testing the code against the test cases. If the output is according to desired test cases, the code is correct.

Troubleshooting

After testing the program we perform the error correction in this step.

Debugging

It is the process of finding the exact location of the error in the code.

Running

After all these steps the program is run to get the desired output.

What is an efficient program?

A program that gives the correct desired output for every input, including the wrong input is efficient. Moreover, it should produce correct results in less time and use less memory space. Besides, an efficient program should have clarity and simplicity of expressions, should make use of proper names for identifiers, contain comments, and have a proper indentation. Here, we will study the indentation in detail.

Browse more Topics Under General Concepts of Programming Methodology

Indentation

Indentation is basically the leading white space character added at the beginning of statements to indicate a block of code. You can denote the single block by giving the same amount of indentation for all the statements of that block. For example:

                    

if (a>b):

a=a+b   #block 1

b=b+c   #block 1

c=c*9    #block 1

else:

a=a+1   #block 2

Some languages use braces t indicate the blocks of code. Therefore, in such languages indentation gives a structure to the code.

Certain features of indentation are as follows:

  • It improves the readability of the code.
  • We should apply indentation to loops and conditional statements.
  • Indented code is good looking and easy to understand.
  • It is compulsory to indent the code in some programming languages like python.
  • All statements belonging to the same block should have the same amount of indentation.

Indentation in Python

Indenting the program code is optional in the programming languages which use braces for separating the blocks. Whereas, certain languages like python makes it compulsory to indent the code. Furthermore, it does not use any braces to divide the blocks. Let us learn how we can indent a code in python.

Example:

                    

a=1

if (a>9):

if (b>8):

a=a+b

else:

b=a+b

c=a+b

When we give spaces to separate the blocks the compiler visualizes the above code as follows:

begin: block 1

continue: block 1

begin: block 2

begin: block 3

continuing: block 2

continue: block 3

continue: block 1

Moreover, python uses 4 spaces for indentation by default. Besides, you can use the indentation as per your choice but, one space is compulsory.

Avoiding errors in python indentation

Certain ways by which you can avoid errors are as follows:

  • Do not use the tab button and space button interchangeably since this can confuse the compiler. Either use space or the tab button.
  • The number of spaces should be consistent. For example, if you use 4 spaces for block 1 then use 8 spaces only for block 2.
  • Indentation error: When you do not use indentation in python code the compiler throws an error. So, it is compulsory in python to give spaces for separating the blocks.

IndentationError: expected an indented block

This error can occur due to the following reasons:

  • Not indenting the statement in a compound statement. For example, if block, for loop, while loop, etc.
  • Not indenting the statements of a user-defined function.

Frequently Asked Questions (FAQs)

Q1. What is programming?

A1. Programming is basically solving a particular problem by giving coded instructions to the computer.

Q2. What are the steps of writing a successful program?

A2. The whole scenario of the programming cycle involves writing, testing, troubleshooting, debugging, and maintaining a computer program.

Q3. What is indentation?

A3. Indentation is basically the leading white space character added at the beginning of statements to indicate a block of code.

Q4. What is the purpose of indenting the code?

A4. The main purpose of indenting the code is to separate the blocks of code. Besides, it also improves the readability of the code.

Q5. State true or false:

It is not compulsory to indent te code in python

A5. False

Q6. Python compiler gives which error if the code is not indented?

A6. IndentationError: expected an indented block

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.