DIR Return Create A Forum - Home
---------------------------------------------------------
The New DS computer forum!!
HTML https://dscomp.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: C++
*****************************************************
#Post#: 409--------------------------------------------------
Tutorial #7 - If Statement, Else Statement
By: Hondaman Date: December 11, 2010, 8:47 am
---------------------------------------------------------
[center]Welcome to Tutorial No.7
In this tutorial you will learn how to use IF and ELSE
statements.[/center]
[font=trebuchet ms]A quick description -[/font]
For a program to make it's own decision you will need to use an
IF statement.
What an if statement does is it checks what something is and
makes a decision based on the value of a variable etc...
In this tutorial I will be showing you how to use IF statements
with the "End Program" script.
But for this script to be-able to test if you want the program
to end or not, It will need to know what to do if you don't want
the program to end.
This is where the ELSE statement comes into play...
The else statement is literally the program checking what else
it is to do if the statement doesn't match the IF statement.
This is the layout for the IF - ELSE statement -
[code]
if(statement)
{
output if this statement is true.
}
else
{
output if the above statement isn't true.
}
[/code]
[font=trebuchet ms]The "End Program" Script -[/font]
The "End Program" script uses an IF - ELSE statement to check IF
the user wants to end the program,
and the ELSE statement is what the program does if the user
doesn't want the program to end.
First you will need to setup your script
[code]
#include <iostream>
using namespace std;
int main()
{
system ("pause");
return 0;
}
[/code]
Now, to use a IF statement all you have to do is type
"if", at the start of a line and then the statement to which it
has to follow.
But for this statement we will need a variable and user input
into the variable to ask the user if he/she wants to end the
program.
An appropriate name for this variable would be "script_End".
So make the variable a 'string' variable using the following
code
[code]
string script_End;
[/code]
And now we need the question, So the question will be "Would you
like to exit the program? yes/no"
And of-course the user input... "cin >> script_End;".
So now your script will look like this,
[code]
#include <iostream>
using namespace std;
int main()
{
string script_End;
cout << "Would you like to exit the program? yes/no" << endl;
cin >> script_End;
system ("pause");
return 0;
}
[/code]
In the question we added at the end "yes/no" this is what the
user will enter,
So the IF statement will check if the user entered "yes" and end
the program or,
If the IF statement didn't check out, the ELSE statement will
run other code so the program will continue to run.
In a later tutorial I will use this program in a loop so the
user can choose an option to exit and do that as many times as
they want,
but in this script the program will only run once and there-fore
it will not seem as exiting as the "End Program 2.0" script.
Now we have a variable setup and user input to check if the
script is to end or not, we only need to code the IF and ELSE
statement now.
In the IF statement, we will write "if(script_End == yes)"
This means if the variable "script_End" is equal to yes (a
single equals sign '=' is used for maths operations, '==' means
'equal to') then the program will output whatever you put in the
curly brace's {} is what will be run,
If the user inputs anything other than "yes" (we still use no so
the user won't get confused)
then whatever is in the curly brace's after the ELSE statement
is run.
Here is the code
[code]
#include <iostream>
using namespace std;
int main()
{
string script_End;
cout << "Would you like to exit the program? yes/no" << endl;
cin >> script_End;
if(script_End == yes)
{
cout << "Thanks for using this program" << endl;
}
else
{
cout << "This program will end anyways but if there was more
after it, you the script would continue." << endl;
}
system ("pause");
return 0;
}
[/code]
And that is it,
In a later tutorial you will see the looped version of this
script "End Program 2.0" and this script will make a lot more
sense.
Thanks for reading this tutorial.
If you see any problems with this tutorial, Please PM me.
Hondaman.
[center]<< Previous
HTML http://www.dscompforums.tk/c/tutorial-6-your-first-proper-game-lost-fortune-part-3/<br
/> | Next >>
HTML http://www.dscompforums.tk/c/tutorial-8-if-statements-within-if-statements/[/center]
*****************************************************