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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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