Types of instructions in C programming:
C instructions are the commands in the program that instructs the compiler to do certain action. Basically it gives the instruction to the compiler on how to achieve the goal of the program.
For example, if I want to instruct C compiler to perform multiplication of 2 numbers, I can do that with help of C Arithmetic instruction. Similarly, if I want to declare a variable, I should be able to do that using Type declaration instruction in C. Following are the 3 different types of C instructions.
There are three types of instructions in C. They are as follows:
1) Type Declaration Instruction
2) Arithmetic Instruction
3) Control instruction
- Type Declaration Instruction:
These instructions inform the compiler about the type of variables used. That means, whenever a variable is used in the program, we have to specify what types of data it can hold – like integer, float, double, character etc. This makes the compiler to store only those specific types of values in it. For example, a variable int Var1 declared as int will accept and store only integer values. It will never accept float or character values.
Declaration of variable is done at the beginning, inside the main function. This function is the starting point of any C program and all the variables used with main function needs to be declared at the beginning of it, before using it in the function. Any other functions in the program can also have variables and they needs to be declared at the beginning of respective functions.
- Arithmetic Instruction
These instructions are used to perform some arithmetic calculation within the program. It uses arithmetic operators like +, -, X, /, %, =, ++,–, +=,-+ etc. The variables that participate in the arithmetic operations are termed as operands. These instructions can have any simple to complex arithmetic calculations using these symbols.
- Control Instructions
Like the name suggests, these instructions are used to control the flow of the program execution. They maintain certain order in which program needs to be executed. These order of execution may be based on certain conditions or may be based on certain values – may be input values or some result values.
There are four types of control instructions in C.
- Sequence Control Instructions
- Decision / Selection Control Instructions
- Loop Control Instructions
- Case Control Instructions
Sequence Control Instructions
These instructions are responsible for executing the instructions one after the other, as they appear in the program. There are not any checks on the condition or values to control the execution. For example, a normal program in which all the arithmetic operation like addition, subtraction, multiplication and division is carried out one after the other. Here there is no check on user option, or any other conditions. It will display results of all the operations in the order they appear in the program.
Decision / Selection Control Instructions
This will have set of conditions to execute the instructions within it. If the condition is true, then it will execute the instructions, else it will execute other set of instructions. This type of instructions use if or while statements to make the decision.
For example, suppose user enters the options like A, S, D or M to indicate addition, subtraction, multiplication and division. Depending on the option entered by the user, the program may add, subtract, multiply or divide the numbers. Here it will not execute all the operation but only one of them entered by the user.
Loop Control Instructions
There will be certain need for executing a set of instruction for certain number of times. This is done by using loop control statement. These loops will have limited number of iterations or will have certain conditions which in turn will give iterations for the instructions to be executed. These instructions will use for loops or while or do/while statements to get the loop iterations.
For example, suppose we have display the first 5 Name of name in the array. This will use for loop to iterate through the array from its beginning till 5th name to display the names in it. Similarly, suppose we need to display all names in sorted array where average marks are not more than 35%. This will use do/while loop to iterate through the array (here we do not know the exact number of iterations) until the condition is matched.
Case Control Instructions
This is similar to decision control instructions where condition will be checked for the execution of the instructions. But this instructions will use switch case statements to check the conditions.
These control instructions cannot be executed without control statements. There are 4 types of control statements :
- If..Else statement or If..Else If..Else statement
- While loop or Do/While loop statements
- Switch Case statements
- For Loop Statements