Hello everybody, this post is to explain the basic use of while loops and for loops.
While is a loop that will be performed if the conditions inside its definition is true. Yes, you are right… again boolean value. If the condition inside the while returns the value true then the code under the while is performed.
For loop are more classy and I love them. Once you understand how it is used, a for loop is good for almos everything. For loops works with the next principle, you have a numer, you add orr substract from that number until a condition is met. For example:
for(i = 0; i<N; i++)
In this example first part befor the semicolon means the initial value of i is equal to cero, then in the second part before the second semicolon this means that the loop is going to be performed until i stops being less than N, the last part before the last brace means that i is going to increment by 1 each time the cycle is completed.
In the next videos this situation is better explained, the first video is mine and the second is someone’s else video I found on Youtube:
Maybe it is much better explained in this second video… just maybe
P.S.: Remember the words of the second’s guy video: a loop repeats a block of code, we are going in circles for a certain number of times.
]]>This post is for mastery points 15 and 16:
So this time it is about conditional if and using else with a conditional
As you may know, an if statement is a fairly simple conditional, yet you can do very interesting things with it. It’s different from a for cycle because with the if condition you only write one parameter. If the parameter is true then the condition is met and the programm performs the piece of code under de condition.
Using else is useful when you have two situations. If the condition is not met in the previous if, then the code under de else statement is going to be performed. As I said, the if statement works with true or false, therefore you could say that an if statements works based on boolean algebra.
The following video is me talking a little about if statements and using else with it.
]]>I created a video explaining how to create a function and then call it later in your code. Believe me it’s really easy.
First you should know that a function is a predefined piece of code contained like in a capsule. Each time ou need the code you call the function and use the code inside of it. You can write your own functions or use the functions that are contained in the different C++ libraries. Â Another definitions for function could be:Â “a group of statements that is given a name, and which can be called from some point of the program”.
To create a function you need to write the type of data that its going to return, because functions have to return something. Then you have to name your function and say what its going to transform. That’s right, as you may have figured out, functions take something, perform a transformation and return something else. It’s like a little a machine.
Look at the following example:
// function example
#include <iostream>
using namespace std;
int addition (int a, int b) /*the first int means that this function is going to return an interger, the second and third int's mean that this function takes an int called "a"
another int called "b".*/
{
int r;
r=a+b;
return r;
}
/*in this part of the function you declare another variable, this is called a local variable because is used only in the function. The variable is going to store the value of the sum of "a" and "b". Then you need to say what is going to return the function, in this cases we return r becauce that is the value we want.*/
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}
References:
]]>I created a video explaining how to create a function and then call it later in your code. Believe me it’s really easy.
First you should know that a function is a predefined piece of code contained like in a capsule. Each time ou need the code you call the function and use the code inside of it. You can write your own functions or use the functions that are contained in the different C++ libraries. Â Another definitions for function could be:Â “a group of statements that is given a name, and which can be called from some point of the program”.
My video:Â http://youtu.be/jltHibh6GN4?hd=1
To create a function you need to write the type of data that its going to return, because functions have to return something. Then you have to name your function and say what its going to transform. That’s right, as you may have figured out, functions take something, perform a transformation and return something else. It’s like a little a machine.
Look at the following example:
// function example
#include <iostream>
using namespace std;
int addition (int a, int b) /*the first int means that this function is going to return an interger, the second and third int's mean that this function takes an int called "a"
another int called "b".*/
{
int r;
r=a+b;
return r;
}
/*in this part of the function you declare another variable, this is called a local variable because is used only in the function. The variable is going to store the value of the sum of "a" and "b". Then you need to say what is going to return the function, in this cases we return r becauce that is the value we want.*/
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}
References:
]]>