Ascending Order in Java Using If Else Statement

Ascending order in java using if else statement. In the Java programming language, an ascending order program is used to arrange the numbers that are entered in an array. The best method for developing programming skills in any programming language is a sorting program.

Ascending Order Definition

The least or smallest item is placed first, followed by the biggest or largest item. The kids were arranged in ascending height order. From lowest to highest, test scores are displayed in ascending order.

Purpose of For Loop in Java

A for loop is a repetition control structure that allows you to design a loop that must be repeated a certain number of times quickly. When you know how many times a task will be repeated, a for loop comes in handy.

For Loop in Java

In Java, the “for” loop is an entry-controlled loop that allows a user to iteratively run a block of statement(s) for a set number of times. The test-condition specified inside the “for” loop determines the number of iterations. The Java “for” loop is one of the most simple Java loops to grasp. A “For” Loop is used to repeat a block of code for a certain number of times.

Temporary Variable in Java

A temporary array persists only for the period of the data step in which it is defined. A temporary array can be used to store constant values used in calculations. There are no comparable variables to identify the array elements in a temporary array. 

A temporary variable is a variable in computer programming that has a brief lifetime and is used to retain data that will be removed quickly or before it can be stored in a more permanent memory location. It is commonly stated as a local variable, or a variable with local scope, because it is short-lived.

Ascending Order in Java Using If Else Statement

import java.util.Scanner;
public class Main 
{
public static void main(String[] args) 
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter the size of an array");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements");
for (int i = 0; i < n; i++) 
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) 
{
for (int j = i + 1; j < n; j++) 
{
if (a[i] > a[j]) 
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("After sorting");
for (int i = 0; i < n - 1; i++) 
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}

Output:
Enter the size of an array 3 
Enter the elements   
34
56
78
After sorting 34,56,78