5.1 – Control Flow Statements in C language

by subbu on September 13, 2013

Need of Conditional Execution:

All the programs written so far are sequential programs; execute statement after statement in written order from the beginning of main() to the end of main(). But we can’t solve every problem just by writing a sequence of statements.
While developing complicated and sophisticated programs, we need to conditionally execute the program.
Some times a statement or multiple statements need to be executed, in other time the same thing has to skip from the execution. Some times we need to execute a statement or a block of statements repeatedly until a condition is attained. In some other case, need to jump from one part of program to other part.

Example:

#include<stdio.h>
int main()
{
int n;
printf("Enter any integer:");
scanf("%d",&n);
printf("Zero");
printf("\nNon-zero");
return 0;
}

Execution 1:
Enter any integer: 12
Zero
Non-zero

Execution 2:
Enter any integer: 0
Zero
Non-zero

The main intention in writing the above program is to take any integer as input and print whether the number is a zero or non-zero.
But the program is giving the same output to any input because the program is not conditionally executing.
Actually the 7th statement must be executed and the 8th statement must be skipped when the input is 0 (zero). The 7th statement must be skipped and the 8th statement must be executed when the input is any thing other than 0 (non-zero). The summery is that it needs conditional execution.

Control flow instructions/statements in C language:

Fortunately, C language provides number of control flow instructions/statements to control the flow of program execution conditionally. These are classified as

  • Decision making control structures (Selection statements)
  • Iteration or Repetitive control structures (Iteration statements)
  • Jump statements

empty line

Control flow statements in C language

How to write a condition:

Both the Decision and iterative control structures control the program execution according to the condition we supply to the conditional statement. Here a condition is either a relational or logical expression that returns either true (1) or false (0). We must first learn how to write a condition before stepping into control flow statements.

Relational Operators in C language:

These operators express the relation among any two operands as either true or false. The following are different relational operators provided by the C language.

Operator Meaning
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not Equal to

Equal to (==) operator in C language:

In C language == is different from =.
= is an assigning operator helps to assign a value or the result of an expression to the variable.
In other hand == is an equal to operator used to compare any two values.

int x;
x=45;

Here the value 45 is assigning to the variable x.

Assigning a value to the variable

int x=65;
if(x==50)
printf("Equal");

Here the value of x 65 is comparing with 50.

Difference between == and = in C language

Relational Expression:

Relational expression is a combination of constants, variable and relational operators. The result of any relational expression is either 1 (true) or 0 (false). Associativity and precedence of relational operators comparatively with arithmetic operators is as follows.

Operator Associativity
() left to right
* / % left to right
+ – left to right
< > <= >= left to right
== != left to right
= right to left

Empty line

We will see the behavior of relational operators through different example programs.

Example:

#include<stdio.h>
int main()
{
int a,b,c,d;
a=40<20;/*assigns 0 (false) to a*/
b=40!=20;/*assigns 1 (true) to b*/
c=a!=b;/*assigns 1 (true) to c */
d=b==c;/*assigns 1 (true) to d*/
printf("%d\n%d\n%d\n%d",a,b,c,d);
return 0;
}

Output:
0
1
1
1


Example:

#include<stdio.h>
int main()
{
int a=45,b=30,c=30,d;
d=(b==c!=a);
printf("%d",d);
return 0;
}

Output:
1

As the associativity of relational operators is left to right, b==c results 1, 1!=a results 1 and that will be assigned to d.
associativity of relational operators


Example:

#include<stdio.h>
int main()
{
int a=10,b=20,c=30,d;
d=c==a+b;
printf("%d",d);
return 0;
}

Output:
1

As the precedence of + is more than ==, a+b results 30, c==30 results 1 and that will be assigned to d.
Priority of relational operators comparatively with arithmetic operators


Example:

#include<stdio.h>
int main()
{
int a=10,b=20,c=0,d;
d=c==a<b;
printf("%d",d);
return 0;
}

Output:
0

As the associativity of relational operators is left to right priority of < is more than ==, a<b results 1, c==1 results 0 and that will be assigned to d
Associativity of relational operators
Knowledge gained so far regarding to the relational operators is enough to write simple conditions to start up with control flow statements.

Previous post:

Next post: