Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to use if condition in switch statement

Status
Not open for further replies.

vead

Full Member level 5
Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Activity points
3,815
what does if statement do within switch statement. I want apply if condition for following task

first case if (task=task1)

second case if (task=task2)

third case if (task=task1)


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<reg51.h>    //header file
#define task1 (1)
#define task2 (2)
#define task3  (3)
 
//prototype function
 
DoSomething0();
DoSomething1();
DoSomething2();
 
int main()
{
    unsigned int task;
 
  switch  (task)
  {
        case task1:
        DoSomething0();
        break;
   
        case task2:
        DoSomething1();
        break;
   
        case task3:
        DoSomething2();
        break;         
  }
 
}
  
     DoSomething0()
       { }
     DoSomething1()
       { }
     DoSomething2()
       { }

 

"Switch-case" statement is equivalent to "if-goto". So if you do the following:

Code C - [expand]
1
2
3
4
5
6
7
switch (task)
{
    case task1:
        if (task == task1)
            DoSomething1();
        break;
}


that makes no sense as case already substituted if and you have two if's for the same condition. But if you want an additional check for a particular task, e.g.

Code C - [expand]
1
2
3
4
5
6
7
switch (task)
{
    case task1:
        if (someVar == someValue)
            DoSomething1();
        break;
}


you can use it without problems.
 

This approach is better to read:
Code:
void Task1 (void)
{
    //do something
    printf("Task1\n");
}

void Task2 (void)
{
    //do something
    printf("Task2\n");
}

void Task3 (void)
{
    //do something
    printf("Task3\n");
}

int main()
{
    void (* Tasks[])(void) = {Task1, Task2, Task3};

    char i = 0;
    
    for (i = 0; i < 3; i++)
        Tasks[i]();
    
    return 0;
}
 
Last edited:

All compilers I've used do not allow variables as arguments for case statements. If you can do that with some compiler, it surelly will turn your code not portable to work with other toolchains.
 

Typically, you'll use an enum to define a set of integer constants, e.g.

Code:
enum E_STATE = {task0, task1, task2, task3};
 

hello
I have compiled below program with know error but when I test on hardware it does not work. so I am just checking what's wrong with code
keil compiler , AT89c51.
c program to blink leds with different times

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<reg51.h>    //header file
 
#define  LED1_ON   1
#define  LED1_OFF  0
#define  LED2_ON   1
#define  LED2_OFF  0
#define  LED3_ON   1
#define  LED4_OFF  0
 
sbit   LED1     = P2^0;    //LED 1 conncted to port P2 pin 0
sbit   LED2     = P2^1;    //LED 2 conncted to port P2 pin 1
sbit   LED3     = P2^2;    //LED 3 conncted to port P2 pin 2
 
typedef enum
{
    task1,
    task2,
    task3
};
// prototype function
void LED1_Blink(void);
void LED2_Blink(void);
void LED3_Blink(void);
int main(void)
{
   unsigned int task=0;
 
    while(1)
  {
   switch  (task)
   {
      case task1:
                 if (task == task1)
                 {
                    LED1 = LED1_ON;
                    LED1_Blink();
                    LED1 = LED1_OFF;
                 }
         break;
 
      case task2:
                 if (task == task2)
                 {
                      LED2 = LED2_ON;
                      LED2_Blink();
                      LED2 = LED2_OFF;
                 }
         break;
 
      case task3:
                 if (task == task3)
                 {
                    LED3 = LED3_ON;
                    LED3_Blink();
                    LED3 = LED4_OFF;
                 }
         break;
   }
}
}
 
void LED1_Blink ()
{
    unsigned int i;
    for (i = 0; i < 1000; i++);
}
 
void LED2_Blink()
{
    unsigned int j;
    for (j = 0; j < 1500; j++);
}
 
void LED3_Blink()
{
   unsigned int k;
    for (k = 0; k < 1200; k++);
}

 

hello,


you don't need to add a test like if ..
Switch is made for that !

switch (task)
{
case task1:
LED1 = LED1_ON;
LED1_Blink();
LED1 = LED1_OFF;
break;

case task2:
LED2 = LED2_ON;
LED2_Blink();
LED2 = LED2_OFF;
break;

case task3:
LED3 = LED3_ON;
LED3_Blink();
LED3 = LED4_OFF;
break;
}
 

Add this:

Code C - [expand]
1
2
3
4
5
6
typedef enum
{
    task1,
    task2,
    task3
}myTasks;



And then in main() just use:

Code C - [expand]
1
2
3
4
int main(void)
{
   myTasks task;
.....

 

Which different behavior do you expect by declaring
Code:
myTasks task;

instead of
Code:
int task;
 

please look at below program. how many mistake do you find in program what could be possible reason

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<reg51.h>    //header file
 
#define  LED1_ON   1
#define  LED1_OFF  0
#define  LED2_ON   1
#define  LED2_OFF  0
#define  LED3_ON   1
#define  LED4_OFF  0
 
sbit   LED1     = P2^0;    //LED 1 conncted to port P2 pin 0
sbit   LED2     = P2^1;    //LED 2 conncted to port P2 pin 1
sbit   LED3     = P2^2;    //LED 3 conncted to port P2 pin 2
 
enum mytask {
  task1 = 1,
  task2,
  task3
};
 
// prototype function
void LED1_Blink(void);
void LED2_Blink(void);
void LED3_Blink(void);
int main(void)
 
{ 
    int i=task1;
 
    for (i=0; i<4; i++)
 
    switch(i)
  {
    case task1: 
    { 
        LED1_ON;
      LED1_Blink (); 
        LED1_OFF;
    }
    break;
    case task2:
    {
            LED2_ON;
      LED2_Blink();
        LED1_OFF;
     }       
     break;
     case task3:
      {
             LED3_ON;   
       LED3_Blink();
             LED3_FF;
      }
      break;
   }
 
}
void LED1_Blink ()
{
    unsigned int i;
    for (i = 0; i < 1000; i++);
}
 
void LED2_Blink()
{
    unsigned int j;
    for (j = 0; j < 1500; j++);
}
 
void LED3_Blink()
{
   unsigned int k;
    for (k = 0; k < 1200; k++);
}

 

how many mistake do you find in program what could be possible reason

The main reason is because you are not making tests before ask for help. If you did it, would find a syntax compilation error at the line 51 and would fix it before posting the code:

For example, this macro LED3_FF should be defined as LED3_OFF but you declared as LED4_OFF but you have used the undeclared one.
 

Moreover, there is no LEDs toggling at all. #defines define (sorry for tautology) only constants, but they are not macros, so after the preprocessing the compilier will see only 1's and 0's followed by semicolon. Your previous version was correct:

Code C - [expand]
1
LED1 = LED1_ON;


Task2 has LED1_OFF, but not LED2_OFF. Although thay are both equal to 0 it's not good to place another's constant.
The style of the code looks very wierd. A lot of copy-paste programming, unnecessary redundancy. But it will work (if you correct led switching).
 

Moreover, there is no LEDs toggling at all. #defines define (sorry for tautology) only constants, but they are not macros, so after the preprocessing the compilier will see only 1's and 0's followed by semicolon. Your previous version was correct:

Code C - [expand]
1
LED1 = LED1_ON;


Task2 has LED1_OFF, but not LED2_OFF. Although thay are both equal to 0 it's not good to place another's constant.
The style of the code looks very wierd. A lot of copy-paste programming, unnecessary redundancy. But it will work (if you correct led switching).
that was my mistake. while doing so many test I posted old code. now code are working fine. I just made that program to learn how to do multiple tasks using c program.
thank you very much
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top