What exactly are loops ?
In computer programming, a loop is a sequence of instructions that are continually repeated until a certain condition is reached.
2 types of loops :
- Entry controlled loops → Condition is checked before executing the body of a loop
- Exit controlled loops → Condition is checked after executing the body of a loop.
There are basically three types of loop constructs most of the programming language :
- while loop → Entry controlled loop
- for loop → Entry controlled loop
- do-while loop → Exit controlled loop
Things to keep in mind while declaring the definition of the loops :
The conditions present in the loop must be well-defined otherwise it will lead to an infinite loop that is the program will never terminate. So we must specify termination condition carefully.
While loop:
while(condition){
//Statements
}
- It is an entry controlled loop hence the condition is evaluated processing a body of the loop. If a condition is true then and only then the body of a loop is executed.
- After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false.
- Once the condition becomes false, the control goes out of the loop that is to the statements which are present immediately after the loop.
- If there is single statement present in the loop then the { } curly braces are not required but it is a good practice though to use the curly braces even we have a single statement in the body.
Do-while Loop:
Syntax for do-while loop
- It is an exit-controlled loop that is condition is always executed after the body of a loop.
- As we saw in a while loop, the body is executed if and only if the condition is true. In some cases, we have to execute a body of the loop at least once even if the condition is false. This type of operation can be achieved by using a do-while loop.
- Similar to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed.
- The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;)
For Loop:
Syntax of for loop →
- The initial value of the for loop is performed only once.
- The condition is a Boolean expression that tests and compares the counter to a fixed value after each iteration, stopping the for loop when false is returned.
- The incrementation/decrementation increases (or decreases) the counter by a set value.
Illustration of for loop →
Extra points related to for loops:
In C or Java, the for loop can have multiple expressions separated by commas in each part.
Also, we can skip the initial value expression, condition and/or increment by adding a semicolon.
Which Loop to select?
Selection of a loop is always a tough task for a programmer, to select a loop do the following steps:
- Analyse the problem and check whether it requires a pre-test or a post-test loop.
- If pre-test is required, use a while or for a loop.
- If post-test is required, use a do-while loop.
Continue statement:
When you want to skip to the next iteration but remain in the loop, you should use the continue statement.
The continue
statement can be used with the if...else
statement.
We can see it in the example given below :
In this program, when the user enters a positive number, the sum is calculated using
sum += number;
statement.
When the user enters a negative number, the continue
statement is executed and it skips the negative number from the calculation.
For queries regarding the content, please drop a mail to loop@cumminscollege.in Links for in-depth studying and practice problems are sent via mail to students of the college on the date of publishing of the blog by Loop Club. The content is a part of DSA Bulletins started by Team Loop, to provide thorough knowledge and facilitate the learning of new topics of DSA. We are a group of students who are passionate about CP and DSA and want to create awareness and help students in the above topics.