GFreak45
I didnt slap you, i high 5'd your face.
- Reaction score
- 130
This tutorial uses: Functions and Variables and Getting Started
Conditions:
Conditions are essentially a question, with a true or false, yes or no answer. For instance, if the user says yes, then do this, else do something else. Variables/Functions returning a true/false are called booleans or bools for short. As well as the bool type, other types are used as conditions as well, if the byte-makeup of a value isnt equal to 0 then it returns true, otherwise it will return false. Common uses of conditions are if statements, ie:
which can also be written like so (if only one action is done per if statement):
Another version of an if statement would be like so (if the return type of true and false actions are the same):
Conditional operators are as follows:
Note the breaks, those are required in switch statements, otherwise the next option will be run as well, for instance, if you delete the break just after case 1, then input 1, it will cout:
You input 1.
You input 2.
Switch statements check the variable they are called with for a value, and it will run based on the case that matches it, and unfortunately cases must be a constant value like 1 or 5, not a variable.
Loops:
Conditions are also used in loops, there are 3 commonly used types of loops, for loops, while loops, and do while loops.
while loops follow a format like so: while (bool is true) { do actions }, ie:
which should output the following:
for loops follow this format: for (variable declaration; bool is true; action at end) { do actions }, ie:
do while loops follow this format: do { actions } while (bool is true);, ie:
The primary difference between a do while and a while loop is that a do while is run at least once regardless of the while condition.
The output for all of these loops should be the following:
X is equal to: 0
X is equal to: 1.
X is equal to: 2.
X is equal to: 3.
X is equal to: 4.
To wrap this up, lets write an application that requests a number from the user (1-100), then outputs whether the number is less than 50 or greater than 50, to challenge yourself allow the user to choose whether they want to continue by selecting 0.
This is how I would do it:
Conditions:
Conditions are essentially a question, with a true or false, yes or no answer. For instance, if the user says yes, then do this, else do something else. Variables/Functions returning a true/false are called booleans or bools for short. As well as the bool type, other types are used as conditions as well, if the byte-makeup of a value isnt equal to 0 then it returns true, otherwise it will return false. Common uses of conditions are if statements, ie:
Code:
if (some bool variable)
{
do stuff for variable being true
}
else if (some other bool variable)
{
do stuff for other variable being true
}
else
{
do default option
}
Code:
if (some bool variable) do stuff for variable being true;
else if (some other bool variable) do stuff for other variable being true;
else do default option;
Another version of an if statement would be like so (if the return type of true and false actions are the same):
Code:
(some bool ? if true actions : if false actions)
ie:
(boolFunction() ? trueFunction() : falseFunction())
- == (equals) - if 2 values are equal to each other this returns true
- != (not equal) - if 2 values are not equal to each other this returns true
- !<bool value> (not) - if not true return true, if true return false
- > < (Greater than/Less than) - if the first value is greater than/less than the second return true
- >= <= (Greater than or equal to/Less than or equal to) - if the first value is greater than or equal to/less than or equal to, return true
- && (and) - if left side and right side of this operator are true, returns true
- || (or) - if the left side or right side of this operator are true, returns true
Code:
unsigned int x = 0;
cin >> x;
switch (x)
{
case 1:
cout << "You input 1.\n";
break;
case 2:
cout << "You input 2.\n";
break;
case 3:
cout << "You input 3.\n";
break;
case 4:
cout << "You input 4.\n";
break;
case 5:
cout << "You input 5.\n";
break;
default:
cout << "You input greater than 5.\n";
break;
}
You input 1.
You input 2.
Switch statements check the variable they are called with for a value, and it will run based on the case that matches it, and unfortunately cases must be a constant value like 1 or 5, not a variable.
Loops:
Conditions are also used in loops, there are 3 commonly used types of loops, for loops, while loops, and do while loops.
while loops follow a format like so: while (bool is true) { do actions }, ie:
Code:
int x = 0;
while (x < 5)
{
cout << "X is equal to: " << x << ".\n";
x++;
}
for loops follow this format: for (variable declaration; bool is true; action at end) { do actions }, ie:
Code:
for (int x = 0; x < 5; x++)
{
cout << "X is equal to: " << x << ".\n";
}
Code:
int x = 0
do {
cout << "X is equal to: " << x << ".\n";
x++;
} while (x < 5);
The output for all of these loops should be the following:
X is equal to: 0
X is equal to: 1.
X is equal to: 2.
X is equal to: 3.
X is equal to: 4.
To wrap this up, lets write an application that requests a number from the user (1-100), then outputs whether the number is less than 50 or greater than 50, to challenge yourself allow the user to choose whether they want to continue by selecting 0.
This is how I would do it:
Code:
#include <iostream>
using namespace std;
int main ()
{
int number = 0;
cout << "Pick a number (1-100): ";
cin >> number;
while (number)
{
if (number <= 50) cout << "The number is less than or equal to 50.\n";
else cout << "The number is greater than 50.\n";
cout << "Pick a number (1-100): ";
cin >> number;
}
cout << "\n";
system("Pause");
return 0;
}