C Programming

Decision Control In C

Decision Control is the basic concept of C language. It actually helps the compiler to take the decision on certain conditions that appear in the program. If- else are the keyword by which decision is controlled by the compiler. Now we discuss in detail decision control.

Control

main ()

{

line 1 ;

line 2 ;

line 3 ;

………..

………..

line 6 ;

}

Explanation

These Lines are called instructions. Instruction is read by the processor. It means the control goes to the line by line. At first, line 1 is executed . after line 1 is executed, control goes to line 2. This execution of control is continuous or in sequence. But if we want to execute, after executing line 1 and 2, and skip lines 3 and 4 and want to execute lines 4 and 6, we apply a process.

Types Of Control Instruction

  1. Decision Control Instruction
  2. Iterative Control Instruction
  3. Switch case Control Instruction
  4. Go to Control Instruction

Decision Control Instruction

It is also called selective control instruction. They are 3 types.

1. if

2. else

3. conditional operator

1. If

Programming Example 1

#include<stdio.h>

int main ()

{

printf ( " hello " ) ;

printf ( " Bye " ) ;

return 0 ;

}

Output

Explanation

If we want to skip the line printf ( “ bye “ ) ; we use decision control instruction – if . It is a keyword. if ( condition ) -> condition is checked .

Programming Example 2

#include<stdio.h>

int main ()

{

int x ;

printf ( " Enter a number " ) ;

scanf ("%d" , &x ) ;

if ( x> 0 )

{

printf ( " positive Number " ) ;

}

if ( x< 0 )

{

printf ( " Negative Number " ) ;

}

return 0 ;

}

Output

Explanation

If ( x > 0 ) then only block of If ( x > 0 ) is executed and show the result, and If ( x < 0 ) block is skipped and if ( x < 0 ), then the block of (x < 0 ) is only executed and show the result. This is how we can stop the execution of line by line.

2. If Else

If ( condition )

{

line 1 ;

line 2 ;

……….

……….

}

else

{

line 1 ;

line 2 ;

……….

……….

}

Explanation

In this case, if (condition) is true, then lines of if block only executed, then blocks of else are not executed.

And if (condition) is false, thin lines of else block is only executed, then block of if is not executed.

That means both blocks of if and else are not executed parallelly. It is based on which condition is true that a particular code of block is only executed.

Programming Example 3

#include<stdio.h>

int main ()

{

int x ;

printf ( " Enter a number " ) ;

scanf ("%d", &x ) ;

if ( x > 0 )

{

printf ( " positive Number " ) ;

}

else

{

printf ( " Negative Number " ) ;

}

return 0 ;

}

Output

Explanation

There are two types of statements in this program,

1 .Declaration statement ( int n ) ;

2 .Action statement ( return 0 🙂

if-else reduce the no of action statement in the program if else if completely one action statement.

If in the block of the if statement contains only one instruction, in that case, there is no need of {} curly bracket.

3. Conditional Operator ( ? : )

This is also called the ternary operator. The formation of a conditional operator is-

Condition ? statement : statement

Programming Example 4

#include<stdio.h>

int main (){

int x ;

printf ( " Enter a number " ) ;

scanf("%d",&x);

x > 0 ? printf ( " positive Number " ) : printf ( " Negative Number " ) ;

return 0 ;

}

Output

Explanation

In the ternary operator, there is no requirement of ( ) in place of condition. We write only conditions, then ? after that statement : then after that another statement.

Another usage of the conditional operator is selective Assignment.

Selective Assignment

x = a > b ? a : b ;

We have to assign max no between a & b in x if a > b, this condition is true, then a is selected. If a > b, this condition false, then b is selected, which is selected between a & b, which is assigned in x.

Programming Example 5

#include<stdio.h>

int main ()

{

int x , y , max ;

printf ( " Enter two numbers " ) ;

scanf ( "%d%d",&x, &y ) ;

max = x >y ? x : y ;

printf ( " Greatest no is %d ", max ) ;

return 0 ;

}

Output

Explanation

Here we use the ternary operator as a selective assignment to show; at first, it is checked which variable between x &y is greater by using the condition ( x > y ) If x > y , this condition is true, x is selected and assigned to the max.

If x > y, this condition is false, y is selected and assigned to the max.

Then print the max variable to show the maximum.

Programming Example 6

#include<stdio.h>

int main ()

{

int x , y ;

printf ( " Enter two numbers " ) ;

scanf( "%d%d ",&x, &y ) ;

printf ( " Greatest no is %d ", x > y ? x : y ) ;

return 0 ;

}

Output

Explanation

We have done this program without assigning x or y to another variable. We just use the printf statement & print the ternary operator.

Conclusion

For the above discussion about the concept of Decision Control, we have come to the conclusion that Decision Control is an important concept in C language. As the compiler takes decisions with the help of a decision control statement, for this, C language is also called the procedural language.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com