Dev Frank
5 min readDec 30, 2023

CONTROL STATEMENT IN C (IF STATEMENT)

Dev Frank

In the context of driving, When you approach a traffic light, it evaluates conditions (colors) and determines the appropriate action:

  • If the light is green, you proceed.
  • If the light is yellow, you prepare to stop.
  • If the light is red, you stop.

Here traffic lights act as control statements.

Control statements is also known as DECISION MAKING STATEMENTS.

As the name implies, control statements are statements we use to make decisions on how a program runs or is been executed.

These statements determine which instructions get executed under certain conditions. There are three main types of control statements in C:

  1. Conditional Statements
  2. Looping Statements
  3. Jump Statements

CONDITIONAL STATEMENTS:

Conditional statements allow you to execute different blocks of code based on whether a given condition is true or false.

#include <stdio.h>
/**
* main - Entry of the program
* &a &b - Reads integer inputs from the user and assigns
*
* Description : Asks user for values for a and b and the prints the sum.
* Return : 0 Indicates the successful execution of the program.
**/


int main(void)
{
int a, b, sum;
printf("Enter value for a & b\n");

scanf("%d %d," &a, &b); // Takes input of a and b from user
sum = a + b; // Summation of a and b

printf("Sum = %d\n", sum);

return(0);
}

This program above undergoes sequential flow of execution i.e. one by one statement will be executed.

Suppose you do not want a sequential flow of execution you want to make some conditions, you want to skip statements based on some certain conditions. Assuming i want the sum of the values of a & b to be printed if both numbers are greater than 1, but if they are not, the control should display on the screen “Don’t want to calculate sum”.

at this time you want to control the flow of execution of the program based on some conditions using some kind of statements called CONDITIONAL STATEMENTS.

Using a real life scenario, Imagine you have different outfits for different weather conditions. If it’s sunny, you wear your sunglasses and shorts. If it’s raining, you wear a raincoat and boots.

These can also be done in programs. We can instruct our computer to do some certain things if only the condition is true. If it’s not it should printf some other thing and skip some statements.

The primary conditional statements in C are:

  • if statement
  • else statement
  • Nested if statement
  • else-if statement
  • Switch Statement

If -

It is a decision making statement. If is a single selection statement.

SYNTAX OF IF STATEMENT

if(condition)
// code to be executed if the condition is true

Example

#include <stdio.h>
/**
* main - Entry of the program
* &a - Reads integer input from the user and assigns
*
* Description : Prompts user for input, prints "True Statement" and
"False Statement" if non-zero, If not prints
"False Statement."
* Return : 0 Indicates the successful execution of the program.
**/

int main(void)
{
int a;
printf("Enter value for a\n");

scanf("%d", &a); // Takes input of a from user

if(a)
printf("True Statement\n");
printf("False Statement");

return(0);
}

In the example above, we didn’t pass in a condition to the statement.

N.B - It does not necessarily mean it should be condition . It could be a variable, test expression etc. The variable “a” was passed to the if statement.

If the value of a entered by the user is true (a != 0), then the if statement will be printed (True Statement), together with the statement (False Statement). But if the statement is false (a = 0), then only the statement “False Statement” will be printed.

EXPLANATION OF IF STATEMENT WITH FLOWCHART

Dev Frank
#include <stdio.h>
/**
* main - Entry of the program
* &a - Reads integer input from the user and assigns
*
* Description : Prompts user for input, prints "True Statement" and
"False Statement" if non-zero, If not prints
"False Statement."
* Return : 0 Indicates the successful execution of the program.
**/

int main(void)
{
int a;
printf("Enter value for a\n");

scanf("%d", &a);

if(a)
{
printf("Inside if block\n");
printf("True Statement\n");
}

printf("False Statement");

return(0);
}

In the above updated code, a printf statement was added, then the curly braces ( { } ).

Using curly braces in an if statement, means that you want two or more statements if the condition is true. This does not mean using curly braces for only a single statement is invalid or will give an error, it still works the same way as not using curly braces at all.

To print multiple statements if the condition is true, you have to use curly braces ( { } ). Using curly braces means that the true statements will be printed and false statement will be printed with it.

If the condition is false, the control won’t enter the if block, it will automatically display the statement(s) after the if block.

#include <stdio.h>
/**
* main - Entry of the program
* &a - Reads integer input from the user and assigns
*
* Description : Prompts user for input, prints "True Statement" and
"False Statement" if non-zero, If not prints
"False Statement."
* Return : 0 Indicates the successful execution of the program.
**/

int main(void)
{
int a;
printf("Enter value for a\n");

scanf("%d", &a);

if(a);
{
printf("Inside if block\n");
printf("True Statement\n");
}

printf("False Statement");

return(0);
}

Did you notice any change in the above program. Yes that’s right :) a semi-colon was added to the parenthesis in the if block.
Here, this is a very trickery question given to students in colleges and interviews.

Putting a semi-colon after the an parenthesis in an if block won’t give an error, it gives an output.

Inside if block
True Statement
False Statement

The semi-colon terminates the if statement.

In conclusion, the if statement’ in C programming is a powerful tool for decision-making. Mastering conditional logic enhances program flexibility. Join me as we delve deeper into Conditional, Loops and Jump statements for a comprehensive understanding of control flow in C.

TRY THIS!!!

#include <stdio.h>

int main(void)
{
int age;

printf("Enter you age \n");
scanf("%d", &age);

if(age > 17)
{
printf("You are %d years old\n", age);
printf("Eligible to vote\n");
}
printf("Go home\n");


return(0);

}

Do well to share your answer in the comment section.
Cheers!!!

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.