The concepts of instructions and operators are present in C programming, and they are the most fundamental elements of any programming language because they allow us to instruct our computer to perform tasks.
Instructions
- A Program is nothing but a set of instructions.
- A C program is a set of instructions that contains instructions to perform specific tasks or actions.
Types of Instructions
- Type declaration instructions
- Arithmetic instructions
- Control instructions
Type declaration instructions
- Declare the variable before using it.
- While declaring the type of a variable, we can also initialize it.
Valid | Invalid |
---|---|
1. int a,b,c; a=b=c=1; | int a,b,c=1; float b=a+3.1; a=1.5; |
2. float a=1.5; b=a+3.1; |
Arithmetic instructions
- *,/,-,+ are arithmetic operators.
- = is an assignment operator.
- operands can be int/float etc.
Var ↰ ↱ Operators
int i = (3 * 4) + 1
↳ — ↳ — ↳ Operands
Valid | Invalid |
---|---|
1. int b = 2; | int b+c = a; |
2. int a = b+c; | int a = bc; |
3. int a = b/c; | int a = b^c; |
- % is a modular division operator.
- returns the remainder.
- cannot be applied to float.
Eg; 3%2=1 ; -3%2=-1;
There is no operator to perform exponentiation in c, However, we can use pow(x,y) from <math.h> header file.
Type Conversion
An Arithmetic operation between
- int and int = int
- int and float = float
- float and float = float.
Examples |
---|
5/2 = 2 ( int ) |
5.0/2 = 2.5 ( float ) |
2/5 = 0 ( int ) |
2.0/5 = 0.4 ( float ) |
Note
int a = 3.5; in this case 3.5(float) will be demoted to 3(int) because a is not able to store floating values.
float a =8; Here a will store 8.0; ( promoted to float ).
Operator precedence in C
- The operator precedence species whose values will be evaluated first and next.
- in c language, mathematical rules like BODMAS, no longer exist.
List of operator priority in c
Priority | Operators |
---|---|
1st | *,/,% |
2nd | +,- |
3rd | = |
• Operators of higher priority are evaluated first in the absence of parenthesis.
Associativity of operators.
When operators of the same priority are present in an expression, associativity takes care of the tie.
- x = 4*3/6*2 = (4*3)/(6*2)….
- x*y/z = (x*y)/z
- x/y*z = (x/y)*z
*,/ always follows left-to-right associativity.
Points to Remember
- An Operator is simply a symbol that is used to perform operations.
- Every operator has Priority and Associativity.
Control instructions
Determines the flow of control in a program.
There are four types of control instructions in c.
- Sequence control instruction
- Decision control instruction
- Loop control instruction
- Case-control instruction
Conditional instructions
Sometimes we want to watch movies if the day is Sunday.
Sometimes we order junk food if it is our friend’s birthday in a hostel or something else etc.
All these are decisions that depend on a condition being met.
In C language too; we must be able to execute instructions on a condition(S) being met.
Decision-Making Instructions in c
- If-else Statement
- Switch Statement
If-else Statement
C language uses the keyword if and else to implement the decision control instruction.
if ( this condition is true )
{
statements 1;
}
else {
statements 2;
}
if the test expression is evaluated to true, statements inside the body of If are executed.
If the test expression is evaluated as false, statements inside the body of else are executed.
Note: Else block is not necessary but optional.
Relational operators in C
Relational operators are used to evaluate conditions ( true or false ) inside the if statements.
A Relational operator checks the relationship between the operands, if the relation is true, it returns 1; if false it returns 0;
relational operators are used in decision-making and loops.
Some Examples of relational operators.
== | Equals |
= | Greater than or Equal to |
< | Greater than |
> | Smaller than |
!= | Not equal to |
Note: ‘=’ is used for an assignment whereas ‘==’ is used for an equality check.
In c language, a non-zero value is considered as True.
Logical Operators
In c language, there are three major logical operators.
- && – AND
- || – OR
- ! – NOT
Usage of Logical Operators
&& – when both conditions are true.
- “1” && “0” – False
- “1” && “1” – True
|| – when at least one of the conditions is true.
- “1” || “1” – True
- “1” || “0” – True
! – when we have to return true if given is false and false if given is true.
- !(3==3) – False
- !(3>3) – True
Else if statement
Instead of using multiples of statements, we can also use “else if” along with if, thus forming an if-else if-else ladder.
if ( condition )
{
statements;
}
else if ( condition )
{
statements;
}
- using if-else if-else reduces indents, the last “else” is optional, also there can be any number of “else if”.
- last value is executed only if all conditions fail.
Operator Precedence
Priority | Operator |
---|---|
1st | ! |
2nd | *,/,% |
3rd | +,- |
4th | <>,<=,>= |
5th | ==,!= |
6th | && |
7th | || |
8th | = |
Conditional Operators
- Ternary operator in c
A shorthand “if-else” can be written using the conditional or ternary operator.
condition ? Do Something if True ; Do Something if false.
Note: “;” is a null statement, it doesn’t do anything on execution.
Switch Case Control Instructions
Switch Case is Used When we have to make a choice between a number of alternatives for a given variable.
- Switch Case consists of conditional-based cases and a default case.
- In a switch statement, the case value can be of “char” and “int” type.
- Each statement of the case can have a break statement, but It is optional.
- Cases can be in any order.
- Nested Switch ( Switch inside switch) are Allowed.
switch ( numbers or expression)
{
case c1:
//Do Something
break ;
case c2:
//Do Something
break ;
default: //Do Something
}
The value of the integer expression is matched against c1, c2… If it matches any of these cases that are along with all subsequent “case” and “default” statements are executed.
Loop Control Instructions
Sometimes we want our programs to execute a few sets of instructions over and over again.
Examples
- Printing 1 to 100.
- First 20 even numbers from 1 to 200.
Loops make it easy for programmers to tell the computer that a given set of instructions must be executed repeatedly.
Types Of Loops
In c language, primarily there are three types of loops.
- while loop
- do-while loop
- for loop
While Loop
The while loop loops through a block of codes as long as a specified condition is true.
If the condition never becomes false, the while loop keeps getting executed. And such a loop is known as an infinite loop.
Its workflow is firstly it checks the condition and then executes the body.
while ( condition ){
//do something
}
Example
int i = 0;
while ( i<10 ) {
printf("The value of i is %d",i);
i++;
}
The loop counter need not be int, it can be float as well.
Increment and Decrement Operators
- i++ = i is increased by 1.
- i– = i is decreased by 1.
Example
printf("--i = %d", --i);
↳ This first decrements i anad then print it.
printf("i-- = %d", i--);
↳ In this first prints i and then decrements it.
Note
- +++ operator does not exist.
- += is compound assignment operator just like -= , *= , /= , &%= ( important).
do while loop
It is an exit-controlled loop
Its workflow is firstly executing the body and then checking the condition.
It is a variant of a while loop.
do {
//do something
} while ( condition );
- Do while loop works very similar to the while loop.
- While loop checks the condition and then executes the code.
- Do while loop executes the code and then checks the condition.
Normally, Do while loop means a loop that executes at least one.
for Loop
It is an entry Controlled loop.
It requires 3 conditions parameters i.e. check expression; conditional statement and uninary operation for updation.
It’s workflow is an initialisation, check / test and then updation.
for ( initialize ; test ; increment or decrement )
{
//do something
}
For loop is mostly used when we know how many times we want to loop through a block of code.
Initialisation
- setting a loop counter to an initial value.
check / test
- checking a Condition
Updation
- Updating the loop counter
for( i=0; i<3,i++){
Printf ("%d",i);
}
Output = 012
A case for Decrementing for Loop
for( i=5; i; i--){
Printf("%d",i);
}
Output = 5 4 3 2 1
Break Statement
It is used to exit the loop irrespective of whether the condition is true or false.
Whenever a “break” is encountered inside the loop , the control is sent outside the loop.
Example :
int i;
for( i=0; i<10; i++){
if(i==4){
break;
}
printf("%d",i);
}
Output = 0 1 2 3
Continue Statement
This is used to immediately move to the next iteration of loop.
The control is taken to the next iteration, thus skipping everything below ” continue ” inside the loop for that iteration.
Example :
int i;
for( i=0; i<10; i++){
if(i==4){
Continue;
}
printf ("%d",i)
}
Output : 0 1 2 3 4 5 6 7 8 9
Points to remember
- break Statement completely Exists the loop.
- continue statement skips the particular iteration of the loop.
- loop counter can be float or even character.
Have a look