0% found this document useful (0 votes)
12 views

C If Statement

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C If Statement

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

C if Statement

The syntax of the if statement in C programming is:

if (test expression)
{
// code
}

How if statement works?

The if statement evaluates the test expression inside the parenthesis () .

 If the test expression is evaluated to true, statements inside the body


of if are executed.
 If the test expression is evaluated to false, statements inside the body
of if are not executed.
Example 1: if statement

// Program to display a number if it is negative

#include <stdio.h>
int main()
{
int number;

printf("Enter an integer: ");


scanf("%d", &number);

// true if number is less than 0


if (number < 0)
{
printf("You entered %d.\n", number);
}
return 0;
}

C if...else Statement

The if statement may have an optional else block. The syntax of


the if..else statement is:

if (test expression) {
// run code if test expression is true
}
else {
// run code if test expression is false
}

How if...else statement works?

If the test expression is evaluated to true,

 statements inside the body of if are executed.


 statements inside the body of else are skipped from execution.
If the test expression is evaluated to false,

 statements inside the body of else are executed


 statements inside the body of if are skipped from execution.

Example 2: if...else statement

// Check whether an integer is odd or even

#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);

// True if the remainder is 0

if (number%2 == 0)
{
printf("%d is an even integer.",number);
}
else
{
printf("%d is an odd integer.",number);
}

return 0;
}

C if...else Ladder

The if...else statement executes two different codes depending upon


whether the test expression is true or false. Sometimes, a choice has to be
made from more than 2 possibilities.
The if...else ladder allows you to check between multiple test expressions
and execute different statements.

Syntax of if...else Ladder

if (test expression1)
{
// statement(s)
}
else if(test expression2)
{
// statement(s)
}
else if (test expression3)
{
// statement(s)
}
.
.
else {
// statement(s)
}

Example 3: C if...else Ladder

// Program to relate two integers using =, > or < symbol

#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

//checks if the two integers are equal.


if(number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}

//checks if number1 is greater than number2.


else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2);
}

//checks if both test expressions are false


else
{
printf("Result: %d < %d",number1, number2);
}

return 0;
}

You might also like