ShopDreamUp AI ArtDreamUp
Deviation Actions
Literature Text
Chapter 4 - Program Flow
When you make a program it starts and int main() and goes to the end.
Last chapter you learned how to go off and start another function...
which it will go though and then when it ends it goes back to where
it left off.
Now it is time to learn to make programs that are not so linear.
Because there is so much to cover, I will only give an example of
each, not a whole program.
First is the "if" statement.
Example 1 - if and if ... else
-----
if (y == 0) {
if (x == 1) { //17
//do stuff here
} else if (x > 1 & z > 0) { //line 19
//do other stuff here
} else { //21
//do something creative here
} //23
}
if (z =< 0 | y != 0) /*do something*/; //25
-----
The comments with numbers are line numbers, which I will use to point
things out.
First off, how if statements work is they skip the code in their scope
if the value in the parentese is 0, and run the code if it is 1. Yes,
it uses a bool value. You can embed "if" statements inside each other,
as shown from line 17 though 23. Also you can use the "else" statement
to have is do something else only if the "if" statement is false (see
lines 19 and 21). The scope of an "if" statement goes to the first ";"
like line 25 or it is the between "{}" like the first statement.
You can add conditions that must also be true with the "&" symbol
which means "and" (see line 19). You can add optional conditions
that will let the code in the scope run if either is true with the
"|" mark which means "or".
Next is the very similar "switch" statement.
Example 2 - switch
-----
switch(x) {
case (0)://do something
break;
case (1)://do something
case (2)://do something
break;
default://do something
break;
}
-----
The "switch" statement will skip to the case that is followed
by a value equal to that of the variable use for the switch
(in this case it is "x"). It will then execute the code till
it reachs a "break" statement. For this example, if x is equal
to 1 it will do the code for both case 1 and 2 and only that of
2 if it is equal to 2. If none of the cases have the value,
it will goto the "default" case if there is one, and if
there is not it skips the switch.
Now let us goto "goto" loops.
Example 3 - goto
-----
loop:
cout << "Enter a number: ";
cin >> x;
if (x == 0) goto loop;
-----
"goto" makes the program goto the point in the current
function with the tag following "goto". You should always
have away out of the loop.
Because there are safer, more controlled types of loops
goto is shunned. While the lesson continues, lets goto
"while" loops.
Example 4 - while and do ... while
-----
while (x==0) {
//do stuff
}
while (y==0) /*function*/;
do {
//stuff
} while(x==1);
do /*function*/; while (y==0);
-----
Normal "while" loops are exactly like an "if" statement
except that it will repeat as long as is true. "do
while" loops on the other hand will run once, regardless
of wether the condition is true or not.
"while" loops and "do while" loops end if they reach a
"break" statement. If they reach a "continue" statement,
they go back to the begining of the loop.
Example 5 - for
-----
for (int x=0; x<5; x++) {
}
for (int x=0 y=6; x<5 & y>0; x++, y--) ;
-----
"for" loops have three parts: one to that is done at the start,
the condtion to determine if the loop should continue, and the
last one is a set of operations to preform each time the loop
reaches the end.
Now there is one more thing you really must learn about C++ to
have mastered the basics. On to classes and structs.
When you make a program it starts and int main() and goes to the end.
Last chapter you learned how to go off and start another function...
which it will go though and then when it ends it goes back to where
it left off.
Now it is time to learn to make programs that are not so linear.
Because there is so much to cover, I will only give an example of
each, not a whole program.
First is the "if" statement.
Example 1 - if and if ... else
-----
if (y == 0) {
if (x == 1) { //17
//do stuff here
} else if (x > 1 & z > 0) { //line 19
//do other stuff here
} else { //21
//do something creative here
} //23
}
if (z =< 0 | y != 0) /*do something*/; //25
-----
The comments with numbers are line numbers, which I will use to point
things out.
First off, how if statements work is they skip the code in their scope
if the value in the parentese is 0, and run the code if it is 1. Yes,
it uses a bool value. You can embed "if" statements inside each other,
as shown from line 17 though 23. Also you can use the "else" statement
to have is do something else only if the "if" statement is false (see
lines 19 and 21). The scope of an "if" statement goes to the first ";"
like line 25 or it is the between "{}" like the first statement.
You can add conditions that must also be true with the "&" symbol
which means "and" (see line 19). You can add optional conditions
that will let the code in the scope run if either is true with the
"|" mark which means "or".
Next is the very similar "switch" statement.
Example 2 - switch
-----
switch(x) {
case (0)://do something
break;
case (1)://do something
case (2)://do something
break;
default://do something
break;
}
-----
The "switch" statement will skip to the case that is followed
by a value equal to that of the variable use for the switch
(in this case it is "x"). It will then execute the code till
it reachs a "break" statement. For this example, if x is equal
to 1 it will do the code for both case 1 and 2 and only that of
2 if it is equal to 2. If none of the cases have the value,
it will goto the "default" case if there is one, and if
there is not it skips the switch.
Now let us goto "goto" loops.
Example 3 - goto
-----
loop:
cout << "Enter a number: ";
cin >> x;
if (x == 0) goto loop;
-----
"goto" makes the program goto the point in the current
function with the tag following "goto". You should always
have away out of the loop.
Because there are safer, more controlled types of loops
goto is shunned. While the lesson continues, lets goto
"while" loops.
Example 4 - while and do ... while
-----
while (x==0) {
//do stuff
}
while (y==0) /*function*/;
do {
//stuff
} while(x==1);
do /*function*/; while (y==0);
-----
Normal "while" loops are exactly like an "if" statement
except that it will repeat as long as is true. "do
while" loops on the other hand will run once, regardless
of wether the condition is true or not.
"while" loops and "do while" loops end if they reach a
"break" statement. If they reach a "continue" statement,
they go back to the begining of the loop.
Example 5 - for
-----
for (int x=0; x<5; x++) {
}
for (int x=0 y=6; x<5 & y>0; x++, y--) ;
-----
"for" loops have three parts: one to that is done at the start,
the condtion to determine if the loop should continue, and the
last one is a set of operations to preform each time the loop
reaches the end.
Now there is one more thing you really must learn about C++ to
have mastered the basics. On to classes and structs.
Chapter 4 in my C++ tutorials.
© 2008 - 2025 Gwenio1
Comments0
Join the community to add your comment. Already a deviant? Log In