Here we will discuss how we can write effective codes with the help of loops. It is a general perception that the approach using loops is treated as naive approach to solve a problem statement. But still, there is a huge scope of improvisation here. So basically, a loop statement allows us to execute a statement or group of statements multiple times. In addition to this, we can also manipulate its execution according to our requirements and write robust codes.
Suppose you want to find the area and perimeter of a circle. The general approach for a rookie programmer would be to check if the radius is greater than zero. If it is not then print enter non zero positive number. Now, what are the flaws here? If the user enters a number less then or equal to zero multiple times then we need to compile our code again and again since the program will print enter non zero positive number and finish its execution.
Example 1
Java
import java.util.Scanner;
public class GFG {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println( "Enter Radius::" );
double radius = s.nextDouble();
double perimeter;
double area;
if (radius <= 0 ) {
System.out.println(
"please enter non zero positive number " );
}
else {
perimeter = 2 * Math.PI * radius;
area = Math.PI * radius * radius;
System.out.println( "Perimeter:: " + perimeter);
System.out.println( "Area:: " + area);
}
}
}
|
Output:
C:\Users\USER\Desktop\Network Java>javac GFG.java
C:\Users\USER\Desktop\Network Java>java GFG
Enter Radius::
-5
please enter non zero positive number
//Execution Finished
C:\Users\USER\Desktop\Network Java>javac GFG.java
C:\Users\USER\Desktop\Network Java>java GFG
Enter Radius::
0
please enter non zero positive number
//Execution Finished
Output explanation:
In the above example, we saw that If the user enters any non zero and negative integers than we need to compile and run the program again and again. This awkward situation can be prevented using while loop. Using loops we can manipulate its execution according to our requirements and write healthy codes.
Example 2
Java
import java.io.*;
class GFG {
public static void main(String[] args)
throws IOException
{
double radius = 50 ;
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println( "Enter Radius::" );
while (radius > Integer.MIN_VALUE) {
radius = Double.parseDouble(br.readLine());
if (radius > 0 ) {
double Perimeter = 2 * Math.PI * radius;
double Area = Math.PI * Math.pow(radius, 2 );
System.out.println( "Perimeter:: "
+ Perimeter);
System.out.println( "Area:: " + Area);
break ;
}
System.out.println(
"please enter non zero positive number" );
System.out.println( "Enter Radius::" );
}
}
}
|
Output:
C:\Users\USER\Desktop\Network Java>javac GFG.java
C:\Users\USER\Desktop\Network Java>java GFG
Enter Radius::
-10
please enter non zero positive number
Enter Radius::
0
please enter non zero positive number
Enter Radius::
3
Perimeter:: 18.84955592153876
Area:: 28.274333882308138
//Execution Finished
Note: In the above program, we see if we read the buffer once then, it will no longer store anything. If we try to read it again then it will ask for input.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Sep, 2021
Like Article
Save Article