C language Conditional statements

Interview Questions on C language Conditional statements

1. What is a conditional statement?

Conditional Statement (also called Condition) is a feature of programming language , which allows it to perform actions depending upon some conditions provided by the programmer. Conditional statement controls the sequence of statements depending on the condition.

2. What are the different types of conditional statements?
 

The different types of conditional statements in C language are as follows:
1.    if statement
2.    if else statement
3.    else if statement
4.    nested if-else statement
5.    switch statement
6.    goto statement

3. Explain about simple If statement?

The if statement gives the user the choice of executing a statement (possibly compound) if the expression is evaluated to true or skipping it is the expression is evaluated to false.

Syntax:
An if statement in C is generally in the following form:
if (expression)
{
 Statement;
}
The statement is executed if and only if the expression is true.

4. Give me example for simple If statement?

Example:

if  (num > 20)
    {
      result = 2 * num;
    }
The content of num is multiply by 2 if and only if the value of num is greater than 20.

5. What is use of if-else statement?
C language also lets one choose between two statements by using the if-else structure.

Syntax:
if (expression)
{
Statement 1
}
Else
{
Statement 2
}
In this case, if the expression is true, then the statement 1 is executed.  Otherwise, statement 2 is executed.

6. Write a program to demonstrate if-else statement?

Example:
  
if  (num > 20)
    {
        result = 2 * num;
    }
else
    {
        result = 3* num;
    }
In the above example, if the num is greater than 20 then the result is equal to the num multiplied by 2 otherwise it is equal to the num multiplied by 3.

7. What is the use of ‘Else if’ statement?

The ‘else if’ statement in C is generally used when we need to compare more than one condition.

Syntax:

if (expression)
{
Statement1
}
Else if (expression2)
{
Statement 2
}
Else
{
Statement 3
}

The else if block is executed if all the preceding conditions failed and the condition provided is true.
In the above case, if expression 1 is evaluated to true then statement 1 is executed.  However, if expression 1 is false but expression 2 is true then statement 2 is executed.  In the case, when both expressions are false then statement 3 is executed.

8. Explain “else if“with an example?

Example:
 if  (num > 20)
    {
        result = 2 * num;
    }
 else if (num > 10)
    {
        result = 3 * num;
    }
else
    {
        result =  4* num;
    }
If the value of num is greater than 20, then result is equal to the num multiplied by 2. However, if the num is greater than 10 but less than 20 then the result is equal to the value of num multiplied by 3, otherwise, the result is the num is multiplied by 4.

9. What is the use of Nested If-Else Statement?

It is a conditional statement which is used when we want to check more than 1 condition at a time in a same program. The conditions are executed from top to bottom checking each condition whether it meets the conditional criteria or not. If it found the condition is true then it executes the block of associated statements of true part else it goes to next condition to execute.
Syntax:

if (condition)
{
    if (condition)
    {
      Statements;
     }
   else
   {
   Statements;
    }
}
else
{
Statements;
}

In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the braces and again checks the next condition. If it is true then it executes the block of statements associated with it else executes else part.

10. Write a program to demonstrate nested if-else statement?

Example:
main()
{
int a,b;
printf(“\n Enter a and b values:”);
scanf(“%d %d ”,&a,&b);
if(a>b)
if((a!=0) && (b!=0))
printf(“\na and b both are +ve and a >b);
else
printf(“\n a is greater than b only”)
else
printf(“ \na is less than b”);
}

11. Explain about “switch” statement?

This is a multiple or multiway brancing decision making statement. When we use nested if-else statement to check more than 1 conditions then the complexity of a program increases in case of a lot of conditions. Thus, the program is difficult to read and maintain. So to overcome this problem, C provides 'switch case'. Switch case checks the value of a expression against a case values, if condition matches the case values then the control is transferred to that point.

Syntax:
switch (expression)
{
  case label1:
    statements;
  case label2:
    statements;
    break;

  case label:
    statements;
    break;
  default:
    statements;
}
In above syntax, switch, case, break are keywords. label1, label2 are known as 'case labels.' Statements inside case expression need not to be closed in braces. Break statement causes an exit from switch statement. Default case is optional case. When neither any match found, it executes.

12. Write a program to demonstrate switch case statement?

#include <stdio.h>
#include <conio.h>
void main()
{
    int no;
    clrscr();
    printf("\n Enter any number from 1 to 3 :");
    scanf("%d",&no);
    switch(no)
    {
        case 1:
            printf("\n\n It is 1 !");
            break;
        case 2:
            printf("\n\n It is 2 !");
            break;
        case 3:
            printf("\n\n It is 3 !");
            break;
        default:
            printf("\n\n Invalid number !");
    }
    getch();
}

13. Explain about goto statement in C?

In C language, goto is an unconditional jump statement. This is a dangerous construct that can lead to highly confusing code.
Syntax:
goto label:

14. Write a program to demonstrate goto statement?

#include <stdio.h>
#include <conio.h>
void main()
{
    int i=1, j;
    clrscr();
    while(i<=3)
    {
        for(j=1; j<=3; j++)
        {
            printf(" * ");
            if(j==2)
            goto stop;
        }
        i = i + 1;
    }
    stop:
        printf("\n\n Exited !");
    getch();
}

15. What are the rules for declaring switch case?

•    The case label should be integer or character constant.
•    Each compound statement of a switch case should contain break statement to exit from case.
•    Case labels must end with (:) colon.

16. What are the advantages of switch case?

•    It is easy to use.
•    It is easy to find out errors.
•    Debugging is made easy in switch case.
•    Complexity of a program is minimized.

17. What are the advantages of Conditional Statements?
Advantages of Conditional Statements:
A conditional statement has many benefits. It can organize a program into logical segments of code that run only if certain conditions are met. It also makes a program more robust by allowing only a portion of code to run if a condition has been met. Often in code, there is a statement that should run only under certain conditions; if statement allows this to happen.

----------------------------
C Language Interview Questions

Interview Questions on C Language Overview

Interview Questions on C Language Data Types

Interview Questions on C Language Variables
 

Interview Questions on C Language Constants

Interview Questions on C Language Operators

Interview Questions on C Language Loop Statements

Interview Questions on C Language Built-in Functions

Interview Questions on C Language User defined Functions

Interview Questions on C Language Pointers

Working with Files

Working with Databases

Interview Questions on C Data Structures

Comments

Programming Examples 

0 comments:

Post a Comment