1 of 9

If statement(conditional statement)

Presented by MINAKSHI SHARMA

2 of 9

What is an if Statement?

  • A way to make decisions in a program.
  • Checks a condition: if true, do something.

​

Use if Statements

  • To control the flow of a program.
  • Example:
    • If a user enters the wrong password, show an error.
    • Checks entered no is even or odd.

​

if statements allow you to make decisions in your program by executing code only when a specific condition is true.

3 of 9

Type of if

  • Simple if
  • If else
  • Nested if
  • Ladder if (else-if)

4 of 9

Simple if Syntax

  • if (condition)

{

// Code to execute if the condition is true

}

​

  • The condition is written inside () brackets.
  • Code runs only if the condition is true.
  • Condition is checked.
  • If true, the block of code runs.
  • If false, the block is skipped.

Example:-

int x = 10;

if (x > 5)

{

printf("x is greater than 5!");

}

5 of 9

If else Syntax

  • if (condition)

{

// Code if true

}

else

{

// Code if false

}

int x = 3;

if (x > 5)

{

printf("x is greater than 5!");

}

else

{

printf("x is 5 or less.");

}

Example

6 of 9

Nested If(if within if)�Syntax

if (num > 5)

{

// Outer condition

if (num % 2 == 0)

{ // Inner condition

printf("The number is greater than 5 and even.\n");

}

}

Example

​

  • A type of if statement placed inside another if statement.
  • Allows you to check conditions within conditions.

if (condition1)

{

if (condition2)

{

// Code to execute if both conditions are true

}

}

​

  • The inner if runs only if the outer if condition is true.
  • Braces {} are important to define code blocks.

7 of 9

ladder If Syntax

Example

if (condition1)

{

// Code if condition1 is true

}

else if (condition2)

{ // Code if condition2 is true

}

else

{ // Code if none are true

}

int x = 7;

if (x > 10)

{

printf("x is greater than 10");

}

else if (x > 5)

{ printf("x is between 6 and 10");

}

else { printf("x is 5 or less");

}

8 of 9

Greatest between three nos

Using Nested if

Using Ladder if

#include <stdio.h>

int main()

{

int a = 10, b = 20, c = 15;

if (a > b && a > c)

{

printf("a (%d) is the greatest.\n", a);

}

else if (b > c)

{

printf("b (%d) is the greatest.\n", b);

}

else { printf("c (%d) is the greatest.\n", c); }

return 0;

}

​

#include <stdio.h>

int main()

{

int a = 10, b = 20, c = 15;

if (a > b)

{ if (a > c)

{

printf("a (%d) is the greatest.\n", a);

}

else

{

printf("c (%d) is the greatest.\n", c);

}

}

else

{

if (b > c)

{

printf("b (%d) is the greatest.\n", b);

}

else

{

printf("c (%d) is the greatest.\n", c);

}

}

return 0;

}

9 of 9

thanks