changing switch statements to if-else statements

Im having problems changing this switch statement to and if-else statement. Can I please get some assistant...this is what i have did so far but Im still getting syntax errors:

SWITCH STATEMENT:
void check_zone(void)
{
unsigned int m;

P1 = 0xFF; //make P1 an input

for(m=0; m<20; m++)
{
P1 |= 0xF0; //must be P1 |= 0xF0;
switch(P1)
{
case(0xF0):
{
lcd_init();
write_string(0,0, "All Doors and");
write_string(0,1,"Window's Ajar");
P1 = 0x0F; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0xE0):
{
lcd_init();
write_string(0,0, "Door 1, Window 1,");
write_string(0,1,"And Window 2 Ajar");
P1 = 0x0E; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0xD0):
{
lcd_init();
write_string(0,0, "Door 2, Window 1,");
write_string(0,1,"And Window 2 Ajar");
P1 = 0x0D; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0xC0):
{
lcd_init();
write_string(0,0, "Window 1, And");
write_string(0,1,"Window 2 Ajar");
P1 = 0x0C; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0xB0):
{
lcd_init();
write_string(0,0, "Window 1, Door 1,");
write_string(0,1,"And Door 2 Ajar");
P1 = 0x0B; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0xA0):
{
lcd_init();
write_string(0,0, "Window 1, And");
write_string(0,1,"Door 1 Ajar");
P1 = 0x0A; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0x90):
{
lcd_init();
write_string(0,0, "Window 1, And");
write_string(0,1,"Door 2 Ajar");
P1 = 0x09; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0x80):
{
lcd_init();
write_string(0,0, "Window 1, Ajar");
P1 = 0x08; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0x70):
{
lcd_init();
write_string(0,0, "Window 2, Door 1");
write_string(0,1,"And Door 2 Ajar");
P1 = 0x07; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0x60):
{
lcd_init();
write_string(0,0, "Window 2, And");
write_string(0,1,"Door 1 Ajar");
P1 = 0x06; msecDelay(500); P1 = 0x00;
msecDelay(500); lcd_init();
input = 0;
break;
}
case(0x50):
{
lcd_init();
write_string(0,0, "Window 2, And");
write_string(0,1,"Door 2 Ajar");
P1 = 0x05; msecDelay(500); P1 = 0x00;
msecDelay(500); lcd_init();
check = 0;
break;
}
case(0x40):
{
lcd_init();
write_string(0,0, "Window 2 Ajar");
P1 = 0x04; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0x30):
{
lcd_init();
write_string(0,0, "Door 1, And");
write_string(0,1,"Door 2 Ajar");
P1 = 0x03; msecDelay(500); P1 = 0x00;
msecDelay(500); lcd_init();
check = 0;
break;
}
case(0x20):
{
lcd_init();
write_string(0,0, "Door 1 Ajar");
P1 = 0x02; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
case(0x10):
{
lcd_init();
write_string(0,0,"Door 2 Ajar");
P1 = 0x01; msecDelay(500); P1 = 0x00;
msecDelay(500);
check = 0;
break;
}
default: {P1 = 0x00; valid = 1;}
}//switch
}//for
} //check_zone


MY IF-ELSE STATEMENT:
void test_area (void)
{
unsigned int z;
for(z=0; z<10; z++)
{
//Check Door.
if (P0 == 0xFF)
{
write_string(0,0, "Doors are Ajar");
P1 = 0xFF; //LEDs on
msecdelay (250);

P1 = 0x00;//LEDs off
msecdelay (250);
}
//Check Window
else if (P0 == 0xFE)
{
write_string(0,0, "Windows are Open");
P1 = 0xFE;
msecdelay (250);

P1 = 0x00;
msecdelay (250);
}

//Check Smoke Alarm
else if (P0 == 0xFD)
{
write_string(0,0, "Smoke Detected");
P1 = 0xFD;
msecdelay (250);

P1 = 0x00;
msecdelay (250);
}

//Check Motion Detector
else if (P0 == 0xFC)
{
write_string(0,0, "Motion Sensed");
P1 = 0xFC;
msecdelay (250);

P1 = 0xFC;
msecdelay (250);
}

else
{
P1 = 0xFF;
msecdelay (1000);
P1 = 0x00;
msecdelay (1000);
}
}
}
1. Could you put you code between [code][/code] tags?
2. Why do you want to make the change? When comparing integral values, switch is better than if.
The class that I am in, we are having problems with switch and if statements so the teacher wanted to know who can change between the two and vice versa.

What do mean by this:Could you put you code between tags?
If you don't use tags, when you paste code, it looks like this:
int main(){
std::cout <<"Hello, World!"<<std::endl;
return 0;
}
If you use code tags, (which is done by placing a [code] and a [/code] at the beginning and the end of the code respectively), it will look like this:
1
2
3
4
int main(){
    std::cout <<"Hello, World!"<<std::endl;
    return 0;
}


Oh, it's one of those assignments that require you to stupidify code, I see.
Last edited on
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
SWITCH STATEMENT
void check_zone(void) 
{
unsigned int m;

P1 = 0xFF; //make P1 an input 

for(m=0; m<20; m++)
{ 
P1 |= 0xF0; //must be P1 |= 0xF0;
switch(P1)
{
case(0xF0):
{
lcd_init();
write_string(0,0, "All Doors and"); 
write_string(0,1,"Window's Ajar");
P1 = 0x0F; msecDelay(500); P1 = 0x00; 
msecDelay(500); 
check = 0;
break;
}
case(0xE0):
{
lcd_init();
write_string(0,0, "Door 1, Window 1,"); 
write_string(0,1,"And Window 2 Ajar");
P1 = 0x0E; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break; 
}
case(0xD0):
{
lcd_init();
write_string(0,0, "Door 2, Window 1,"); 
write_string(0,1,"And Window 2 Ajar"); 
P1 = 0x0D; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
}
case(0xC0):
{
lcd_init();
write_string(0,0, "Window 1, And"); 
write_string(0,1,"Window 2 Ajar");
P1 = 0x0C; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
}
case(0xB0):
{ 
lcd_init();
write_string(0,0, "Window 1, Door 1,"); 
write_string(0,1,"And Door 2 Ajar"); 
P1 = 0x0B; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
}
case(0xA0):
{
lcd_init();
write_string(0,0, "Window 1, And"); 
write_string(0,1,"Door 1 Ajar");
P1 = 0x0A; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
}
case(0x90):
{
lcd_init();
write_string(0,0, "Window 1, And"); 
write_string(0,1,"Door 2 Ajar");
P1 = 0x09; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
}
case(0x80):
{
lcd_init();
write_string(0,0, "Window 1, Ajar"); 
P1 = 0x08; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
}
case(0x70):
{
lcd_init();
write_string(0,0, "Window 2, Door 1"); 
write_string(0,1,"And Door 2 Ajar");
P1 = 0x07; msecDelay(500); P1 = 0x00; 
msecDelay(500); 
check = 0;
break;
}
case(0x60):
{
lcd_init(); 
write_string(0,0, "Window 2, And"); 
write_string(0,1,"Door 1 Ajar");
P1 = 0x06; msecDelay(500); P1 = 0x00;
msecDelay(500); lcd_init(); 
input = 0;
break;
}
case(0x50):
{
lcd_init(); 
write_string(0,0, "Window 2, And"); 
write_string(0,1,"Door 2 Ajar");
P1 = 0x05; msecDelay(500); P1 = 0x00;
msecDelay(500); lcd_init();
check = 0;
break;
}
case(0x40):
{
lcd_init();
write_string(0,0, "Window 2 Ajar"); 
P1 = 0x04; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
}
case(0x30):
{
lcd_init();
write_string(0,0, "Door 1, And"); 
write_string(0,1,"Door 2 Ajar");
P1 = 0x03; msecDelay(500); P1 = 0x00;
msecDelay(500); lcd_init();
check = 0;
break;
}
case(0x20):
{
lcd_init();
write_string(0,0, "Door 1 Ajar"); 
P1 = 0x02; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
}
case(0x10):
{
lcd_init();
write_string(0,0,"Door 2 Ajar"); 
P1 = 0x01; msecDelay(500); P1 = 0x00;
msecDelay(500); 
check = 0;
break;
} 
default: {P1 = 0x00; valid = 1;} 
}//switch 
}//for 
} //check_zone 


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
MY IF-ELSE STATEMENT
void test_area (void)
{
unsigned int z;
for(z=0; z<10; z++)
{ 
//Check Door.
if (P0 == 0xFF)
{
write_string(0,0, "Doors are Ajar");
P1 = 0xFF; //LEDs on
msecdelay (250);

P1 = 0x00;//LEDs off
msecdelay (250); 
}
//Check Window
else if (P0 == 0xFE)
{
write_string(0,0, "Windows are Open");
P1 = 0xFE;
msecdelay (250);

P1 = 0x00;
msecdelay (250); 
}

//Check Smoke Alarm
else if (P0 == 0xFD)
{
write_string(0,0, "Smoke Detected");
P1 = 0xFD;
msecdelay (250);

P1 = 0x00;
msecdelay (250); 
}

//Check Motion Detector
else if (P0 == 0xFC)
{
write_string(0,0, "Motion Sensed");
P1 = 0xFC;
msecdelay (250);

P1 = 0xFC;
msecdelay (250);
}

else
{
P1 = 0xFF;
msecdelay (1000);
P1 = 0x00;
msecdelay (1000);
}
} 
}


BETTER????
Ugh. No. Don't copy-paste the first post. Copy from the original source.
Ah, forget it. I'll just use indent.

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
void
check_zone (void)
{
  unsigned int m;

  P1 = 0xFF;			//make P1 an input

  for (m = 0; m < 20; m++)
    {
      P1 |= 0xF0;		//must be P1 |= 0xF0;
      switch (P1)
	{
	case (0xF0):
	  {
	    lcd_init ();
	    write_string (0, 0, "All Doors and");
	    write_string (0, 1, "Window's Ajar");
	    P1 = 0x0F;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0xE0):
	  {
	    lcd_init ();
	    write_string (0, 0, "Door 1, Window 1,");
	    write_string (0, 1, "And Window 2 Ajar");
	    P1 = 0x0E;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0xD0):
	  {
	    lcd_init ();
	    write_string (0, 0, "Door 2, Window 1,");
	    write_string (0, 1, "And Window 2 Ajar");
	    P1 = 0x0D;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0xC0):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 1, And");
	    write_string (0, 1, "Window 2 Ajar");
	    P1 = 0x0C;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0xB0):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 1, Door 1,");
	    write_string (0, 1, "And Door 2 Ajar");
	    P1 = 0x0B;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0xA0):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 1, And");
	    write_string (0, 1, "Door 1 Ajar");
	    P1 = 0x0A;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0x90):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 1, And");
	    write_string (0, 1, "Door 2 Ajar");
	    P1 = 0x09;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0x80):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 1, Ajar");
	    P1 = 0x08;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0x70):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 2, Door 1");
	    write_string (0, 1, "And Door 2 Ajar");
	    P1 = 0x07;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0x60):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 2, And");
	    write_string (0, 1, "Door 1 Ajar");
	    P1 = 0x06;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    lcd_init ();
	    input = 0;
	    break;
	  }
	case (0x50):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 2, And");
	    write_string (0, 1, "Door 2 Ajar");
	    P1 = 0x05;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    lcd_init ();
	    check = 0;
	    break;
	  }
	case (0x40):
	  {
	    lcd_init ();
	    write_string (0, 0, "Window 2 Ajar");
	    P1 = 0x04;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0x30):
	  {
	    lcd_init ();
	    write_string (0, 0, "Door 1, And");
	    write_string (0, 1, "Door 2 Ajar");
	    P1 = 0x03;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    lcd_init ();
	    check = 0;
	    break;
	  }
	case (0x20):
	  {
	    lcd_init ();
	    write_string (0, 0, "Door 1 Ajar");
	    P1 = 0x02;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	case (0x10):
	  {
	    lcd_init ();
	    write_string (0, 0, "Door 2 Ajar");
	    P1 = 0x01;
	    msecDelay (500);
	    P1 = 0x00;
	    msecDelay (500);
	    check = 0;
	    break;
	  }
	default:
	  {
	    P1 = 0x00;
	    valid = 1;
	  }
	}			//switch
    }				//for
}				//check_zone


