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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top