The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C if statement

Why if Statement?

All programming languages enable you to make decisions. They enable the program to follow a certain course of action depending on whether a particular condition is met. This is what gives programming language their intelligence.

if statement in C

C if Syntax And Definition

  • C uses the keyword if to execute a set of statements when logical condition is true.
  • When the logical condition is false, the compiler simply skips the statement within the block.
  • The "if" statement is also known as one way decision statement.

Syntax if statement

Syntax
if(condition)
{
here goes statements;
.
.
.
.
here goes statements;
}

if Statement Uses

if statements are commonly used in following scenarios

  • Is A bigger than B?
  • Is X equal to Y?
  • Is M not equal to N?

Realtime Time if Statement Uses

if statement in real time

Arduino microcontroller make use C programming, where you need to blink a warning light(red light) when certain condition met. Example program is as below:

realtimeif
if(x = 123)
digitalWrite(LEDpin, high)

Note:

x is a variable which get its input from a sensor continuously.

if Statement Rules

  • The expression (or) condition is always enclosed within pair of parenthesis. e.g.) if ( a > b )
  • The if statement should not be terminated with a semicolon. If it happens, the block of statements will not execute even the condition is true.
  • The statements following the if condition is normally enclosed between 2 braces (in curly braces).

if statement Flow Chart

Following flow chart will clearly explain how if statement works

if statement flow chart

if statement C Program

ifstatement.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
int age = 18;
if(age > 17){
printf("you are eligible for voting ");
}
printf("\nThis is normal flow ");
return 0;
}
  • you are eligible for voting
  • This is normal flow

Note:

If the user input is greater than 17, then the condition will be true and the statements under if condition gets executed. Otherwise, it executes next to the if block.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report