void test_area (void)
{
  unsigned int z;
  for (z = 0; z < 10; z++)
    {
	//Check Door.
	  if (P0 == 0xFF)
		{
		  write_string (0, 0, "Doors are Ajar");
		  P1 = 0xFF;		//LEDs on
		  msecdelay (250);

		  P1 = 0x00;		//LEDs off
		  msecdelay (250);
		}
	//Check Window
	  else if (P0 == 0xFE)
		{
		  write_string (0, 0, "Windows are Open");
		  P1 = 0xFE;
		  msecdelay (250);

		  P1 = 0x00;
		  msecdelay (250);
		}

	//Check Smoke Alarm
	  else if (P0 == 0xFD)
		{
		  write_string (0, 0, "Smoke Detected");
		  P1 = 0xFD;
		  msecdelay (250);

		  P1 = 0x00;
		  msecdelay (250);
		}

	//Check Motion Detector
	  else if (P0 == 0xFC)
		{
		  write_string (0, 0, "Motion Sensed");
		  P1 = 0xFC;
		  msecdelay (250);

		  P1 = 0xFC;
		  msecdelay (250);
		}

	  else
		{
		  P1 = 0xFF;
		  msecdelay (1000);
		  P1 = 0x00;
		  msecdelay (1000);
		}
    }
}
Last edited on
BETTER????


