✔ C-LANGUAGE CHAPTERS:-
1. C-Introduction
2. C-Constants
3. C-Basic Programs
4. C-Instructors
5. C-Operators
6. C-If Else Statement
7. C-Loop Contructs
8. C-Nesting Of Loops
9. C-Programming Examples
10. C-Functions
11. C-Functions Naming Convention
12. C-Pointers
13. C-Arrays
14. C-Arrays & Functions
15. C-Strings
16. C-Structures
17. C-Union
18. C-Storage Classes
19. C-PreProcessor
20. C-Header Files
21. C-R/W one Line
22. C-Accessing Random File
23. C-Important Point
24. C-Interview Questions
1. C-Introduction
2. C-Constants
3. C-Basic Programs
4. C-Instructors
5. C-Operators
6. C-If Else Statement
7. C-Loop Contructs
8. C-Nesting Of Loops
9. C-Programming Examples
10. C-Functions
11. C-Functions Naming Convention
12. C-Pointers
13. C-Arrays
14. C-Arrays & Functions
15. C-Strings
16. C-Structures
17. C-Union
18. C-Storage Classes
19. C-PreProcessor
20. C-Header Files
21. C-R/W one Line
22. C-Accessing Random File
23. C-Important Point
24. C-Interview Questions
C-Instructors: Previous Next: C-If-Else-Statement
“Operators are special symbol that are used to perform Arithmetic operation on variables”.
It is an essential element of an expression. Suppose we are writing a + b, in this a and b are operands and ‘+’ is operator and the whole makes an Expression.
It is an essential element of an expression. Suppose we are writing a + b, in this a and b are operands and ‘+’ is operator and the whole makes an Expression.
Category of operators according to number of operands: -
(A)Unary (B) Binary (C) Ternary
- Unary: - When we have a single operand in expression. E.g.: - increment/decrement operators (a++).
- Binary: - when we have two operands in expression. E.g.: - A + B
- Ternary: - when we have three operands in expression. E.g.: - A? B: C
Types of Operators: -
Mainly there are 8 operators which are supported by C.
- Arithmetic operator.
- Relational operator.
- Logical operator.
- Bitwise operator.
- Increment/Decrement operator.
- Assignment operator.
- Conditional operator.
- Special operator.
- Arithmetic operator: - These operators are used to perform Arithmetic calculation on operands. The operands can be a variable or an constant.
Operator | Symbol | Expression |
Addition | + | x + y |
Subtraction | - | x – y |
Multiplication | * | x * y |
Division | / | x / y |
Modulus | % | x % y |
E.g.: - 10 + 20 = 30 20 - 10 = 10
10 * 5 = 5012 / 2 = 6
11 % 5 = 2
- Relational operator: - As a name implies these operators are used to define the relationship between operands. These operators are used to compare values. An expressions having these operators are known as Relational expression. Relational operators are as follows: -
Operator | Operator name | Example |
< | Less than | 10 < 20 TRUE |
> | Greater than | 10 > 20 FALSE |
<= | Less than or equals to | 10 <= 10 TRUE |
>= | Greater than or equals to | 10 >= 11 TRUE |
== | Equals to | 10 == 10 TRUE |
!= | Not Equal to | 10 != 10 FALSE |
- Logical operators: - These are used to combine two or more conditions together to form a single condition. They always returns answer in Boolean i.e. True or False. Three operators are:
AND (&&) OR(||) NOT(!)
3.1) AND (&&): - It combines two conditions and return True when both conditions are True, otherwise it returns False. E.g.: - 10 > 5 (T) && 10 < 20 (T) TRUE
10 < 20 (T) && 20 > 10 (F) FALSE
10 == 20 (F) && 20 == 10 (F) FALSE
10 > 5 (T) || 10 < 20 (T) TRUE
10 < 20 (T) || 20 < 10 (F) TRUE
10 == 20 (F) || 20 == 10 (F) FALSE
This is used to reverse the specified condition. If the condition is True then it returns False and if condition returns False then it returns True.
E.g.: - 10 > 5 (T) FALSE
10 < 5 (F) TRUE
Logical operators are divided into If statement and Else If Statement.
(a) if statement: - these are further divided into following :
- simple if
- nested if
- multiple if
if (condition)
{
True statement part;
}
{
True statement part;
}
Example 1 : Write a program to take input from user and check the inputted number is 20 or not?
Solution:
#include<stdio.h>
#include<conio.h>
void main( )
{
int num;
printf(“Enter a Number : ”);
scanf(“%d”, &num);
if( num == 20 )
{
printf(“Number is 20”);
}
}
2. nested if: - In simple if we have only single if with a single True statement, but in nested if we have multiple`s if statements placed inside a if. This can be called as if another if.
#include<conio.h>
void main( )
{
int num;
printf(“Enter a Number : ”);
scanf(“%d”, &num);
if( num == 20 )
{
printf(“Number is 20”);
}
}
2. nested if: - In simple if we have only single if with a single True statement, but in nested if we have multiple`s if statements placed inside a if. This can be called as if another if.
If number is Even if number is > 40
Example 2: Write a program to print a message “Accurate…!!” when the following conditions are satisfied :
Solution:
- if number is not equal to 60.
Example 2: Write a program to print a message “Accurate…!!” when the following conditions are satisfied :
Solution:
#include<stdio.h>
#include<conio.h>
void main( )
{
int num;
clrscr( );
printf(“Enter a Number : ”);
scanf(“%d”, &num);
if( num > 40 )
{
if(num % 2 == 0)
{
if(num != 60)
{
printf(“Accurate…!!”);
}
}
}
}
#include<conio.h>
void main( )
{
int num;
clrscr( );
printf(“Enter a Number : ”);
scanf(“%d”, &num);
if( num > 40 )
{
if(num % 2 == 0)
{
if(num != 60)
{
printf(“Accurate…!!”);
}
}
}
}
clrscr( ): - this function is used to clear the output screen.
Note: When we have a multiple conditions to be satisfied then Logical AND(&&) can be used and makes a program short and simple and also easy to understand. Now take a same example by using AND.
E.g.:- Logical AND(&&)
#include<stdio.h>
#include<conio.h>
void main( )
{
int num;
printf(“Enter a Number : ”);
scanf(“%d”, &num);
if( num > 40 && num % 2 == 0 && num != 60 )
{
printf(“Accurate…!!”);
}
}
E.g.: - Logical OR( || )
In above example if we change the condition i.e. display message if any one of the condition is True.
#include<stdio.h>
#include<conio.h>
void main( )
{
int num;
printf(“Enter a Number : ”);
scanf(“%d”, &num);
if( num > 40 || num % 2 == 0 || num != 60 )
{
printf(“Accurate…!!”);
}
}
iii. multiple if: - in nested if, all condition must be satisfied to execute the true part but in multiple if, all condition plays a different role to execute their own true part statement. Difference can be defined is that it contains multiple true part statements depending upon the specified conditions and conditions are independent.
Syntax:
if (condition1)
{
True statement part1;
}
if (condition2)
{
True statement part2;
}
if (condition3)
{
True statement part3;
}
E.g.: - Write a C program to calculate bonus on entered basic salary of an employee. Bonus is to be given according to following dept no.
Dept no Bonus
10 20% of Basic
20 18% of Basic
30 15% of Basic
Solution:
#include<stdio.h>
void main( )
{
float sal;
int dptno;
printf(“Enter a Basic Salary : ”);
scanf(“%f”, &sal);
printf(“Enter a Department Number : ”);
scanf(“%d”, &dptno);
if( dptno == 10 )
{
sal = sal * 1.20 ;
}
if( dptno == 20 )
{
sal = sal * 1.18 ;
}
if( dptno == 30 )
{
sal = sal * 1.15 ;
}
printf(“The Salary is %f ”, sal);