Decision Making in C

There come situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions we will execute the next block of code.

Decision-making statements in programming languages decide the direction of the flow of program execution. Decision-making statements available in C.

  1. if statement
  • break

1. if statement in C

if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax:

if(condition) 
{
// Statements to execute if
// condition is true
}

Flowchart

Example:

// C program to illustrate If statement
#include <stdio.h>

int main()
{
int i = 10;

if (i > 15)

{
printf(“10 is greater than 15”);
}

printf(“I am Not in if”);
}

Output:

I am Not in if

2. if-else in C

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the C else statement. We can use the else statement with the if statement to execute a block of code when the condition is false.

Syntax:

if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

Flowchart:

Example:

// C program to illustrate If statement
#include <stdio.h>

int main()
{
int i = 20;

if (i < 15)

{

printf(“i is smaller than 15”);
}
else

{

printf(“i is greater than 15”);
}
return 0;
}

Output:

i is greater than 15

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store