C Conditional Statements

C Language Conditional Statements

Conditional statements:

Conditional statements are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Conditional statements control the sequence of statements, depending on the condition.

There are different types of conditional statements. They are:

•    if statement
•    if else statement
•    else if statement
•    nested if else statement
•    switch statement
•    goto statement

Simple if statement:

This is used to execute a statement or a collection of statements conditionally or you can say that it is used to execute only one action. It is called one way branching. Here the logical condition is tested which results either TRUE or FALSE.

Syntax of if statement is:

 if (condition) 
    {
 statement1;
    }
Where,
Condition is a logical expression that results in TRUE or FALSE.
Statement a simple statement (single statement) or compound statement (collection of two or more statement).
If the logical condition is TRUE then statement1 is executed and if the logical condition is FALSE then control transfers to the next executable statement.

Example:
#include<stdio.h>
int main()
{
int a, b;
printf (“Enter a, b values:”);
scanf (“%d %d”, &a, &b);
if (a>b)
printf (“\n a is greater than b”);
}
Output:
Enter a, b values: 10
5
a is greater than b

If-Else Statement:
This is used to execute two statements alternatively. It is called a two way branching.
The syntax of if-else statement is
 if (condition) 
     {
 statement1;
     }
 else
     {
 statement2;
     }
If the logical condition is TRUE then statement1 is executed and if the logical condition is FALSE then control transfers to the next executable statement that is statement2.

Example:
#include<stdio.h>
main ()
{
int a, b;
printf (“Enter a, b values:”);
scanf (“%d %d”, &a,&b);
if (a>b)
printf (“\n a is greater than b”);
else
printf (“\n b is greater than a”);
}
Output:
Enter a, b values: 10
20
b is greater than a.

Else-if statement:

If we have more than one condition to check, then else-if statement is used.
The syntax of else-if statement is:
if (condition)
  {
    statement1;
  }
else if (condition2)
  {
    statement2;
  }
else
  {
    statement3;
  }

If the logical condition1 is TRUE then statement1 is executed. However, if the logical condition1 is FALSE but the logical condition2 is TRUE then statement2 is executed. If both the conditions are FALSE, then statement3 is executed.

Example:
#include<stdio.h>
main ()
{
int a, b, c;
printf (“Enter a, b, c values :”);
scanf (“%d %d”, &a, &b, &c);
if (a>b)
printf (“\n a is greater than b”);
else if(“b>c)
printf (“\n b is greater than c”);
else
printf (“\n c is greater than a”);
}

Nested If-Else Statement:

It is used if there are more than two alternatives to select.
The syntax of nested-if statement is
if(condition1) 
{
 if(condition2) 
      {
 statement1;
      }
 else
     {
 statement2;
     }
 }
 else
    {
 statement3;
     }
Statement1 is executed if condition1 and condition2 are TRUE. If condition1 is TRUE and condition2 is FALSE then statement2 is executed. If condition1 is FALSE then control transfer to the else part and statement3 is executed.

Example:
#include<stdio.h>
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 (“\n a and b both are +ve and a >b);
else
printf (“\n a is greater than b only”)
else
printf (“ \n a is less than b”);
}
Output:
Enter a and b values: 30
20
a and b both are +ve and a > b

Switch Statement:

It provides a multi way branching. It allows user to select any one of the several alternatives, depending upon the value of an expression. The value of expression enclosed within the parentheses. Depending upon the value of expression, the control is transferred to a particular case and statements executed according to the case value.
The syntax of switch statement is:
switch (expression)
{
case value1;
  statement1;
      break;
case value2;
   statement2;
      break;
case value3;
    statement3;
      break;
case default;
   statement n;
       break;
}
The expression is evaluated first which must either result into an integer or a character constant, and the values(value 1, value 2,…, value n) must be either an integer or character constant.
After evaluation of expression, the evaluated value is matched against value 1, value 2… value n and when an exact match is found the statements connected with that value are executed. But, in case if no match is found, then, the statements connected with default are executed.
It should be noted that the use of both default and break statements are optional. And, the use of break statement in switch case brings the compiler out of the switch case.
 

Example:
#include<stdio.h>
int main ()
{
int a = 2;
switch (a)
{
case 1: printf (“1\n”);
break;
case 2: printf (“2\n”);
break;
default: printf (“No match”)
}
Output:
2

Goto statement:

Goto statement is used for unconditional jump from one part of the program to another part of the program. It is always suggested not to use goto statement as this reduces the readability of the program. Using goto statement is considered as poor programming approach.

Syntax:
goto label;
..
.
label: statement;
The goto statement consists of two parts: label and goto keyword.
Example:
#include<stdio.h>
void main(){
int a;
goto label;
a = 10;
printf(“%d”, a);
label:
a = 20;
printf(“%d”, a);
}
Output:
20
When goto label is encountered, control goes to the statement next to the label. Here, label is not the keyword. We can use any name for the label. Its user defined.

0 comments:

Post a Comment