# Conditional statement in c language

#### Parth786

Joined Jun 19, 2017
642
I had been reading about conditional statement (if, else or if else or if or if) and Now I want to use conditional statement in Program. Let’s suppose, four switches and one LED connected to microcontroller. I wrote some c programs with example but I am not sure whether its right or wrong. so if there is any mistake please let me know.

If press switch1, LED should be ON and If don’t press switch1, LED should be OFF
If press switch2, LED should be ON and If don’t press switch2, LED should be OFF
If press switch3, LED should be ON and If don’t press switch3, LED should be OFF
If press switch4, LED should be ON and If don’t press switch4, LED should be OFF

C program sing If and else condition
Code:
#include<REGX51.h>

#define LED_ON         (1)
#define LED_OFF        (0)

#define Switch1_ON     (1)
#define Switch2_ON     (1)
#define Switch3_ON     (1)
#define Switch4_ON     (1)

/*set bit to LED*/
sbit LED = P1^0;

/*set bits to Switch*/
sbit  Switch1 = P2^0;
sbit  Switch2 = P2^1;
sbit  Switch3 = P2^2;
sbit  Switch4 = P2^3;

void main()
{
while (1)
{
if(Switch1 == Switch1_ON)
{
LED = LED_ON;
}

else
{
LED = LED_OFF;
}
if(Switch2 == Switch2_ON)
{
LED = LED_ON;
}

else
{
LED = LED_OFF;
}
if(Switch3 == Switch3_ON)
{
LED = LED_ON;
}

else
{
LED = LED_OFF;
}
if(Switch4 == Switch4_ON)
{
LED = LED_ON;
}

else
{
LED = LED_OFF;
}
}

}
C program with If and if else condition
Code:
#include<REGX51.h>

#define LED_ON         (1)
#define LED_OFF        (0)

#define Switch1_ON     (1)
#define Switch2_ON     (1)
#define Switch3_ON     (1)
#define Switch4_ON     (1)

#define Switch1_OFF    (0)
#define Switch2_OFF    (0)
#define Switch3_OFF    (0)
#define Switch4_OFF    (0)

/*set bit to LED*/
sbit LED = P1^0;

/*set bits to Switch*/
sbit  Switch1 = P2^0;
sbit  Switch2 = P2^1;
sbit  Switch3 = P2^2;
sbit  Switch4 = P2^3;

void main()
{
while (1)
{
if(Switch1 == Switch1_ON)
{
LED = LED_ON;
}

else if (Switch1 == Switch1_OFF)
{
LED = LED_OFF;
}
if(Switch2 == Switch2_ON)
{
LED = LED_ON;
}

else if (Switch2 == Switch2_OFF)
{
LED = LED_OFF;
}
if(Switch3 == Switch3_ON)
{
LED = LED_ON;
}

else if (Switch3 == Switch4_OFF)
{
LED = LED_OFF;
}
if(Switch4 == Switch4_ON)
{
LED = LED_ON;
}

else if (Switch4 == Switch4_OFF)
{
LED = LED_OFF;
}
}
}
If press switch1, LED should be ON
If press switch2, LED should be ON
If press switch3, LED should be ON
If press switch4, LED should be ON
If don’t press any switches, LED should be OFF

C programming using if if if and else conditions
Code:
#include<REGX51.h>

#define LED_ON         (1)
#define LED_OFF        (0)

#define Switch1_ON     (1)
#define Switch2_ON     (1)
#define Switch3_ON     (1)
#define Switch4_ON     (1)

/*set bit to LED*/
sbit LED = P1^0;

/*set bits to Switch*/
sbit  Switch1 = P2^0;
sbit  Switch2 = P2^1;
sbit  Switch3 = P2^2;
sbit  Switch4 = P2^3;

void main()
{
while (1)
{
if(Switch1 == Switch1_ON)
{
LED = LED_ON;
if(Switch2 == Switch2_ON)
{
LED = LED_ON;
if(Switch3 == Switch3_ON)
{
LED = LED_ON;
if(Switch4 == Switch4_ON)
{
LED = LED_ON;
}
}
}
}

else
{
LED = LED_OFF;
}
}
}
I think my last program is not in correct way. if you find anywhere wrong please let me know

Last edited:

#### WBahn

