Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Conditional Statements In C

Last Updated on April 17, 2023 by Prepbytes

Conditional statements in C are programming constructs that allow a program to execute different blocks of code based on whether a certain condition is true or false. The most common types of conditional statements in C are the if, else if, and else statements.

What is a Conditional Statement In C?

Conditional statement in C programming are used to base choices on the conditions. When there is no condition surrounding the statements, conditional statement in C are executed sequentially. The execution flow may alter if a condition is added to a block of statements depending on the outcome of the condition’s evaluation. In "C", this procedure is known as decision-making.

Conditional statement in C are possible with the use of the following two structures:

  1. If statement
  2. If-else statement

As a program chooses which statement to execute based on the outcome of the evaluated condition, it is also known as branching.

In this tutorial, you will learn-

  • What is a Conditional Statement In C?
  • If Statement
  • Relational Operators
  • The If-Else Statement
  • Conditional Expressions
  • Nested If-else Statements
  • Nested Else-if Statements

If statement

One of the effective conditional statement in C is this one. The if statement is in charge of changing how a program executes. A condition is always used with an if statement. Any statement inside the body of if is first evaluated against the condition. The following is the syntax for an if statement:

 if (condition) 
     instruction;

The result of the condition is either true or false. False is a value that contains zero, and true is always a value that is not zero. A code block surrounded by curly braces { } or a single instruction can both be used as instructions.

Following program illustrates the use of if construct in C programming:

#include<stdio.h>
int main()
{
    int num1=1;
    int num2=2;
    if(num1<num2)
    {
        printf("num1 is smaller than num2");
    }
    return 0;
}

Output

num1 is smaller than num2

Steps for if conditional statement in C
The if construct is used to examine the equality of two numbers in the program above.

  1. In the above program, we have initialized two variables with num1, num2 with value as 1, 2 respectively.
  2. Then, to determine which number is the smallest and which is the largest, we will use if with a test-expression. In the if construct, we used a relational expression. The condition will evaluate to true because the value of num1 is less than the value of num2.
  3. Thus, the statement contained within the If block will be printed. The control will then leave the block at that point, and the program will end successfully.

Relational Operators

For decision-making and condition testing, C contains six relational operators that can be used to create Boolean expressions that return true or false:

  • < less than
  • <= less than or equal to
  • greater than

  • = greater than or equal to

  • == equal to
  • != not equal to

One of the most common concerns a programmer gets into when mixing up the assignment operator (=) and the equal test (==) but actually they are distinct..

For example

int x = 41;
x =x+ 1;
if (x == 42) {
   printf("Number is Changed");}

Output

Number is Changed

Remember that a condition is considered to be true if it evaluates to a non-zero value.

For example

int present = 1;
if (present)
  printf("One Person is Present\n");

Output

One Person is Present

The If-Else statement

The if-else statement is a developed form of the if statement. The following is the if-else general form:

if (test-expression)
{
    True block of statements
}
Else
{
    False block of statements
}
Statements;

If the test-expression value is true in this type of construct, then the true block of statements will be performed. The false block of statements will be executed if the test-value expression is false. Regardless, upon execution, control will be immediately passed to the statements outside the If block.

Following programs illustrate the use of the if-else construct:

In order to determine whether a value is less than or greater than ten, we will create a variable with a value and write a program.

#include<stdio.h>
int main()
{
    int num=19;
    if(num<10)
    {
        printf("The value is less than 10");
    }
    else
    {
        printf("The value is greater than 10");
    }
    return 0;
}

Output

The value is greater than 10

Steps to use if-else conditional statement in C

  1. A variable has been initialized with the value 19. Using a "C" program, we must determine whether the number is greater than or less than 10. We have used the if-else construct to accomplish this.
  2. Because we need to compare this value to 10, we have added the condition num< 10.
  3. As you can see, the first block is always a true block, meaning it will be performed if the test-value expression is true.
  4. An else block makes up the second block. The statements in this block are the ones that will be carried out if the test-value expression is changed to false. Because num in our program is more than ten, the test-condition is false, and the else block is then run. As a result, the else block’s output will be "The value is more than 10." After the if-else statement, the program will end successfully.

Nesting of if-else statements is the technique of using multiple if-else constructs inside of one another in "C" programming.

Conditional Expressions

The ? operator can also be used to create an if-else expression. The ? is used in a conditional expression. There is only one statement connected to the if and else in the operator.

For example

#include <stdio.h>
int main() {
  int y;
  int x = 2;
   y = (x >= 6) ?  6 : x;
   /* This is equivalent to:  if (x >= 5)    y = 5;  else    y = x; */
   printf("y =%d ",y);
  return 0;
}

Output

y =2

