Post by Square*** on Jul 3, 2008 2:09:00 GMT -5
Hey guys,
This Tutorial will explain what If-statements are used for and how to use them, enjoy.
The ifstatements are used to execute a command when (If) something is true or has an special value.
Ill use a simple value-based if statement to explain this to you
Ok, first the code and then Ill explain
NOTE :If you dont get things in this tutorial, feel free to ask away.
NOTE: Be aware that you have to put the ;, >> and << correctly in C++ so you wont get any errors
As you can see in the code above, I let the console ask you to give in a number.
( NOTE :: the int-function you can see in the code is used to store numbers in (like 2, 4 or even -4) If you have questions about the int-function Ill explain that to you)
When you insert a number (Digit) the program will see if the number is bigger, smaller or equal to 0.
Because that is the command we gave the program:
When you will compile this program and you execute it, it will read the number you gave in and compares it to 0.
I gave the compiler the command to throw out a sentence if (if - statement) the number is bigger then 0.
Now thats simple.
But watch this, I also used a else-command:
This will tell the program that if the number isnt bigger then 0, it will throw out a sentence saying its smaller or equal to 0.
So this program can decide hisself if the input is bigger,smaller or equal to 0.
In fact its quite simple, you just have to get the point of it.
And I would like to help you with that. If this tutorial isnt clear or you just have a question, reply and ask away
Thanks for your time and I will release some other tutorial soon.
Regards,
Square
This Tutorial will explain what If-statements are used for and how to use them, enjoy.
The ifstatements are used to execute a command when (If) something is true or has an special value.
Ill use a simple value-based if statement to explain this to you
Ok, first the code and then Ill explain
NOTE :If you dont get things in this tutorial, feel free to ask away.
NOTE: Be aware that you have to put the ;, >> and << correctly in C++ so you wont get any errors
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Give in a number";
cin >> number;
If (number < 0)
{
cout << "Number is bigger than 0" << endl;
}
Else
{
cout << "Number is smaller or equal to 0" << endl;
}
cout << "Give in a number";
cin >> number;
}
As you can see in the code above, I let the console ask you to give in a number.
int number;
cout << "Give in a number";
cin >> number;
( NOTE :: the int-function you can see in the code is used to store numbers in (like 2, 4 or even -4) If you have questions about the int-function Ill explain that to you)
When you insert a number (Digit) the program will see if the number is bigger, smaller or equal to 0.
Because that is the command we gave the program:
If (number < 0)
When you will compile this program and you execute it, it will read the number you gave in and compares it to 0.
I gave the compiler the command to throw out a sentence if (if - statement) the number is bigger then 0.
Now thats simple.
But watch this, I also used a else-command:
Else
{
cout << "Number is smaller or equal to 0" << endl;
}
This will tell the program that if the number isnt bigger then 0, it will throw out a sentence saying its smaller or equal to 0.
So this program can decide hisself if the input is bigger,smaller or equal to 0.
In fact its quite simple, you just have to get the point of it.
And I would like to help you with that. If this tutorial isnt clear or you just have a question, reply and ask away
Thanks for your time and I will release some other tutorial soon.
Regards,
Square