Skip to main content

C if...else Statement

C if...else Statement

In programming, decision making is used to specify the order in which statements are executed. In this tutorial, you will learn to write if...else statements to make decisions in your program.

C if statement

The syntax of if statement is:
  1. if (testExpression)
  2. {
  3. // statement(s)
  4. }

How if statement works?

The if statement evaluates the test expression inside the parenthesis.
  • If the test expression is evaluated to true (nonzero), statement(s) inside the body of if is executed.
  • If the test expression is evaluated to false (0), statement(s) inside the body of if is skipped from execution.
How if statement works in C programming?
To learn more on when test expression is evaluated to nonzero (true) and 0 (false), check relational and logical operators.

Example 1: if statement

  1. // Program to display a number if user enters negative number
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int number;
  6. printf("Enter an integer: ");
  7. scanf("%d", &number);
  8. // Test expression is true if number is less than 0
  9. if (number < 0)
  10. {
  11. printf("You entered %d.\n", number);
  12. }
  13. printf("The if statement is easy.");
  14. return 0;
  15. }
Output 1
Enter an integer: -2
You entered -2.
The if statement is easy.
When user enters -2, the test expression (number < 0) is evaluated to true. Hence, You entered -2 is displayed on the screen.
Output 2
Enter an integer: 5
The if statement is easy.
When user enters 5, the test expression (number < 0) is evaluated to false and the statement inside the body of if is skipped.

C if...else statement

The if statement may have an optional else block. The syntax of if..else statement is:
if (testExpression) {
    // statement(s) inside the body of if
}
else {
    // statement(s) inside the body of else
}

How if...else statement works?

If test expression is evaluated to true,
  • statement(s) inside the body of if statement is executed
  • statement(s) inside the body of else statement is skipped from execution.
If test expression is evaluated to false,
  • statement(s) inside the body of else statement is executed
  • statement(s) inside the body of if statement is skipped.
How if...else statement works in C programming?

Example 2: if...else statement

  1. // Program to check whether an integer entered by the user is odd or even
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int number;
  6. printf("Enter an integer: ");
  7. scanf("%d",&number);
  8. // True if remainder is 0
  9. if( number%2 == 0 )
  10. printf("%d is an even integer.",number);
  11. else
  12. printf("%d is an odd integer.",number);
  13. return 0;
  14. }
Output
Enter an integer: 7
7 is an odd integer.
When user enters 7, the test expression ( number%2 == 0 ) is evaluated to false. Hence, the statement inside the body of else statement printf("%d is an odd integer"); is executed and the statement inside the body of if is skipped.

if...else Ladder (if...else if....else Statement)

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 for multiple test expressions and execute different statement(s).

Syntax of nested if...else statement.

if (testExpression1) 
{
   // statement(s)
}
else if(testExpression2) 
{
   // statement(s)
}
else if (testExpression 3) 
{
   // statement(s)
}
.
.
else 
{
   // statement(s)
}

Example 3: C if...else Ladder

  1. // Program to relate two integers using =, > or <
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int number1, number2;
  6. printf("Enter two integers: ");
  7. scanf("%d %d", &number1, &number2);
  8. //checks if two integers are equal.
  9. if(number1 == number2)
  10. {
  11. printf("Result: %d = %d",number1,number2);
  12. }
  13. //checks if number1 is greater than number2.
  14. else if (number1 > number2)
  15. {
  16. printf("Result: %d > %d", number1, number2);
  17. }
  18. // if both test expression is false
  19. else
  20. {
  21. printf("Result: %d < %d",number1, number2);
  22. }
  23. return 0;
  24. }
Output
Enter two integers: 12
23
Result: 12 < 23

Nested if...else

It is possible to include if...else statement(s) inside the body of another if...elsestatement.
This program below relates two integers using either <> and = similar like in if...elseladder example. However, we will use nested if...else statement to solve this problem.

Example 4: Nested if...else

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int number1, number2;
  5. printf("Enter two integers: ");
  6. scanf("%d %d", &number1, &number2);
  7. if (number1 >= number2)
  8. {
  9. if (number1 == number2)
  10. {
  11. printf("Result: %d = %d",number1,number2);
  12. }
  13. else
  14. {
  15. printf("Result: %d > %d", number1, number2);
  16. }
  17. }
  18. else
  19. {
  20. printf("Result: %d < %d",number1, number2);
  21. }
  22. return 0;
  23. }

If the body of if...else statement has only one statement, you do not need to use parenthesis { }.
This code
if (a > b) {
    print("Hello");
}
print("Hi");
is equivalent to
if (a > b)
    print("Hello");
print("Hi");

Also Read: C switch statement

Comments

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

C LANGUAGE - OVERVIEW C - VARIABLES

C LANGUAGE - OVERVIEW Advertisements C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute  A N S I A N S ...

environment

It looks like you meant "environment." The environment refers to the surroundings or conditions in which a person, animal, or plant lives or operates. It encompasses various elements, including natural resources, ecosystems, climate, and human-made structures. Protecting and preserving the environment is crucial for the well-being of all living organisms on Earth. Is there anything specific you'd like to discuss or learn about regarding the environment?