Control Structures in Programming Languages - GeeksforGeeks

16 Jan, 2020
Control Structures are just a way to specify flow of control in programs. Any algorithm or program can be more clear and understood if they use self-contained modules called as logic or control structures. It basically analyzes and chooses in which direction a program flows based on certain parameters or conditions. There are three basic types of logic, or flow of control, known as:
Sequence logic, or sequential flowSelection logic, or conditional flowIteration logic, or repetitive flow
Sequence logic, or sequential flow
Selection logic, or conditional flow
Iteration logic, or repetitive flow
Let us see them in detail:
Sequential Logic (Sequential Flow)Sequential logic as the name suggests follows a serial or sequential flow in which the flow depends on the series of instructions given to the computer. Unless new instructions are given, the modules are executed in the obvious sequence. The sequences may be given, by means of numbered steps explicitly. Also, implicitly follows the order in which modules are written. Most of the processing, even some complex problems, will generally follow this elementary flow pattern.Sequential Control flowSelection Logic (Conditional Flow)Selection Logic simply involves a number of conditions or parameters which decides one out of several written modules. The structures which use these type of logic are known as Conditional Structures. These structures can be of three types:Single AlternativeThis structure has the form:If (condition) then:
     [Module A]   
[End of If structure]Implementation:C/C++ if statement with ExamplesJava if statement with ExamplesDouble AlternativeThis structure has the form:If (Condition), then:
     [Module A]
Else:
     [Module B]
[End if structure]
Implementation:C/C++ if-else statement with ExamplesJava if-else statement with ExamplesMultiple AlternativesThis structure has the form:If (condition A), then:
     [Module A]
Else if (condition B), then:
     [Module B]
        ..
        ..
Else if (condition N), then:
     [Module N]
[End If structure]
Implementation:C/C++ if-else if statement with ExamplesJava if-else if statement with ExamplesIn this way, the flow of the program depends on the set of conditions that are written. This can be more understood by the following flow charts:Double Alternative Control FlowIteration Logic (Repetitive Flow)The Iteration logic employs a loop which involves a repeat statement followed by a module known as the body of a loop.The two types of these structures are:Repeat-For StructureThis structure has the form:Repeat for i = A to N by I:
       [Module]
[End of loop]
Here, A is the initial value, N is the end value and I is the increment. The loop ends when A>B. K increases or decreases according to the positive and negative value of I respectively.Repeat-For FlowImplementation:C/C++ for loop with ExamplesJava for loop with ExamplesRepeat-While StructureIt also uses a condition to control the loop. This structure has the form:Repeat while condition:
     [Module]
[End of Loop]
Repeat While FlowImplementation:C/C++ while loop with ExamplesJava while loop with ExamplesIn this, there requires a statement that initializes the condition controlling the loop, and there must also be a statement inside the module that will change this condition leading to the end of the loop.
Sequential Logic (Sequential Flow)Sequential logic as the name suggests follows a serial or sequential flow in which the flow depends on the series of instructions given to the computer. Unless new instructions are given, the modules are executed in the obvious sequence. The sequences may be given, by means of numbered steps explicitly. Also, implicitly follows the order in which modules are written. Most of the processing, even some complex problems, will generally follow this elementary flow pattern.Sequential Control flow
Sequential logic as the name suggests follows a serial or sequential flow in which the flow depends on the series of instructions given to the computer. Unless new instructions are given, the modules are executed in the obvious sequence. The sequences may be given, by means of numbered steps explicitly. Also, implicitly follows the order in which modules are written. Most of the processing, even some complex problems, will generally follow this elementary flow pattern.
Sequential Control flow
Selection Logic (Conditional Flow)Selection Logic simply involves a number of conditions or parameters which decides one out of several written modules. The structures which use these type of logic are known as Conditional Structures. These structures can be of three types:Single AlternativeThis structure has the form:If (condition) then:
     [Module A]   
[End of If structure]Implementation:C/C++ if statement with ExamplesJava if statement with ExamplesDouble AlternativeThis structure has the form:If (Condition), then:
     [Module A]
Else:
     [Module B]
[End if structure]
Implementation:C/C++ if-else statement with ExamplesJava if-else statement with ExamplesMultiple AlternativesThis structure has the form:If (condition A), then:
     [Module A]
