6 - wake me up inside
How's it going everyone? No Chin here, and today we're going to talk about If-Else statements. Basically, an If-Else statement is a lot like a real world If Else statement. Something like: If something is true, then do something. Otherwise, don't do anything.Unlike the other objects we used, the If statement uses its own set of curly-braces and parameters. Here's an example:
int main()
{
int x;
cin >> x;
if(x==1)
{
cout << "WHy" << endl;
}
return 0;
}
The parameters have to be set to something with the If statement, so in this case, we told the If statement that x has to be equal to 1 in order for it to output something, and if x is not equal to 1, it can't output anything. However, we can still get it to output something even if x is not equal to 1, and that's where the Else statement comes in. Unlike the If statement, the Else statement doesn't require its own parameters. It's basically just a continuation of the If statement. For example:
int main()
{
int x;
cin >> x;
if(x==1)
{
cout << "topkek" << endl;
}
else
{
cout << "get off my computer" << endl;
}
return 0;
}
Note that the "==" is intentional. One equal sign sets a variable, and two means that something is equal to another thing.
Now let's go back to our password program. Last time, we excluded the if-else statement, so the string could be set to anything and it would output the same result. Let's try it with the If-Else Statement:
int main()
{
string harambe;
cin >> harambe;
if (harambe=="harambe")
{
cout << "f" << endl;
}
else
{
cout << "get off my computer" << endl;
}
return 0;
}
You should see something like this if you got the password right (Top is password, bottom is output):
And something like this if you got the password wrong:
Keep in mind, this is a very basic password program so it has no method of keeping your personal documents safe.
That's all I have for now. Until then...
No comments:
Post a Comment