If Statement Mischief...

Okay, I heard this person who had to be perfectly insane say something really weird... He said that if statements take up a lot of RAM... Now, being interested, I continued to listen... He said that it is actually more memory saving to write your own comparison function... Now, this just can't be! if statements is part of the core of C++, is it not? So, my question is, how would you writing an if statement function without an if statement? At some point you would need a Boolean return value of a comparison.... An if statement. So, is this possible and how?

Actually, I have another question... I have about 78 if statements that are running every frame in my game, and it is managing to slow it down... Every if statement has a break if it is true, and no more than one of them is true at a time... How would I optimize this? Thank you for you time and help!!

- Kyle
I don't know about exact RAM usage but I wouldn't worry too much about a few if statements, there are going to be many more things that will take up more memory then that in your program and I don't see many things you could write that would be more efficient.

The problem is more about the situations you use them in then the statements themselves. In a lot of cases there are better ways to write pieces of code without using 100 if statements in a line etc. Make sure you are using if-else if-else when you do use them together or switch statements when possible.

Showing exactly how to optimize code to use less if statements is hard without any samples to work off of.
That person was high as a kite.

The if statement literally translates into only a few assembly instructions that will run natively on the compiler. There is very little (read.. none) overhead in an if-statement.
Okay, I heard this person who had to be perfectly insane say something really weird... He said that if statements take up a lot of RAM...
Please present the complete argument you heard from the person

I have about 78 if statements that are running every frame in my game, and it is managing to slow it down... Every if statement has a break if it is true, and no more than one of them is true at a time... How would I optimize this?
1. Can you predict which if statment will be true in the next frame?, if yes may be you can use function pointer, and keep changing them every frame
2. What does a typical if statement expresssion look like?, If is simple variable check then may be you can work out some kind of offset based jump mechanism

Okay, I'll try to attach the code... I was not having luck yesterday...

Okay... There we go. This is SFML code. The last huge if statement was simply to see if there is any difference in speed when putting it in one statement instead of 26... I saw no difference.

