SkidForums


Where skids go hackers
 
HomeFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 C++ Control Flow tutorial.

View previous topic View next topic Go down 
AuthorMessage
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: C++ Control Flow tutorial.   Fri Apr 27, 2012 9:39 am

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;
}
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: C++ Control Flow tutorial.   Fri Apr 27, 2012 10:05 am

Thanks man ^^ great tutorial Smile!
+ repped
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: C++ Control Flow tutorial.   Fri Apr 27, 2012 3:18 pm

No problem. I'll post another one soon on switch.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: C++ Control Flow tutorial.   Fri Apr 27, 2012 9:37 pm

sure , would it be an idea i sticky a post from you with links to all tuts :p?
thanks for these pro tuts Very Happy
looking forward to next one study
PS: nice sparkles , and since 1 min ago also a yellow glow Smile
and i added a blackhat smiley Very Happy blackhat
Back to top Go down
View user profile http://skid.forum-leaks.com
Rzdude
Beginner
Beginner


Posts : 78
Points : 110
Reputation : 0
Join date : 2014-06-27

PostSubject: Re: C++ Control Flow tutorial.   Fri Jun 27, 2014 5:41 am

Thank u very much
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: C++ Control Flow tutorial.   

Back to top Go down
 
C++ Control Flow tutorial.
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» problem with the game : no control over the moves of the pc
» Great Tank and Weathering tutorial
» Power Sword Tutorial (with more to follow)
» Instant Mold Tutorial
» good c++ tutorial series

Permissions in this forum:You cannot reply to topics in this forum
SkidForums ::   :: Coding :: Coding :: C/C++-
Jump to: