C++ Tutorial: Conditions and Loops

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:

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
}
which can also be written like so (if only one action is done per if statement):

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())
Conditional operators are as follows:
  • == (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
Another way of writing an if statement is with a switch statement (less efficient until you get into high ammounts of else ifs in one if statement) , ie:
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;
}
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:
Code:
int x = 0;
while (x < 5)
{
    cout << "X is equal to: " << x << ".\n";
    x++;
}
which should output the following:
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";
}
do while loops follow this format: do { actions } while (bool is true);, ie:
Code:
int x = 0
do {
    cout << "X is equal to: " << x << ".\n";
    x++;
} while (x < 5);
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:
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;
}
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
well i know the VC++ library has the System::String class which can handle unicode characters and automatically manages them for you, im not sure if the standard C++ library std::string class can do this but i believe it's parent class can, as its just a template class, ill look into it

Edit:
The std::string class handles utf8 the same way wc3 does (well almost) and it is possible to use std::basic_string<wchar> (wide char) for storing unicode but when using unicode-only characters it will cause errors in the display, apparently the boost library can handle the conversion from a wchar string to read-able unicode text
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    Happy Sunday!
    +1
  • The Helper The Helper:
    I will be out of town until Sunday evening
    +1
  • The Helper The Helper:
    I am back! Did you miss me LOL
    +1
  • jonas jonas:
    where did you go?
  • The Helper The Helper:
    Jefferson TX on a Paranormal Investigation of a haunted bed and breakfast - I got some friends that are paranormal investigators and they have an RV and do YouTubes
    +1
  • The Helper The Helper:
    It was a lot of fun. The RV was bad ass
  • jonas jonas:
    That sounds like fun!
    +1
  • The Helper The Helper:
    it was a blast!
  • The Helper The Helper:
    I am going to post the Youtube of the investigation in the forums when it is ready
    +1
  • jonas jonas:
    cool!
  • vypur85 vypur85:
    Sounds cool TH.
  • tom_mai78101 tom_mai78101:
    I was on a Legend of Zelda marathon...
  • tom_mai78101 tom_mai78101:
    Am still doing it now
    +1
  • jonas jonas:
    which one(s) are you playing?
  • jonas jonas:
    I played a little bit of the switch title two weeks ago and found it quite boring
  • The Helper The Helper:
    just got back from San Antonio this weekend had the best Buffalo Chicken Cheesesteak sandwhich in Universal City, TX - place was called Yous Guys freaking awesome! Hope everyone had a fantastic weekend!
    +1
  • The Helper The Helper:
    Happy Tuesday!
  • The Helper The Helper:
    We have been getting crazy numbers reported by the forum of people online the bots are going crazy on us I think it is AI training bots going at it at least that is what it looks like to me.
  • The Helper The Helper:
    Most legit traffic is tracked on multiple Analytics and we have Cloud Flare setup to block a ton of stuff but still there is large amount of bots that seem to escape detection and show up in the user list of the forum. I have been watching this bullshit for a year and still cannot figure it out it is drving me crazy lol.
    +1
  • Ghan Ghan:
    Beep boop
    +1
  • The Helper The Helper:
    hears robot sounds while 250 bots are on the forum lol
  • The Helper The Helper:
    Happy Saturday!
    +1
  • The Helper The Helper:
    and then it was Thursday...
    +1

    The Helper Discord

    Members online

    No members online now.

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top