What are loops? Why do we need loops in our program ?
While programming in any language whether it is c , c++ , java , python ,etc. we often heard the word loops.
So hear we’ll learn about loops and also we’ll learn the uses of loops in our program.
So by hearing the word loop the thing which comes in our mind is something related to the spiral, trap or hinge.
Now we can relate the word loop in programming that loops are related to trap our code in our program to check or to perform some task till some particular condition or something like that.
Now let’s move towards the proper defination of the loops
Loops:- loops are used to execute a set of statement until a particular condition is satisfied
Hopefully with the above definition you have understand the meaning of loops
Now let’s move towards the types of loops
So hear we will study the three types of loop’s which is as follows
For loop
While loop
Do while loop
So let’s begin with the very first in our list which is for loop
Syntax :-
for ( statement1; statement2 ; statement3)
{code block }
Where the statement1 shows the initialization of the loop, statement2 states the condition of the loop and statement3 is for reinitialization of the loop which is either increment or decrement.
While using this for loop in your program you need to remember some minor mistakes made by us during programming which will throw some unexpected errors or may be give some unexpected results
So, the very first thing which you need to keep in mind is if the first statement is missing which is initialization of the loop then your program will throw an error at compile time so you need to take proper care of the initialisation of your loop.
Now come to the next common mistakes which is not good for our program
If the second and third statement is missing in our program which indicates condition and reinitialisation of the loop then our program will run infinitely till the completely filling of heap memory of our system and again which is not good for our program
Now with the help of above syntax try to implement a for loop in your program and letter on use this to solve some simple or beginner level questions
Now we will try to understand the implementation and uses of for loop with the help of some examples
We have to write a program to print the factorial of a number with the help of for loop
ans.:-
Let the number be “n” and the factorial of the number is
n! = n(n-1)(n-2).......
Now we’ll write the code for this problem using java
Public class factorial {
public static void main (string[]args){
scanner sc= new scanner (system.in);
int n = Sc. nextInt();
int factorial = 1;
for (int i= n; i>=1; i-- ){
factorial = factorial * i
}
system.out.println(factorial);
}
}
The above code will print the factorial of the number given by the user
Now we’ll see the use of break and continue statements which is also a very helpful role in our program.
So very first we’ll see the use of break statement
Syntax :- let’s try to understand the use of break statement with the help of syntax below
for (statement1; statement2; statement3){
// code blocks
if (condition){
break;
}
}
Let's see the use of break statement for the better understanding of break statement
i.e. :- use of break condition
for (int i=1;i<=100;i++){
if (i=35){
break;
}
system.out.println(i);
}
}
The above written java code will print the numbers till 34 at the point when number becomes equal to 35 then the program will stop printing the numbers after 35
Now lets see the implementation of continue statement in our program
Syntax :- below with the help of syntax let’s try to understand the use and implementation of continue statement
for (statement 1; statement2; statement3){
//code block
if (condition){
continue;
}
}
Now we’ll see the use of continue and understand the implementation of continue statement with the help of an example
i.e. :- write a program to print the numbers from 1 to 100 but we have to skip the number between 40 to 50
ans :- so for this example we’ll using a java code to solve the above problem
public class continue {
public static void main (string[]args){
for (int i=1;i<=100;i++){
if (i>=40 && i<=50)
continue;
}
system.out.println(i);
}
}
So the above written program will print the number between 1 to 100 and skip the number between 40 to 50.
Syntax :-
initialisation
while (condition){
//code inside the body of while loop
reinitialisation
}
In the above syntax we have seen the the while loop in a program is used to check a condition in program now we’ll understand the use of while loop in our program with the help of an example
i.e. : - we have to write a program to find the sum of the digits of a number that is for a given number 123 the sum is 6
Now we will study the do while loop with the help of an example and with the help of syntax
In do while loop the statement verifies at least one condition then after it comes to the another condition
In this loop the statement is executed at least one time
Now below with the help of syntax we’ll try to understand the implementation of do while loop in our program
Syntax :-
do {
// codes inside the body if do while loop
} while (condition);
Now we’ll see the use of do while loop with the help of an example
i.e. :- we have to write a program which checks whether a number is zero or not
ans :- so hear we will write a program using do while loop to check weather the number is zero or not and the language which we are using hear is java
public class Do While Loops {
public static void main (string [ ]args){
int n =0;
do {
system.out.println(“n is +n”);
}
while(n!=0);
}
}
Hopefully with the help of above mentioned information i have tried to understand the loops and it’s implementation with the help of examples and syntaxes
Now you will be much familiar with the syntaxes and the implementation of the loops so you can use these loops in your own program.
At the last to become more familiar with the programming you need to do practice with your own without practice you can't get the best result out of it so practice more and more to get familiar with programming
Stay connected with us in future we will bring more such valuable content
Thankyou
BY :- ADARSH GUPTA