Else if (condition B), then:
     [Module B]
        ..
        ..
Else if (condition N), then:
     [Module N]
[End If structure]
Implementation:C/C++ if-else if statement with ExamplesJava if-else if statement with ExamplesIn this way, the flow of the program depends on the set of conditions that are written. This can be more understood by the following flow charts:Double Alternative Control Flow
Selection Logic simply involves a number of conditions or parameters which decides one out of several written modules. The structures which use these type of logic are known as Conditional Structures. These structures can be of three types:
Single AlternativeThis structure has the form:If (condition) then:
     [Module A]   
[End of If structure]Implementation:C/C++ if statement with ExamplesJava if statement with Examples
If (condition) then:
     [Module A]   
[End of If structure]
Implementation:
C/C++ if statement with Examples
Java if statement with Examples
Double AlternativeThis structure has the form:If (Condition), then:
     [Module A]
Else:
     [Module B]
[End if structure]
Implementation:C/C++ if-else statement with ExamplesJava if-else statement with Examples
If (Condition), then:
     [Module A]
Else:
     [Module B]
[End if structure]

Implementation:
C/C++ if-else statement with Examples
Java if-else statement with Examples
Multiple AlternativesThis structure has the form:If (condition A), then:
     [Module A]
Else if (condition B), then:
     [Module B]
        ..
        ..
Else if (condition N), then:
     [Module N]
[End If structure]
Implementation:C/C++ if-else if statement with ExamplesJava if-else if statement with Examples
If (condition A), then:
     [Module A]
Else if (condition B), then:
     [Module B]
        ..
        ..
Else if (condition N), then:
     [Module N]
[End If structure]

Implementation:
C/C++ if-else if statement with Examples
Java if-else if statement with Examples
In this way, the flow of the program depends on the set of conditions that are written. This can be more understood by the following flow charts:
Double Alternative Control Flow
Iteration Logic (Repetitive Flow)The Iteration logic employs a loop which involves a repeat statement followed by a module known as the body of a loop.The two types of these structures are:Repeat-For StructureThis structure has the form:Repeat for i = A to N by I:
       [Module]
[End of loop]
Here, A is the initial value, N is the end value and I is the increment. The loop ends when A>B. K increases or decreases according to the positive and negative value of I respectively.Repeat-For FlowImplementation:C/C++ for loop with ExamplesJava for loop with ExamplesRepeat-While StructureIt also uses a condition to control the loop. This structure has the form:Repeat while condition:
     [Module]
[End of Loop]
Repeat While FlowImplementation:C/C++ while loop with ExamplesJava while loop with ExamplesIn this, there requires a statement that initializes the condition controlling the loop, and there must also be a statement inside the module that will change this condition leading to the end of the loop.
Repeat-For StructureThis structure has the form:Repeat for i = A to N by I:
       [Module]
[End of loop]
Here, A is the initial value, N is the end value and I is the increment. The loop ends when A>B. K increases or decreases according to the positive and negative value of I respectively.Repeat-For FlowImplementation:C/C++ for loop with ExamplesJava for loop with Examples
Repeat for i = A to N by I:
       [Module]
[End of loop]

Here, A is the initial value, N is the end value and I is the increment. The loop ends when A>B. K increases or decreases according to the positive and negative value of I respectively.
Repeat-For Flow
Implementation:
C/C++ for loop with Examples
Java for loop with Examples
Repeat-While StructureIt also uses a condition to control the loop. This structure has the form:Repeat while condition:
     [Module]
[End of Loop]
Repeat While FlowImplementation:C/C++ while loop with ExamplesJava while loop with Examples
Repeat while condition:
     [Module]
[End of Loop]

Repeat While Flow
Implementation:
C/C++ while loop with Examples
Java while loop with Examples
In this, there requires a statement that initializes the condition controlling the loop, and there must also be a statement inside the module that will change this condition leading to the end of the loop.
Programming Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
R - Matrices
R - Charts and Graphs
R - Data Types
7 Highest Paying Programming Languages For Freelancers in 2022
Introduction of Object Oriented Programming
5 Best Languages for Competitive Programming
Exception Handling in C#
Maximum value of unsigned int in C++
Problem in comparing Floating point numbers and how to compare them correctly?
Difference between for and while loop in C, C++, Java