• Home
  • About
  • Contact Us

More Scientific

  • Computer Networks
  • Operating Systems
  • Programming Languages
  • Project Management
  • Software Development
  • Software Testing

C Programming: Relational, Logical Operators, If Statement

April 21, 2019 by admin

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:

PriorityOperatorsDescription
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:

  1. Sequence Control Instruction.
  2. Selection or Decision Control Instruction.
  3. Repetition or Loop Control Instruction.
  4. 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 ExpressionIs True If
x == yx is equal to y.
x != yx is not equal to y.
x <yx is less than y.
x > yx is greater than y.
x <=yx is less than or equal to y.
x >=yx 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:

Program Demonstrating if and Relational Operators

Flow 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:

  1. 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.

Filed Under: Programming Languages Tagged With: arithmetic, assignment, chart, code, coding, comparison, condition, control, conversions, decision, execute, float, flow, forms, hierarchy, if else, instructions, integer, language, loop, nested, nesting, operations, program, real, repetition, results, statements

Project Management

Importance Of Planning In Project Management

Planning means “Deciding what has to done, Who has to do, when it has to done, how it to be done, Where it has be done. Planning is an intellectual process, which requires anyone to think before acting. To plan one would need objective, goal, way to perform and means of to perform. Planning would […]

Operating Systems

C Programming Under Linux – Zombies And Orphans Process

What is Linux? Linux operating system is clone of Unix operating system. Kernel of Linux was developed from scratch by Linus Torvalds with assistance from a loosely-knit team of programmers across the world on internet. It has got all the features that would be expected in a modern operating system. Unlike windows or Unix, Linux […]

Web Development

Website Testing Methods: Functionality, Usability, Interface

In this post, I will describe major web testing methods which are useful to test a website: Functionality Testing. Usability Testing. Interface Testing. Compatibility Testing. Performance Testing. Security Testing. 1) Functionality Testing Functionality testing involves testing of all the links shown are functional. Navigating to target pages. Test links will remain in the same page. Detail […]

Data Modeling

How To Identify Entity Type In Database Designing

In this post, I will discuss about how to identify Entity Type in Data Base Designing, Property of … [Read More...]

  • Software Development
  • Programming Languages
  • Project Management
  • Operating Systems
  • Computer Networks
  • Software Testing

Copyright © 2025 More Scientific