In this post I will describe about Integer and Float Conversions, Hierarchy of Operations, Control Instructions in C, if statement, Program Demonstrating if and Relational Operators, Program Demonstrating if and Relational Operators, Flow Demonstrating if and Relational Operators, Nested if-else statements, Forms of if statement, Use of Logical Operators and The ! operator in C programming language.
Integer and Float Conversions:
In order to effectively develop C programs, it will be necessary to understand the rules that are used for the implicit conversion of floating point and integer values in C. These are mentioned below. Note them carefully.
I. An arithmetic operation between an integer and integer always yields an integer result.
II. An operation between a real and real always yields a real result.
III. An operation between an integer and real always yields a real result. In this operation the integer is first promoted to real and then the operation is performed. Hence the result is real.
Hierarchy of Operations:
While executing an arithmetic statement, which has two or more operators, we may have some problems as to how exactly does it get executed. For example, does the expression 2 * x – 3 * y correspond to (2x)-(3y) or to 2(x-3y)? Similarly, does A / B * C correspond to A / (B * C) or to (A / B) * C? To answer these questions satisfactorily, one has to understand the ‘hierarchy’ of operations. The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations. The hierarchy of commonly used operators is shown below:
Priority | Operators | Description |
1st | * / % | Multiplication, Division, Modular Division |
2nd | + – | Addition, Subtraction |
3rd | = | Assignment |
Control Instructions in C:
As the name suggests, the ‘Control Instructions’ enable us to specify the order in which the various instructions in a program are to be executed by the computer. In other words, the control instructions determine the ‘flow of control’ in a program. There are 4 types of Control Instructions in C. They are:
- Sequence Control Instruction.
- Selection or Decision Control Instruction.
- Repetition or Loop Control Instruction.
- Case Control Instruction.
The if statement:
Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:
If ( this condition is true )
Execute this statement;
The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, and then the statement is executed. If the condition is not true, then the statement is not executed, instead the program skips past it. But how do we express the condition itself in C? And how do we evaluate its truth or falsity? As a general rule, we express a condition using C’s ‘relational’ operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other. Here’s how they look and how they are evaluated in C.
This Expression | Is True If |
x == y | x is equal to y. |
x != y | x is not equal to y. |
x <y | x is less than y. |
x > y | x is greater than y. |
x <=y | x is less than or equal to y. |
x >=y | x is greater than or equal to y |
Program Demonstrating if and Relational Operators:
The Relational Operators should be familiar to you except for the equality operator == and the inequality! =. Note that = is used for assignment, whereas, == is used for comparison of two quantities. Here is simple program, which demonstrates the use of if and the Relational Operator.
/* Demonstration of if statement*/
#include<stdio.h>
main( )
{
int num;
printf(“ Enter a number less than 10” )
scanf( “%d”, &num);
if (num < 10)
printf (“ More Journals!!!”);
Program Demonstrating if and Relational Operators:
Flow Demonstrating if and Relational Operators:
Nested if-else statements:
It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’ of ifs. This is shown below:
/* A quick demo of nested if-else*/
#include<stdio.h>
main( )
{
int I;
printf(“Enter either 1 or 2” );
scanf(“%d”, &i);
if(i==1)
printf(“You would go to heaven!”);
else
{
if(i==2)
printf(“Hell was created with you in mind”);
else
printf(“How about Mother Earth”);
}
}
Forms of if statement:
The if statement can take any of the following forms:
- if (condition)
do this;
2. if (condition)
{
do this;
and this;
}
3. if (condition)
do this;
else
do this;
4. if (condition)
{
do this;
and this;
}
else
{
do this;
and this;
}
5. if (condition)
do this;
else
{
if(condition)
do this;
else
{
do this;
and this;
}
}
6. if (condition)
{
If(condition)
do this;
else
{
do this;
and this;
}
}
else
do this;
Use of Logical Operators:
C allows usage of 3 logical operators, namely, &&, ll and !. These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively.
There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: && and ll. Don’t use the single symbol I and &. These single symbols also have a meaning. They are bitwise operators.
The first two operators, && and ll, allow two or more conditions to be combined in an if statement. Let us see how they are used in a program.
Example: The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules:
Percentage above or equal to 60- First Division.
Percentage between 50 and 59- Second Division.
Percentage between 40 and 49- Third Division.
Percentage below 40- Fail.
Write a program to calculate the division obtained by the student.
/*Use of Logical Operators*/
#include<stdio.h>
main( )
{
int m1, m2, m3, m4, m5, per;
printf(“Enter marks in five subjects);
scanf(“%d %d %d %d %d”, &m1, &m2, &m3, &m4, &m5);
per=(m1+ m2+ m3+ m4+ m5) / 500 * 100;
if(per>=60)
printf(“First Division”);
else
{
if(per>=50)
printf(“Second Division”);
else
{
if(per>=40)
printf(“Third Division”);
else
printf(“Fail’);
}
}
}
The ! operator:
So far we have used only the logical operators && and ll. The third logical operator is the NOT operator, written as !. This operator reverses the result of the expression it operates on. For example if the expression evaluates to a non-zero value, then applying! Operator to it results into a 0. Vice versa, if the expression evaluates to zero then on applying! Operator to it makes it 1, a non-zero value. The final result (after applying!) 0 or 1 is considered to be false or true respectively.
Ex. !( y<10 )
This means “not y is less than 10”. In other words, if y Is less than 10, the expression will be false, since ( y < 10 ) is true. We can express the same condition as ( y >= 10 ).
The NOT operator is often used to reverse the logical value of a single variable, as in the expression
if( ! flag )
This is another way of saying
if( flag == 0 )
If it makes you uncomfortable, avoid it, as the same result can be achieved without using it.