All right, when running a C++ program. The executable jumps to the main function and executes the code line by line.
So let's create our includes and our main function.
- Code:
-
#include <iostream>
using std::cout;
using std::cin;
int main()
{
return 0;
}
One thing to point out is, I did not use using namespace std; Since I will only be dealing with two objects from the namespace, cout, and cin.
If we were to compile and run that code it would execute just fine.
So let's create a seperate function.
- Code:
-
void function1()
{
cout << "Hello world! I've been called from the main function!\n"; // \n indicates begin a new line
}
int main()
{
function1();
return 0;
}
So the executable jumped to the main function (Skipped over function1()) and the main function called function1().
When function1() was executed line by line. It finished, and the main function began where it left off.
- Code:
-
void function2()
{
cout << "I will be called after function 1!\n";
}
void function1()
{
cout << "I will be called first! \n";
}
int main()
{
function1();
function2();
cout << "I will output this after both functions are called!\n";
return 0;
}
So you understand the basic idea. Now the code does not have to run line by line, it can be manipulated with if statements.
Let's say the computer had an integer which held a value, and we were to gues that value.
Now we're not going to get into random number generation, this is just a basic if statement.
- Code:
-
void Correct()
{
cout << "You got the correct number!\n";
}
void Incorrect()
{
cout << "Oh you got it wrong!\n";
}
int main()
{
int ComputerNumber = 5; // = is an assign operator. So we're assining the number 5 to ComputerNumber
cout << "Gues the computer's number!->"
int x; //This is our gues variable;
cin >> x;
if (x == ComputerNumber)
{
Correct();
}
else
{
Incorrect();
}
return 0;
}
Now the way an if statement works is we call if and inside the parentheses is our condition.
In this case our condition was if our guess = the computer's number.
Now notice I used ==?
- Code:
-
if (x == ComputerNumber)
The reason for that is because a single = is an assignment operator. We can assign values to variables.
An == is a comparison operator. So make sure not to confuse the two.
Now in this code.
- Code:
-
void Correct()
{
cout << "You got the correct number!\n";
}
void Incorrect()
{
cout << "Oh you got it wrong!\n";
}
int main()
{
int ComputerNumber = 5; //= is an assign operator. So we're assining the number 5 to the int variable ComputerNumber
cout << "Gues the computer's number!->"
int x; //This is our gues variable;
cin >> x;
if (x == ComputerNumber)
{
Correct();
}
else
{
Incorrect();
}
return 0;
}
If the variable we entered was 5 which is the value we assigned the ComputerNumber, then you can gues the function Correct(); was called. And the output would be You got the correct number! And if we guesed any other number (else) then Incorrect(); would be called.
Also if the condition was true meaning our gues did math the number, then every thing inside the { } that came after the if statement would be executed. So you can execute more than one line of code in an if statement, or an else statement.
Well I'm not trying to go too much in depth with this tutorial. But to give a basic understanding of how a c++ program is executed. Hope you enjoyed!
Here is the full snippit.
- Code:
-
#include <iostream>
using std::cout;
using std::cin;
void Correct()
{
cout << "You got the correct number!\n";
}
void Incorrect()
{
cout << "Oh you got it wrong!\n";
}
int main()
{
int ComputerNumber = 5; //= is an assign operator. So we're assining the number 5 to the int variable ComputerNumber
cout << "Gues the computer's number!->"
int x; //This is our gues variable;
cin >> x;
if (x == ComputerNumber)
{
Correct();
}
else
{
Incorrect();
}
return 0;
}