Conditional statement in C

We will look into following conditional statements.

if
if else
else if
switch
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
#include
void main()
{
int a,b;
a=10;
b=5;
if(a>b)
{
printf(“a is greater”);
}
getch();
}
Program Algorithm / Explanation :

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

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
#include
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 :

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

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
#include
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 :

#include header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
#include is used because the C in-built function getch comes under conio.h header files.
main () function is the place where C program execution begins.
Two variables a & b of type int are declared.
Values for a & b are received from user through scanf.
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.

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.

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

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
#include
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 :

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

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
#include
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 :

#include header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files.
#include is used because the C in-built function getch() comes under conio.h header files.
main () function is the place where C program execution begins.
Two variables a & b of type int are declared.
User Inputs are received for a & b using scanf.
if condition is used to check whether a is greater than b. if(a>b).
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:
if the condition is false a statement is used to jump to label second. goto second : and the statement under second : is executed.
Output :

Advertisements

About pinku12

HIMANSHU SRIVASTAVA VARANASI
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s