Sequence Statements are statements that are executed in a sequential manner, one after another, in the order they appear in the code.
In programming, sequence statements are the most basic type of statements and are used to perform a series of operations or tasks. Each statement is executed one at a time, and the flow of execution moves from the first statement to the next until all statements have been executed.
Sequence statements are the simplest type of control structure in programming where each line of code is executed one after the other.
Think of it like a recipe: you can't eat the cake before you bake it, and you can't bake it before you mix the ingredients.
For example, the steps to calculate the area of a rectangle are:
When building such an interactive program to calculate the area, you must follow the following steps:
As a programmer, you must know the syntax to use to:
The syntax to accomplish these varies from one programming language to another.
We've already learned about variable declaration and initialization from our previous lessons.
The syntax for displaying output in Java is the System.out.println() or System.out.print() method.
The text to display is specified as a string and written between double quotes (")
For example, to display the message "Hello, World!" in Java, you can use the following code:
System.out.println("Hello, World!");
To display "WELCOME TO JAVA PROGRAMMING" in Java, you can use the following code:
System.out.println("WELCOME TO JAVA PROGRAMMING");
In the above program to calculate the area of a rectangle, to display "Enter length:", you can use the following code:
System.out.print("Enter length: ");
To display "Enter width:", you can use the following code:
System.out.print("Enter width: ");
We now have the necessary syntax knowledge to write our program to calculate the area of a rectangle in Java.
Since the length and width could be decimal values, it is appropriate to store them as floats or doubles.
To handle user input in Java, we use the Scanner class from the java.util package.
| Data Type | Scanner Method | Description & Example Usage |
|---|---|---|
int |
scanner.nextInt() |
Reads an integer value (e.g., age, count).int age = scanner.nextInt(); |
float |
scanner.nextFloat() |
Reads a single-precision decimal number.float length = scanner.nextFloat(); |
double |
scanner.nextDouble() |
Reads a double-precision decimal number.double price = scanner.nextDouble(); |
boolean |
scanner.nextBoolean() |
Reads a boolean value (true or false).boolean isActive = scanner.nextBoolean(); |
String (Single Word) |
scanner.next() |
Reads the next token/word up to the first space.String firstName = scanner.next(); |
String (Full Line) |
scanner.nextLine() |
Reads an entire line of text until a newline character.String fullName = scanner.nextLine(); |
long |
scanner.nextLong() |
Reads a large integer value.long population = scanner.nextLong(); |
short |
scanner.nextShort() |
Reads a short integer value.short smallNum = scanner.nextShort(); |
byte |
scanner.nextByte() |
Reads a byte value.byte b = scanner.nextByte(); |
import java.util.Scanner;
public class RectangleArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
float length, width; /* Declare variables to store length and width */
System.out.print("Enter length: "); /* Prompt the user to enter the length */
length = scanner.nextFloat(); /* Read the length from the user and store it in the variable 'length' */
System.out.print("Enter width: "); /* Prompt the user to enter the width */
width = scanner.nextFloat(); /* Read the width from the user and store it in the variable 'width' */
System.out.println("The area of a rectangle of length " + length + " cm and width " + width + " cm is " + (length * width) + " cm square");
scanner.close();
}
}
Notes:
+ operator to concatenate strings and variables in output statements.java.util.Scanner, creating a Scanner object, and calling methods like nextFloat().Note: The executable code should be placed inside the main method.
The explanatory statements are called comments which are ignored by the compiler.
You can run the code in your IDE or use the Java online compiler below to test the program:
https://www.programiz.com/java-programming/online-compiler/
Write a program in Java to calculate the circumference and area of a circle.
Notes:
Scanner.final keyword (e.g., final float PI = 3.142;).The sequence of steps to solve this problem is as follows:
final float PI = 3.142;.import java.util.Scanner;
public class CircleCalculator {
// Define a constant for PI
static final float PI = 3.142;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
float radius, circumference, area; /* Declare variables to store radius, circumference, and area */
System.out.print("Enter radius: "); /* Prompt the user to enter the radius */
radius = scanner.nextFloat(); /* Read the radius from the user and store it in the variable 'radius' */
circumference = 2 * PI * radius; /* Calculate the circumference using the formula C = 2 * π * r */
area = PI * radius * radius; /* Calculate the area using the formula A = π * r^2 */
System.out.println("The circumference of a circle with radius " + radius + " cm is " + circumference + " cm");
System.out.println("The area of a circle with radius " + radius + " cm is " + area + " cm square");
scanner.close();
}
}
Alternatively, we could declare the constant locally inside the main method:
import java.util.Scanner;
public class CircleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final float PI = 3.142; /* Local constant for PI */
float radius, circumference, area; /* Declare variables to store radius, circumference, and area */
System.out.print("Enter radius: "); /* Prompt the user to enter the radius */
radius = scanner.nextFloat(); /* Read the radius from the user and store it in the variable 'radius' */
circumference = 2 * PI * radius; /* Calculate the circumference */
area = PI * radius * radius; /* Calculate the area */
System.out.println("The circumference of a circle with radius " + radius + " cm is " + circumference + " cm");
System.out.println("The area of a circle with radius " + radius + " cm is " + area + " cm square");
scanner.close();
}
}
In the above Java program, the circumference and area are stored before being displayed to the user.
In case you decide not to store them in variables but calculate directly inside the print statement, the code will be:
import java.util.Scanner;
public class CircleCalculatorDirect {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final float PI = 3.142;
float radius; /* Declare variable to store radius */
System.out.print("Enter radius: "); /* Prompt the user to enter the radius */
radius = scanner.nextFloat(); /* Read the radius from the user and store it in the variable 'radius' */
System.out.println("The circumference of a circle with radius " + radius + " cm is " + (2 * PI * radius) + " cm");
System.out.println("The area of a circle with radius " + radius + " cm is " + (PI * radius * radius) + " cm square");
scanner.close();
}
}