Define control structure. Explain different types of control structure.
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.
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 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:
Implementation:
Implementation:
Implementation:
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
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:
Repeat While Flow
Implementation:
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.
Structures in Cis a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.
Defining a structure:To define astructure, you must use thestructstatement. The struct statement defines a newdata type, with more than or equal to one member. The format of the struct statement is as follows:
struct [structure name] { member definition; member definition; ... member definition; }; (OR) struct [structure name] { member...