What is Loop?
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
What is While Loop?
While loop is basically a conditional statement with a block of codes. Conditional Statement allows the line of codes to be evaluated continuously till the given statement is valid.
1. Condition statement : It can be any relational expression or relation between variables. It allows the block of codes to be carried out up to a finite number of times. The statement returns True if the condition is satisfied otherwise it is false.
2. Body of the Loop : The statement herein is considered to be a single statement or a block of statements. These lines are executed repeatedly during the Loop execution.
3. True : The loop body is executed when the condition statement returns a True value.
4. Testing condition : It is used for testing the exit expression for a loop. It basically helps to terminate the loop. It returns a Boolean value either True or False. This happens after the block of code is executed.
While Syntax
While (condition)
{
Block of statements execution...
}
Flow Diagram
As per the flow chart, it can be seen that the while loop starts by initializing the situation. The conditions are given inside the parenthesis ( ). So, when the circumstances return ‘True’, it means the control block is satisfied. After satisfaction, the block of statements starts to execute. Otherwise, the whole block will be terminated. When the control statement starts to give only True value, then the loop starts to evaluate many times and becomes an infinite loop.
While loop is also called the Entry control loop. Because while loop starts by checking of control block first. So, if the given situation returns True then only the body of the loop starts to execute. Otherwise, the loop will be terminated. Hence for this reason, while loop is also called the Entry control loop.
Break statement allows us to terminate or stop the loop at the point where the break statement is.
Loop Example
// Java program.
Syntax
class Demo
{
public static void main(String args[])
{
int x = 1; // variable which control the block of statement
// Exit when x becomes greater than 5.
while (x <= 5) // loop iteration
{
System.out.print("x:" + x);
// Increment x for.
// next iteration.
x++;
}
}
}
Output:
x:1
x:2
x:3
x:4
x:5
Breakdown of the Program
- Here, the initial value of ‘x’ is 1. It creates the situation in the while loop as True. Then the execution starts to enter the body of the while loop.
- After this, the value of ‘x’ is increased as per the syntax in the body. Then the value of ‘x’ is printed.
- Further, the execution goes back in order to check the conditions in the while loop. Now the value of ‘x’ is 2.It again satisfies the condition.
- The evaluation of the body of the block takes place again and the iteration is done until the condition comes out to be True. Whereas, the execution gets terminated as soon as the False condition arises.
Example:
#Python Program to add natural numbers up to sum = 1+2+3+...+n.
# To take input from the user.
n = int(input("Enter n: "))
# initialize sum and counter.
sum = 0
i = 1
while i <= n:
sum = sum + i // variable to store sum
i = i+1 # update counter.
# print the sum.
print("The sum is", sum)
Output:
Enter n: 10.
The sum is 55.
Note*
Here we can see that in all the cases the syntax of the while does not change in any programming language. The concept remains the same in all the programming languages.
Comparing between For Loop and While Loop
Basis for Comparison | for | while |
Declaration | for(initialization; condition; iteration) { //body of 'for' } | while (condition) { block of codes; //body of while } |
Format | Initialization, condition checking, increment/decrement are done at the top. | Only initialization and condition checking are done at the top. |
Use | The 'for' loop used only when the number of iterations to be used is already known. | The 'while' block used only when the number of iterations is not known. |
Condition | If the condition is not put in the 'for' block, then it starts to execute infinitely. | If the situation is not given in the 'while', then the compiler gives a syntax error. |
Initialization | In the 'for', the initialization is done only once and never repeated. | In the 'while', if initialization is done during situation checking, then initialization is done each time when the block iterate. |
Iteration statement | In the 'for', the iteration statement is written at the top, so it starts execution only after all codes in the block are executed. | In the 'while', the iteration statement can be written after the block of code. |
Comparing While and Do While
Basis for Comparison | do-while | while |
Declaration. | do { block of codes; // body } while(Loop Condition); | while (condition) { block of codes; //body of while } |
Controlling Condition | In the do-while loop, the controlling statement appears at the end of the block. | In the 'while' the controlling situation appears at the start of the block. |
Iterations | The iteration occurs at least once even if the condition listed is false at the first iteration. | The iterations do not occur if, the condition at the first iteration, appears false. |
Also knows as | Exit-controlled loop. | Entry-controlled loop. |
Use of Semi-colon | Used at the end of the loop. | Not used. |
Common Mistakes
- It is an entry control looping structure.
- If required to execute the block of statement continuously until the given condition becomes False then we can use while.
- In the 'while' the condition is checked at the beginning of the loop entry. If the given expression becomes True then the body of the block is executed.
- The while loop contains only the expression part, initialization is done before the while statement.
Context and Application
This topic is significant in the professional exams for both undergraduate and graduate courses, especially for
- Bachelor in Computer Science.
- Bachelor in Computer Application.
- Masters in Computer Science.
- Bachelor in Computer Engineering.
Want more help with your computer science homework?
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.