Yo dude, what's got you all grumpy? He was just trying to let you have a neat post so people might help you and not close the topic because it wasn't neat. I didn't mean to be rude just now, but try to be nicer, and everyone will be your friend before you know it!
Im asking a question I dnt need a FRIEND and mind you business. all i said was BETTER????? and u jumping to concludes. i dont care if you were trying to be rude ot not. I was not talking to YOU!!!! There was othing disrespectful with what I said you are just trying blow stuff out of proportion.
I'm stunned at how rude people can be. I mean WOW!!
Just because this is a friendly environment, I won't answer you in your own grumpy manner, because actually humans lost that trait, and APES adopted it. BTW you were talking to me. Cuz when you post here. You are talking to everybody who reads your post!!
Last edited on
I GUESS YOU DID!!!!
Actually, I didn't care for that "better", either.

See, this switch doesn't make sense to me. First you assign 0XFF to P1, then you P1 OR 0xF0, which leaves it as P1 as 0xFF.
Then there's the if, that uses P0, yet I don't P0 being set anywhere. Didn't you learn not to use globals?

What exactly is wrong here, aside from what I just mentioned?
soullady wrote:
>Dont you ever in your life talk to me like that I wasnt talking to YOU.


That was written to me by soullady. Was that a threat? Because if it was, I'm pretty scared right now!
Please do not spam my inbox with such things. I'll hate to make you unhappy just because I was trying to help.
Yeah Ok, anyways.......I have warnings that say UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
You didnt help at all you posted your stupid opinion. and i dont make threats thats as close as I could get to a promise
Whatever. Look I don't want to start a fight here. I was just telling you. If you don't want to be told, FINE BY ME. I don't care.
If you didnt care you would have never posted anything from the beginning. you made a ignorant comment to me I said nothing to you
Okay, Whatever.
Topic archived. No new replies allowed.