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...
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...
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...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
This problem will give you hands-on practice with the following programming concepts: • All programming structures...
This problem will give you hands-on practice with the following programming concepts: • All programming structures (Sequential, Decision, and Repetition) • Methods • Random Number Generation (RNG) Create a Java program that teaches people how to multiply single-digit numbers. Your program will generate two random single-digit numbers and wrap them into a multiplication question. The program will provide random feedback messages. The random questions keep generated until the user exits the program by typing (-1). For this problem, multiple methods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT