Tutorials Navigation
C++ Beginners Tutorial: Conditionals.
Tutorial Name: C++ Beginners Tutorial: Conditionals.
Category: PC Tutorials
Submitted By: Nissan
Date Added:
Comments: 0
Views: 2,274
Related Forum: PC Building Forum
Share:
C++ Beginners Tutorial: Conditionals.
Its all very well having a program that can do a single, linear task, such as converting a length in metres to feet and vice versa, or saying Hello to a person, but it is very rare that you will come across a program that only does a linear task. Programs almost always use something called conditionals, statements which can branch the code into various different paths depending on whether a certain condition is true or not; a T-junction, if you will.
C++ conditionals take the form of an if statement; if something is true, execute one piece of code, if it is false, execute another piece. Take a look at the following code snippet for an idea:
int a = 1;
if(a < 2)
{
cout << "a is less than 2!\n";
}
Lines 1 and 5 should be obvious by now, but line 3 will need some explanation. This is an example of a simple if statement. It works by first taking the contents of the integer variable a, and checking to see if it is less than 2. If it is, then it will run whatever code is in the curly-brackets, if it is not then it will continue as if that piece of code was never there. We know that 1 (the value of a) is definitely less than 2, so if you were to compile and run this program you should see a is less than 2! printed onto the screen.
Here we have introduced another operator, the less than operator, <. If you are familiar at all with mathematics then the usage of this sign should be self-explanatory. If not, then you'll just have to learn it. Note that although < looks a lot like <<, they are nothing like each other, and are not related in any way!
Of course, C++ is a powerful language, and so it can do a lot more powerful things using conditionals. Here's another snippet of code:
int a = 1;
if(a < 2)
{
cout << "a is less than 2!\n";
}
else
{
cout << "a is not less than 2!\n";
}
For the most part it is the same as the previous example, except this time we have another statement, else. This goes together with the if to make a slightly more complicated conditional. This time, it is saying 'If the value of a is less than 2, then say so, but if it isn't, then say that instead.
So, if a is less than 2, it will print 'a is less than 2!' to the screen, but if it is not it will print 'a is not less than 2!'. This way, no matter the value of a, something will always be printed to the screen. If you are looking closely, you might realise that whilst the first piece of code will only be executed if a is less than 2, (1, 0, -99, -65536...) the second piece of code will be executed if either a is greater than 2, or it is equal to 2. You may be thinking 'Why does that matter?', but what if we wanted to say if a was less than, greater than, or equal to 2, but we only wanted 1 line to be outputted? You may say that that's being quite picky, but C++ says that it's easy:
int a = 1;
if(a < 2)
{
cout << "a is less than 2!\n";
}
else if(a == 2)
{
cout << "a is equal to 2!\n";
}
else
{
cout << "a is not less than 2!\n";
}
Before we go into the explanation of that new conditional, you will have seen that we have used another operator, the equality operator, or ==. One of the most common mistakes people make when learning C++ is getting the == operator confused with the =, but you must remember that they do completely different things; == checks if two things are equal, = sets two things to be equal. The awkward thing is that your code would still make sense if you swapped then around in a conditional, as C++ just treats it (mostly) like an ordinary statement.
Going back to the code, out program now goes like this; 'If the value of a is less than 2, then say so. If it is equal to 2, say that. If it is neither greater or the same, it must be lower, so say that instead. This may seem more complicated when written in plain English, but really it is quite simple; all it is is just an else statement like we saw before with another if tacked on the end.
As a summary, a C++ conditional starts with an if statement, then can have any number of else ifs afterwards, and one or zero elses. An important thing to remember is that once one of the conditions has been satisfied, then all of the rest of the checks will be skipped. If you want them all to be checked, whether one has been satisfied or not, then you can just do several single if statements one after the other.
Ratings
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,656)
- 02. How to: Matrix Numbers | Batch File(1,954)
- 03. How to Password Protect Files on Windows(865)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,879)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,243)
- 06. How to embed an image on TheTechGame(3,140)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(13,088)
- 08. Host bot lobbies! Full Tutorial!(11,610)
- 09. Unban yourself [Plutonium BO2](14,308)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,410)
- 11. Best Crosshair Settings for Valorant(6,550)
- 12. Othercide The Surgeon Boss Guide(2,568)
- 13. Othercide Remembrances Unlock Guide(4,517)
- 14. Othercide Beginners Tips and Tricks(2,734)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,881)
"C++ Beginners Tutorial: Conditionals." :: Login/Create an Account :: 0 comments