HOME      |      HTML      |      PHP      |      DREAMWEAVER      |      CSS      |      C      |

Conditional Statements in C :


Conditional statements are used to execute a statement or a group of statement based on certain conditions.

We will look into following conditional statements.

  1. if
  2. if else
  3. else if
  4. switch
  5. goto

1) if conditional statement in C :

Syntax for if statement in C :

if(condition)
{
Valid C Statements;
}

If the condition is true the statements inside the parenthesis { }, will be executed, else the control will be transferred to the next statement after if.

if.c

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
a=10;
b=5;
if(a>b)
{
printf("a is greater");
}
getch();
}

Program Algorithm / Explanation :

  1. #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
  2. #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.
  3. main () function is the place where C program execution begins.
  4. Two variables a&b of type int is declared.
  5. Variable a is assigned a value of 10 and variable b with 5.
  6. A if condition is used to check whether a is greater than b. if(a>b)
  7. As a is greater than b the printf inside the if { } is executed with a message “a is greater”.

Output :

if statement in c

2) if else in C :

Syntax for if :

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

In if else if the condition is true the statements between if and else is executed. If it is false the statement after else is executed.

Sample Program :

if_else.c

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter a value for a:");
scanf("%d",&a);
printf("\nEnter a value for b:");
scanf("%d",&b);

if(a>b)
{
printf("\n a got greater value");
}
else
{
printf("\n b got greater value");
}
printf("\n Press any key to close the Application"); 
getch();
}

Program Algorithm / Explanation :

  1. #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
  2. #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.
  3. main () function is the place where C program execution begins.
  4. Two integer variable a and b are declared.
  5. User Input values are received for both a and b using scanf.
  6. An if else conditional statement is used to check whether a is greater than b.
  7. If a is greater than b, the message “a got greater value” is displayed using printf.
  8. If b is greater the message “b got greater value” is displayed.

Output :

if else in C

3) else if in C :

Syntax :

if(condition)
{
Valid C Statements;
}
else if(condition 1)
{
Valid C Statements;
}
-
-
else if(condition n)
{
Valid C Statements;
}
else
{
Valid C Statements;
}

In else if, if the condition is true the statements between if and else if is executed. If it is false the condition in else if is checked and if it is true it will be executed. If none of the condition is true the statement under else is executed.

else_if.c

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter a value for a:");
scanf("%d",&a);
printf("\nEnter a value for b:");
scanf("%d",&b);
if(a>b)
{
printf("\n a is greater than b");
}
else if(b>a)
{
printf("\n b is greater than a");
}
else
{
printf("\n Both a and b are equal");
}
printf("\n Press any key to close the application");
getch();
}

Program Algorithm / Explanation :

  1. #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
  2. #include<conio.h> is used because the C in-built function getch comes under conio.h header files.
  3. main () function is the place where C program execution begins.
  4. Two variables a & b of type int are declared.
  5. Values for a & b are received from user through scanf.
  6. if condition is used to check whether a is greater than b. if(a>b)

If the condition is true the statement between if and else if is executed.

  1. If the condition is not satisfied else if() condition is checked. Else if(b>a)

If b is greater than a the statement between else if and else is executed.

  1. If both the conditions are false the statement after else is executed.

Output :

else if in C

4) Switch statement in C :

Syntax :

switch(variable)
{
case 1:
Valid C Statements;
break;
-
-
case n:
Valid C Statements;
break;
default:
Valid C Statements;
break;
}

Switch statements can also be called matching case statements. If matches the value in variable (switch(variable)) with any of the case inside, the statements under the case that matches will be executed. If none of the case is matched the statement under default will be executed.

switch.c

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter a no between 1 and 5 : ");
scanf("%d",&a);

switch(a)
{
case 1:
printf("You choosed One");
break;
case 2:
printf("You choosed Two");
break;
case 3:
printf("You choosed Three");
break;
case 4:
printf("You choosed Four");
break;
case 5:
printf("You choosed Five");
break;
default :
printf("Wrong Choice. Enter a no between 1 and 5");
break;
}

getch();
}

Program Algorithm / Explanation :

  1. #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
  2. #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.
  3. main () function is the place where C program execution begins.
  4. Variable a of type int is declared.
  5. scanf is used to get the value from user for variable a.
  6. variable a is included for switch case. switch(a).
  7. If the user Input was 1 the statement inside case 1: will be executed. Likewise for the rest of the case’s until 5.
  8. But if the user Input is other than 1 to 5 the statement under default : will be executed.

Output :

stich statement in C

5) goto statement in C :

goto is a unconditional jump statement.

Syntax :

goto label;

so we have to use the goto carefully inside a conditional statement.

goto.c

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter 2 nos A and B one by one : ");
scanf("%d%d",&a,&b);
if(a>b)
{
goto first;
}
else
{
goto second;
}

first:
printf("\n A is greater..");
goto g;

second:
printf("\n B is greater..");

g:
getch();
}

Program Algorithm / Explanation :

  1. #include<stdio.h> header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
  2. #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.
  3. main () function is the place where C program execution begins.
  4. Two variables a  & b of type int are declared.
  5. User Inputs are received for a & b using scanf.
  6. if condition is used to check whether a is greater than b. if(a>b).
  7. if it is true goto statement is used to jump to the label first : and the statement under first : is executed and then again jump is performed to get to the end of the program. goto g:
  8. if the condition is false a statement is used to jump to label second. goto second : and the statement under second : is executed.

Output :

goto statement in C

 

 

WEBSITE TUTORIALS