Nested If-else Statements

Nested if-else is used when a sequence of decisions is necessary. Utilizing one if-else construct inside another is known as nesting.

Let’s write a program to illustrate the use of nested if-else.

#include<stdio.h>
int main()
{
    int num=1;
    if(num<10)
    {
        if(num==1)
        {
            printf("The value is:%d\n",num);
        }
        else
        {
            printf("The value is greater than 1");
        }
    }
    else
    {
        printf("The value is greater than 10");
    }
    return 0;
}

Output

The value is:1

The program mentioned above uses a nested if-else construct to verify whether a value is less than or larger than 10 and prints the result.

Steps for Nested If-else conditional statement in C

  1. Firstly, we have declared a variable num with value as 1. Then we have used the if-else construct.
  2. In the outer if-else, the condition provided checks if a number is less than 10. If the condition is true then and only then it will execute the inner loop. In this case, the condition is true hence the inner block is processed.
  3. A condition that determines whether or not our variable holds the value 1 is present once more in the inner block. If a condition is satisfied, the If block will be executed; otherwise, the else block will be executed. Since the condition is satisfied in this instance, the If statement is run, and the value is displayed on the output screen.
  4. The aforementioned program will successfully exit after printing the value of a variable.

Try altering a variable’s value to observe how the program behaves.

NOTE: Because several if-else constructs are used in nested if-else, it can be challenging to identify separate constructs, thus we must be careful with the indentation. The program is easier to read when properly indented.

Nested Else-if statements

When multipath decisions are necessary, nested else-if is used.
The following is a broad description of how else-if ladders are built in "C" programming:

 if (test - expression 1) {
    statement1;
} else if (test - expression 2) {
    Statement2;
} else if (test - expression 3) {
    Statement3;
} else if (test - expression n) {
    Statement n;
} else {
    default;
}
Statement x;

The else-if ladder is a particular form of structure. This chain is known as an else-if ladder as it typically has a ladder-like appearance. The test expressions are evaluated in reverse order. A statement linked to a true test expression is performed when it is discovered. The default else statement is run whenever all n test-expressions have been evaluated as false.

Let us see the actual working with the help of a program.

#include<stdio.h>
int main()
{
    int marks=83;
    if(marks>75){
        printf("First class");
    }
    else if(marks>65){
        printf("Second class");
    }
    else if(marks>55){
        printf("Third class");
    }
    else{
        printf("Fourth class");
    }
    return 0;
}

Output

First class

The program mentioned above prints the grade based on the test results. In the program mentioned above, we used the else-if ladder construct.

Steps for else-if conditional statement in C

  1. A variable has been initialized with marks. Our else-if ladder structure includes a number of conditions.
  2. When the first condition is met, the value from the variable marks will be compared with it, and if it is, the statement linked to it will be written on the output screen.
  3. If the first test condition proves to be untrue, the second test condition is then evaluated.
  4. Until all expressions are evaluated, this procedure will continue. If not, control will exit the else-if ladder, and the default statement will be written.

Try changing the value and observe how the output changes.

Conclusion
In this article, we had discussed completely about the conditional statement in C. Depending on the outcome of the evaluated expression, one path is chosen using decision-making or branching statements. Because it manages how a program is executed, it is also known as a control statement. If, if-else constructions are available in "C" for use in decision-making statements. When several pathways need to be evaluated, we can also stack if-else within one another. When we need to verify several ways dependent on the expression’s result, we use the else-if ladder.

Frequently Asked Questions(FAQs)

Q1. What is the syntax for an if conditional statement in C?
Ans: The syntax for an if conditional statement in C is:

if (condition) {
   // code to execute if the condition is true
}

Q2. What is the syntax for an else if conditional statement in C?
Ans: The syntax for an else if conditional statement in C is:

if (condition1) {
   // code to execute if condition1 is true
}
else if (condition2) {
   // code to execute if condition1 is false and condition2 is true
}

Q3. What is the syntax for an else conditional statement in C?
Ans: The syntax for an else conditional statement in C is:

if (condition) {
   // code to execute if the condition is true
}
else {
   // code to execute if the condition is false
}

Q4. Can I use multiple else if statements in a row in C?
Ans: Yes, you can use multiple else if statements in a row in C to check multiple conditions in a sequence.

Q5. How do I use logical operators (&&, ||, !) with conditional statements in C?
Ans: Logical operators can be used to combine multiple conditions in a single if statement or to negate a condition.

For example:

if (condition1 && condition2) {
   // code to execute if both condition1 and condition2 are true
}
if (condition1 || condition2) {
   // code to execute if either condition1 or condition2 is true
}
if (!condition) {
   // code to execute if the condition is false
}

Leave a Reply

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