Skip to content

Decision Making In C

What Is Decision Making In C

A Decision making program involves a decision of the statement(s) to be run supported bound conditions or to repeat a gaggle of statements to make sure that certain conditions are fulfilled. These are also known as Conditional statements. C language controls decision making programs by supporting the subsequent statements. The most conventional decision making structure found in maximum programming languages is shown below:

Any non-zero and non-null value is considered true in the C programming language and likewise, zero or null value is considered false value.

What is the need for Decision Making programs in C?

In real-life scenarios, sometimes we need to make some decisions and decide what to do next based on them. Similar situations will appear in programming. When we need to make some decisions and based on it, perform the following operations and based on these decisions run the given code block. This is why we need decision making statements in C.

The C language promotes decision making program by supporting the following decision making statements:

if statement In C

It contains a boolean expression followed by one or more statements.These decision making statements control the flow of the program most simply provided it is applied to run a code block statement if the resultant comes out to be true.

The basic format of if statement is:

if(test_expression)

{

statement 1;

statement 2;

}

if-else statement

These decision making statements also control the flow of the program provided if the boolean expression comes out to be false then an if statement can be followed by an optional else statement.

The basic format of if statement is:

if (condition)

{

// Executes this block if

// condition is true

}

else

{

// Executes this block if

// condition is false

}

Nested if-else statement

Nested if decision making statements in C targets another C statement. It means that one conditional statement can be used under another conditional statement.

The basic format of if statement is:

if(test_expression one)

{

if(test_expression two) {

//Statement block Executes when the boolean test expression two is true.

}

}

else

{

//else statement block

}

If else If ladder:

In C if else if Ladder helps the user to choose between several options. C if statements are run from top-down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed and the rest of the else if ladder in C is skipped. If none of the conditions apply, the last else statement is executed.

The basic format of if statement is:

statement 1;

else if (condition)

statement 2;

.

.

else

statement;

Switch Statement

The type of control decision making statements that allows you to choose one among the plethora of the choices given is a Switch Statement. The Switch expression executes itself and returns an integral value which is then compared with other values in different cases.

The basic format of if statement is:

switch(variable)

{

case 1:

//execute your code

break;

case n:

//execute your code

break;

default:

//execute your code

break;

}

It is very important to insert a break statement otherwise all the consecutive code blocks will be run downwards after matching case block.

Nested switch Statements

The decision making statements which allow us to use one switch statement under another switch statement(s) is called nested switch statements.

The basic format of if statement is:

switch(ch1) {

case ‘A’:

printf(“This A is part of outer switch” );

switch(ch2) {

case ‘A’:

printf(“This A is part of inner switch” );

break;

case ‘B’: /* case code */

}

break;

case ‘B’: /* case code */

}

Ternary Operator:

This conditional operator is very much similar to if-else decision making statements as both follow the same set of instructions. The only difference is that a conditional operator takes up less space and helps in writing if-else statements in the simplest way possible

The basic format of if statement is:

The conditional operator is of the form

variable = Expression1 ? Expression2 : Expression3

It can be visualized into if-else statement as:

if(Expression1)

{

variable = Expression2;

}

else

{

variable = Expression3;

}

Conclusion:

In this blog post, we’ve covered the basics of decision making in C programming. If you’re not sure where to start with your next project, try checking out our list! We hope that these tips will help get you started on the right track and make coding a little less intimidating for beginners. What are some other topics or questions about coding that have been keeping you up at night? Let us know in the comments below so we can provide an answer as soon as possible!

nv-author-image

Vaibhav Kapoor

A professional web entrepreneur, WordPress developer and digital marketing strategist with more than 4 years of experience in building a business from scratch. Knowledgeable about SEO, blogging, internet marketing, social media and website development.

Leave a Reply

Your email address will not be published. Required fields are marked *

[wpfepp_submission_form form="1"]