And unfortunately no... There is no way of predicting i the next key to be pressed, however, I can switch the shift and not shift... It is statistically more likely for a letter to not be caps..

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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
char GetLetterPressed()
{
	const sf::Input& Input = Screen::Window.GetInput();
	if (Input.IsKeyDown(sf::Key::LShift) || Input.IsKeyDown(sf::Key::RShift))
	{
		if (Input.IsKeyDown(sf::Key::A) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'A';
		}
		
		else if (Input.IsKeyDown(sf::Key::B) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'B';
		}
		
		else if (Input.IsKeyDown(sf::Key::C) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'C';
		}
		
		else if (Input.IsKeyDown(sf::Key::D) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'D';
		}
		
		else if (Input.IsKeyDown(sf::Key::E) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'E';
		}
		
		else if (Input.IsKeyDown(sf::Key::F) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'F';
		}
		
		else if (Input.IsKeyDown(sf::Key::G) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'G';
		}
		
		else if (Input.IsKeyDown(sf::Key::H) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'H';
		}
		
		else if (Input.IsKeyDown(sf::Key::I) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'I';
		}
		
		else if (Input.IsKeyDown(sf::Key::J) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'J';
		}
		
		else if (Input.IsKeyDown(sf::Key::K) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'K';
		}
		
		else if (Input.IsKeyDown(sf::Key::L) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'L';
		}
		
		else if (Input.IsKeyDown(sf::Key::M) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'M';
		}
		
		else if (Input.IsKeyDown(sf::Key::N) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'N';
		}
		
		else if (Input.IsKeyDown(sf::Key::O) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'O';
		}
		
		else if (Input.IsKeyDown(sf::Key::P) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'P';
		}
		
		else if (Input.IsKeyDown(sf::Key::Q) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'Q';
		}
		
		else if (Input.IsKeyDown(sf::Key::R) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'R';
		}
		
		else if (Input.IsKeyDown(sf::Key::S) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'S';
		}
		
		else if (Input.IsKeyDown(sf::Key::T) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'T';
		}
		
		else if (Input.IsKeyDown(sf::Key::U) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'U';
		}
		
		else if (Input.IsKeyDown(sf::Key::V) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'V';
		}
		
		else if (Input.IsKeyDown(sf::Key::W) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'W';
		}
		
		else if (Input.IsKeyDown(sf::Key::X) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'X';
		}
		
		else if (Input.IsKeyDown(sf::Key::Y) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'Y';
		}
		
		else if (Input.IsKeyDown(sf::Key::Z) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'Z';
		}
	}
	if (!Input.IsKeyDown(sf::Key::LShift) && !Input.IsKeyDown(sf::Key::RShift))
	{
		if (Input.IsKeyDown(sf::Key::A) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'a';
		}
		
		else if (Input.IsKeyDown(sf::Key::B) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'b';
		}
		
		else if (Input.IsKeyDown(sf::Key::C) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'c';
		}
		
		else if (Input.IsKeyDown(sf::Key::D) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'd';
		}
		
		else if (Input.IsKeyDown(sf::Key::E) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'e';
		}
		
		else if (Input.IsKeyDown(sf::Key::F) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'f';
		}
		
		else if (Input.IsKeyDown(sf::Key::G) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'g';
		}
		
		else if (Input.IsKeyDown(sf::Key::H) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'h';
		}
		
		else if (Input.IsKeyDown(sf::Key::I) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'i';
		}
		
		else if (Input.IsKeyDown(sf::Key::J) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'j';
		}
		
		else if (Input.IsKeyDown(sf::Key::K) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'k';
		}
		
		else if (Input.IsKeyDown(sf::Key::L) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'l';
		}
		
		else if (Input.IsKeyDown(sf::Key::M) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'm';
		}
		
		else if (Input.IsKeyDown(sf::Key::N) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'n';
		}
		
		else if (Input.IsKeyDown(sf::Key::O) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'o';
		}
		
		else if (Input.IsKeyDown(sf::Key::P) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'p';
		}
		
		else if (Input.IsKeyDown(sf::Key::Q) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'q';
		}
		
		else if (Input.IsKeyDown(sf::Key::R) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'r';
		}
		
		else if (Input.IsKeyDown(sf::Key::S) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 's';
		}
		
		else if (Input.IsKeyDown(sf::Key::T) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 't';
		}
		
		else if (Input.IsKeyDown(sf::Key::U) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'u';
		}
		
		else if (Input.IsKeyDown(sf::Key::V) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'v';
		}
		
		else if (Input.IsKeyDown(sf::Key::W) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'w';
		}
		
		else if (Input.IsKeyDown(sf::Key::X) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'x';
		}
		
		else if (Input.IsKeyDown(sf::Key::Y) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'y';
		}
		
		else if (Input.IsKeyDown(sf::Key::Z) && !Keyboard::Key)
		{
			Keyboard::Key = true;
			return 'z';
		}
	}
	
	if (Input.IsKeyDown(sf::Key::A) ||
	Input.IsKeyDown(sf::Key::B) ||
	Input.IsKeyDown(sf::Key::C) ||
	Input.IsKeyDown(sf::Key::D) ||
	Input.IsKeyDown(sf::Key::E) ||
	Input.IsKeyDown(sf::Key::F) ||
	Input.IsKeyDown(sf::Key::G) ||
	Input.IsKeyDown(sf::Key::H) ||
	Input.IsKeyDown(sf::Key::I) ||
	Input.IsKeyDown(sf::Key::J) ||
	Input.IsKeyDown(sf::Key::K) ||
	Input.IsKeyDown(sf::Key::L) ||
	Input.IsKeyDown(sf::Key::M) ||
	Input.IsKeyDown(sf::Key::N) ||
	Input.IsKeyDown(sf::Key::O) ||
	Input.IsKeyDown(sf::Key::P) ||
	Input.IsKeyDown(sf::Key::Q) ||
	Input.IsKeyDown(sf::Key::R) ||
	Input.IsKeyDown(sf::Key::S) ||
	Input.IsKeyDown(sf::Key::T) ||
	Input.IsKeyDown(sf::Key::U) ||
	Input.IsKeyDown(sf::Key::V) ||
	Input.IsKeyDown(sf::Key::W) ||
	Input.IsKeyDown(sf::Key::X) ||
	Input.IsKeyDown(sf::Key::Y) ||
	Input.IsKeyDown(sf::Key::Z))
	{
		Keyboard::Key = true;
	}
	else
	{
		Keyboard::Key = false;
	}
	return NULL;
}
Did it occur to you to process events?
Can you not simply get the key pressed in the event, and store it. Later return that from the this function...
Last edited on
If you have written such code as can be seen above with so many conditional checks, you need to ask yourself "there must be an easier solution to this?", as suggested by @cire and @Santosh. A basic rule of programming is that if the code looks nice 'n' tidy then the solution is probably the right one...
Topic archived. No new replies allowed.