Joined Mar 31, 2012
26,398
I had been reading about conditional statement (if, else or if else or if or if) and Now I want to use conditional statement in Program. Let’s suppose, four switches and one LED connected to microcontroller. I wrote some c programs with example but I am not sure whether its right or wrong. so if there is any mistake please let me know.
Well, vead, does it do what you wanted it to do? If not, then it is wrong. If so, then there is at least a chance that it is right. So which is it?

#### Parth786

Joined Jun 19, 2017
642
does it do what you wanted it to do? If not, then it is wrong. If so, then there is at least a chance that it is right. So which is it?

as per as my knowledge. I have written correct program. first and second program will do job but I had doubt in last but I am sure there will be little mistake in closing brackets

#### eetech00

Joined Jun 8, 2013
2,280
Hi

Which micro controller and C compiler are you using?

#### WBahn

Joined Mar 31, 2012
26,398

as per as my knowledge. I have written correct program. first and second program will do job but I had doubt in last but I am sure there will be little mistake in closing brackets
Have you even tried to compile them?

Have you tried to run them?

Have you checked to see if they do what you want them to do?

#### Parth786

Joined Jun 19, 2017
642
Have you even tried to compile them?

Have you tried to run them?

Have you checked to see if they do what you want them to do?
Yes I have compiled code without error
Yes I tried to run them but its not working. to see why its not working i connect only one switch and LED and wrote program but its not working according my program. I wanted to LED ON when switch is high and OFF if switch is low. but its work opposite. LED is ON when switch is open and LED OFF when switch is closed. Now I am in on the way. I will post that Program.

#### Ramussons

Joined May 3, 2013
915

If press switch1, LED should be ON and If don’t press switch1, LED should be OFF
If press switch2, LED should be ON and If don’t press switch2, LED should be OFF
If press switch3, LED should be ON and If don’t press switch3, LED should be OFF
If press switch4, LED should be ON and If don’t press switch4, LED should be OFF
Now, each of these conditions are Independent of each other. Condition of any Switch x, in no way, affects the other switches.

So, you cannot have a "IF ELSE" condition. If you put an ELSE condition, then in case Switch 1 is ON, that LED will light up and the prog will exit.

You will need to have 4 standalone IF statements.

#### Parth786

Joined Jun 19, 2017
642

Now, each of these conditions are Independent of each other. Condition of any Switch x, in no way, affects the other switches.

So, you cannot have a "IF ELSE" condition. If you put an ELSE condition, then in case Switch 1 is ON, that LED will light up and the prog will exit.

You will need to have 4 standalone IF statements.
Actually I am trying to simulate on Proteus design since last 2 hours but its not giving proper output. I don't understand why does LED automatically turn ON. I tried with different different approach but I am not getting result which I want to get. Now I stuck weather my hardware design is wrong or my program is wrong. I am sure my program is correct I think there problem in hardware design
first problem is that LED Turn on Automatically so I tried to OFF initially. and checked with below program. but still LED is on
Code:
#include<REGX51.h>

#define LED_ON         (1)
#define LED_OFF        (0)

/*set bit P1^0 to LED*/
sbit LED = P1^0;

void main()
{
LED = LED_OFF;

}
just to rectify I tried below program but this also not working. I am not getting what's the exactly problem are?

Code:
#include<REGX51.h>

#define LED_ON         (1)
#define LED_OFF        (0)
#define Switch1_ON     (1)
#define Switch1_OFF    (0)

/*set bit P1^0 to LED*/
sbit LED = P1^0;

/*set bit P2^0 to Switch*/
sbit  Switch1 = P2^0;

void main()
{

while (1)
{

if(Switch1 == Switch1_ON )
{
LED = LED_ON;
}

else
{       LED = LED_OFF;

}
}

}
any how I am trying to get result if something is happen I will tell you

Last edited:

#### Ramussons

Joined May 3, 2013
915
I had been reading about conditional statement (if, else or if else or if or if) and Now I want to use conditional statement in Program. Let’s suppose, four switches and one LED connected to microcontroller. I wrote some c programs with example but I am not sure whether its right or wrong. so if there is any mistake please let me know.
Sorry, did'nt notice this.
If this is the condition, then logically. the condition would be IF .OR.
If Switch 1 is Pressed OR Switch 2 is Pressed OR switch 3 is Pressed OR Switch 4 is Pressed, LED should be ON.
Why is a IF ELSE condition being invoked?

Or, maybe, I did not get your question