This page was last updated on March 13th, 2020(UTC) and it is currently March 21st, 2023(UTC).
That means this page is 3 years, 7 days, 19 hours, 26 minutes and 57 seconds old. Please keep that in mind.
34 - Control Flow
"Control flow" is accomplished via evaluation of boolean values. "if", "elseif", "else", "switch", "while", "do while", "for", "goto" are you main structures. If you've discovered the "loop" instruction in x86, you'll surely find it a curious mess that the compiler is not likely to actually use it. There are 2 good reasons for this: first being that, if a function is called, only certain registers (which are defined by the ABI, which you should look up for any platform before making serious programs for it) are expected to be "preserved," meaning a function call could likely clobber said register, and the second being that they aren't as intimate with the processor as you would expect a compiler maker to be. This means there are often functions available by the CPU that (xchg is a good example) that you'll only pull out when using assembly. Below is an example of the important ones. Obviously, you'll want to get intimate with the disassembly, because these things can change meaning on different processors.
#include <stdio.h>
//------------------------------------------------------------------------
int IF(int x){
if(x<0)
return -1;
else if(x>0)
return 1;
else{ //You can place many lines of code if you use brackets.
return 0;
}
}
//------------------------------------------------------------------------
int WHILE(int x){
if(x < 0) x=~--x; //Let's avoid the infinite loops and lack of output scenarios, please. (0-x would've worked, too, but this is more fun)
while(x) //Should add !=0 or >0 if you're not 100% sure it gets tested as non-zero for true)
printf("%i\n", x--);
return x;
}
//------------------------------------------------------------------------
int DOWHILE(int x){ //Send this one a negative number and watch how it performs differently.
do
printf("%i\n", x--); //Don't forget the meaning of --x over x--
while(x>0);
return x;
}
//------------------------------------------------------------------------
int FOR(int x){
for(int n = 0; n > x; n++)
printf("%i\n", n);
return x;
}
//------------------------------------------------------------------------
int GOTO(int x){
loopy: if(x < 0) goto end;
else{
printf("%i\n", x--);
goto loopy;
}
end: return x;
}
//------------------------------------------------------------------------
extern "C" void meow(){
int sel = 1, num = -10;
switch(sel){
case 1:
printf("IF(%i) = %i\n", num, IF(num));
//No break, see what happens!
case 2:
printf("WHILE(%i) = %i\n", num, WHILE(num));
break;
case 3:
printf("DOWHILE(%i) = %i\n", num, DOWHILE(num));
break;
case 4:
printf("FOR(%i) = %i\n", num, FOR(num));
break;
default: //else
printf("GOTO(%i) = %i\n", num, GOTO(num));
//No need for break;
}
}
At this point, you should also be noticing that there's an unwritten rule for how many tabs you use: subordinate chunks should have a higher tab. This is an informal rule, but it's consistent throughout most of the programming world. There's also different schools on the brackets as well, and there're even politics-like debates on goto's very existance. Also, if you take the time to think about it, "else if" is actually "else { if }", which should mess with your head a little. Some languages will actually make "elseif" a concept of it's own right, though. "break" exists in loops, too, not just switch statements. There's also a "continue" that lets you "skip the rest and go to the next run" which is dangerous if you're using an if or a break to escape from the loop. Experiment and explore!
Get your own web kitty here!
©Copyright 2010-2023. All rights reserved.