Sequential
Branching
Looping
Methods/Function Calls
Sequential:
Executing Code Line by Line.
ex.
int i;//Declare variable i
i = 0;//set i to 0
i = i + 1;//increment i by 1
cout << i << endl;//print the value of i to the screen via standard output
Branching
Branch to another line of code based on the results of a Comparison (Comparison's become booleans: true or false). These are the if statements.
They have to be enclosed in brackets too.
ex.
int i;//Instanciate an integer and call it 'i'
cin >> i;//Prompt the User for an input value and store it in 'i'
//the results of 'i == 0'is either true or false. So we can actually assign this to a
//Boolean value: bool toZero = i == 0; is valid!
if(i == 0)//Compare 'i' with the value 0. If they are the same, execute the
//code inside the brackets, otherwise, skip it and go on.
{
cout << "You Entered a 0!!" << endl;//Display the 'You Entered a 0!!' to the command line
}
There are also the switch statements, but they are kind of useless. They are only used to simplify a large if-else statement. You can
do anything without the switches. But what if you want to make multiple decisions. Like, go to movies, if theatres are closed, go shopping. If stores are closed
stay at home and watch movies. If have no movies, commit suicide. These are the nested else's. An if-then-else statement. These could go on and on and on
and on. Let's take a look at one:
int i;//declare i, automatically default as 0
cin >> i;//Prompt user input and store it in i
//Checks the value of i
if(i == 0)
{
cout << "You suck" << endl;//Do this if i is equal to 0
}
else if(i == 1)//Notice this one has no brackets!!!!!!!!!!
cout << "You are cool" << endl;//Do this if i is equal to 1
else if(i == 2)
{
cout << "What the...?" << endl;//Do this if i is equal to 2
}
else
{
cout << "I am confused. :-(" << endl;//Do this if i is not 0, 1, or 2. If i is something else!
}
Technically, you don't need open and close brackets if it is followed by one line, but, it is good habit to always include them anyway.
Better safe then sorry.
Also, te thing that goes inside the comparison place 'if(comparison place)' can be any boolean value: a 'true/false', or even 'bool hi = false; if(hi)'
is legal. You can even put number values: if(2) or if(0). Any positive value greater than zero is considered to be true. A
0 means false. If you haven't already figured out the standard syntax of an if statement, it goes like this:
if(boolean)
{
...code...
}
else//This part is optional
{
...More Code...
}
/**************************/
//Is this LEGAL?????????
if(boolean)// #1
{
if(boolean)// #2
if(boolean)// #3
...code...
else//This goes with #3
{
...code...
//Is there an else for this if?
if(boolean)// #4
{
...code...
}
}
//This else goes with if #2 not #4, why?
else if(boolean)// #5
...code...
}
else//What if does this else go with?
...code...
Well, do you think it is legal...do you...well, it IS LEAGAL! This is why it is called nested else's. Which else's goes with which if's.
The way I formatted the code makes it obviouse, which is why we programmers code like that. Easy to READ. But what if they had no tabs.
How would you be able to tell which else goes with which if. It is simple, each else will go to the next closes if, only if it is not already
taken by a closer else. Other wise, it will go to the next one. Study the above code untiil you understand how it works.
//Notice This
if(boolean)
...code...
else if(boolean)
...code...
if(boolean)
...code...
else
if(boolean)
...code...
//Both of these mean the same thing. White spaces are not inportant.
if (boolean) {...code...} else if(boolean) {...code...}
//This is a legal expression, but as you can also see, very hard to understand. Not recomended, at ALL.
//Please Don't DO THIS.
//If fact, your entire program can be in ONE LINE!!!!!!
Looping:
Repeating the same code over a number of times. You do this when you are want to do the same thing over and over again. For example, if you wanted to
print out 'Hello World" 1000 times, it is very tedious to write the same code over and over again. It is easier to just write it once and tell the
computer to repaet it a certain number of times. Example of Looping in C++:
for(int i=0;i<10;i++)//Setting the initial value for the incrementor, the condition that must be met in order
//For the Loop to continue, and incrementing the loop counter.
{
cout << "Hello World" << endl;//The statement that will be repeated a number of times.
}
Examine the code and see if you can tell how it works.
There are several types of loops: The For Loop, While Loop, and Do While Loop. Technically, they are all the same. You really don't need more than one, like the switch statement, it just makes it slightly simpler in some cases to choose one over another, but you don't to. In fact, I really just use the For
Loop. In very rare cases do I use the while loop, and I NEVER used the do while loop.
The difference between each loop simple, the while loop has the following syntax:
while(boolean)//Loop goes if boolean is true, terminates when it is false.
{
...code...
...some sort of incrementor that will eventually end the loop...//Otherwise the loop will never end, of wourse you have to
//set an initial value somewheer outside this loop
}
/**************/
int i=0
while(i < 10)//When i is greater than or equal to 10, exit the loop.
{
cout << "Hello World" << endl;
i = i + 1;//This code is used very often, so the creators of C++ created a short way to represent this: i++;
//Notice this and the language name: C++. That name was derived with that.
//C++ is 1 level higher than the original C, so C + 1 or C = C + 1 or C++!!! :-)
}
The Syntax for the For Loop:
for(initial value;boolean;incrementor)
{
...Code...//In this loop, the incrementor code is put at the location above that reads incrementor.
//The Condition is put in the field read boolean
//The initial value, well, if you don't get it by now, you don't have the brain power to be a programmer
}
/*****************/
for(int i=0;i<10;i++)//All of these loops do the same thing, can you see what they do?
{
cout << "Hello World" << endl;
}
The for loop incorporates all the neccessary things into one line. Thus, is the most commonly used loop. I won't bother with the do loop,
it is useless. the difference between it and the while is that the do loop executes code first, then checks the condition
whereas the For and while loops check conditions before execution.
It is possible to break out of a loop. What if you want to do a statement 1000 times as long as another condition is meant. example:
for(int i=0;i<1000;i++)
{
if(...Some Value... is not stupid)
i = i + ...Some Value...
else
{
break;//This statement forces the loop to exit.
}
}
This code will keep looping until it hits 1000 or the condition in the if statement is false. In case you didn't read the above code, the statement
to exit a loop is 'break'. This statement can be used to exit any loop.
Methods/Function Calls:
A Method/Function call takes you from one part of your program to another. Since by now, you should know that methods and functions are the same,
I will call them functions only. You can think of a function like a function in math. You inout some kind of value, or values, and the
function output some sort of value.