Question

In: Computer Science

Which are the three control structures used in java programming. Which of these three control structures...

Which are the three control structures used in java programming. Which of these three control structures is used in every program or application

Solutions

Expert Solution

There are three types of control structures in java

1. Sequential:

In this control structure, Statements are executed one after another without missing any statement in between. This is the basic control structure of Java programming.

2. Conditional

In this control structure, there are different blocks of statements and a condition decides, whether a block should be executed or not. If a condition is met, then the block of statements are executed, if not met, then the whole block is ignored.

eg. if , if-else, switch etc.

3. Iterative or repetitons

In this control structure , a block of statements are called multiple times again and again till a specific condition is getting satisfied. In this type of control structure, a condition is checked multiple times, and the block is executed again and again till the condition is true.

eg. for, do-while, while etc

Below is the example of code to demonstrate each of the control structure

public class ControlStructure {
        public static void main(String[] args) {

                // Sequential Control Structure starts
                int a = 5;
                int b = 10;
                int c = a + b;
                System.out.println("Sum is " + c);
                // Sequential Control Structure ends

                // Conditional Control Structure Starts
                if (c > 0) {
                        System.out.println("Value of C is greater than 0");
                } else {
                        System.out.println("Value of C is less than 0");
                }
                // Conditional Control Structure ends

                // Iterative Control Structure starts
                for (int i = 0; i < 5; i++) {
                        System.out.println("Calling multiple times...");
                }
                // Iterative Control Structure ends
        }
}

Output

As you can see in above code,

First four statements are executed one by one sequentially So they fall under first category i.e. sequential control structure.

Next we are checking a condition if c > 0, then we are printing that value of c is greater than 0, else we are printing that value of c is less than zero. So here it depends on value of c that which statement is executed. This is conditional control structure.

Next there is a loop which runs from i = 0 to i < 5 , so it runs for i = 0,1,2,3,4 i.e. 5 times and each time it prints one line which is Calling multiple times.... So a statement is getting repeated mutliple times. And it stops when i < 5 condition is not met. This is iterative control structure or repetitons

Out of these 3, Sequential Control structure is used in every program, becase in every program, there are series of instructure which are executed one by one. So Sequential control structure exists in every program.


Related Solutions

java from control structures through objects 6th edition, programming challenge 5 on chapter 16 Write a...
java from control structures through objects 6th edition, programming challenge 5 on chapter 16 Write a boolean method that uses recursion to determine whether a string argument is a palindrome. the method should return true if the argument reads the same forward amd backword. Demonstrate the method in a program, use comments in the code for better understanding. there must be a demo class and method class also .. generate javadocs through eclipse compiler. And make a UML diagram with...
There are various selection structures that can be used in programming. What are the different relational...
There are various selection structures that can be used in programming. What are the different relational operators used in selection structures in the Python programming language? Also, explain what short-circuiting is with respect to compound conditional expressions in Python. Provide examples to support your response in addition to constructive feedback on the structures and circumstances posted by your peers. Provide at least one reference to support your findings.
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the functionalities of all three programming languages. Why would you choose one language over another? Provide code examples demonstrating their usefulness in a real-world scenario.
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
Java programming extra credit. The following program will be used to test your knowledge on Implementing...
Java programming extra credit. The following program will be used to test your knowledge on Implementing with tester classes. Included is the Details class. Your assignment is to create a class named DetailsTester using info from the two classes(Procedure class is only included to know the details of the procedure, which includes desc, date, and cost) Patient class is included as well.That will properly implement the class. package doctorOffice; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; public class Patient extends Person...
2. What are the three program control structures? Define each thoroughly and give an example of...
2. What are the three program control structures? Define each thoroughly and give an example of each. 3. Explain in detail the difference between a Class Variable and an Instance Variable. How do we declare a Class Variable? 4. Which of the following is true? a.   Java programs can run on different platforms, like Windows and Unix, without being recompiled. b.   C++ programs have always run more slowly than Java programs. c.   In contrast to Java, C# requires programmers to...
In the java programming language. How would you find if THREE (3) numbers in an array...
In the java programming language. How would you find if THREE (3) numbers in an array add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 270? YES (200+50+70) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums...
What are control structures in Linux/Unix?
What are control structures in Linux/Unix?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT