if Statement – Flow of Control

Simple if Statement – So far in our journey of learning C programming, we learned to write down simple C programs, that execute within the sequence we write. A program that consistently executes in the same succession is useless. The power to regulate program flow and statements execution is worth it.

if statement allows us to pick an action supported some condition. It gives the programmer to require control over a bit of code. The programmer can control the execution of code supported by some condition or user input. for instance – if the user inputs valid account number and pin, then allow money withdrawal.

On the off chance that announcement works like “In the event that condition is met, at that point execute the undertaking”. it’s wont to compare things and take some action supported the comparison. Relational and logical operators support this comparison.

If statement perform action supported boolean expression true or false.

What is a Boolean expression?

A C-expression that evaluates either true or false is understood as a Boolean expression. However, in C programming there’s no concept of true or false value.

In C we speak to valid with a non-zero whole number and bogus with zero. Hence, in C if an expression evaluates to integer is taken into account as a Boolean expression.

Syntax of if statement

if(boolean_expression)
{
 // body of if
}

In the above linguistic structure in the event that boolean articulation assesses valid, at that point proclamations inside if body executes in any case skipped.

To learn about flow charts — Click Here

Flowchart of if statement

if Statement - Flow of Control

Example program of if statement

Let us write our first program supported conditions. Write a program to input user age and check if he’s eligible to choose India or not. an individual in India is eligible to vote if he’s 18+.

/**
 * C program to see if an individual is eligible to vote or not.
 */

#include 

int main()
{
 /* Variable declaration to store age */
 int age;

 /* Input age from user */
 printf("Enter your age: ");
 scanf("%d", &age);

 /* Use relational operator to see age */
 if(age >= 18)
 {
 /* If age is bigger than or equal 18 years */
 printf("You are eligible to choose India.");
 }

 return 0;
}
Output:
Enter your age: 24
You are eligible to choose India.

Single vs compound statement inside if body:

If there’s only a single statement inside if body, then braces { } are optional. However, braces after if statement is mandatory when the body of if contains quite one statement.

So, you’ll write an if condition in two ways.

if(boolean_expression)
 // Single statement inside if
if(boolean_expression)
{
 // Statement 1
 // Statement 2
 ...
 ...
 // Statement n
}

There may happen situations once you want to perform a single task inside if. for instance – If it’s Saturday or Sunday, then the employee shouldn’t log in today. If a student got quite 80 marks, then he passed with distinction. In such situations, you’ll ignore braces {}.

Learn more at Teche Programmer — Click Here

Leave a Reply

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