Conditionals

Conditionals

Requirements:
Linux Distribution
g++
any text editor

My Setup:
Debian 10
g++ version 6.3.0
pluma

Welcome back to another tutorial on C++! Today we will be going over
conditional statements, such as if/else if/else statements, as well as switches
and ternary statements. Conditional statements are used to determine what is
executed next. If a certain block of code needs to execute if a variable is a
specific value, then when the program runs, if the variable is the value, then
it runs. In essence, it is used to control the flow of logic within the program.
The first one we will look at is the if , if/else , if/else if/else statements:


/************************************************************************
if example
************************************************************************/

int x = 5;

if(x < 10){
  //execute this code if x is less than 10
}

//more code here

/***********************************************************************
if else example
***********************************************************************/

int x = 5;

if(x < 10){
  //execute this code if x is less than 10
}else {
  //execute this code if x is not less than 10
}

//more code here

/***********************************************************************
if/else if/else example
***********************************************************************/

int x = 5;

if(x < 10){
  //execute this code if x is less than 10
}else if(x > 10){
  //execute this code if x is greater than 10
}else{
  //execute this code of neither of those are correct.
}

//more code here

These are pretty straight fowrard. If the condition is checked to be true, then
the program will run through that block of code. You can have as many else if’s
as you need, however I recommend looking at what you need to do and determining
if if/else if/else statements are really what you need , or if you can use a
switch instead. Switches are generally used when you have a specific amount of
possibilities in which your code can go, all determined by the value of a single
variable. In these, you need to make sure you add a break to the end of each
case. They look like this:


int x = 5;

switch(x){
  case 1:
    //run if x == 1
    break;
  case 2:
    //run if x == 2
    break;
  case 3:
    //run if x == 3
    break;
   default:
    //run if any other number
  }

//more code here

In this example, you see the switch gets the variable passed into it, and then
you define what each case means via “case”. Keep in mind, there can not be
duplicate cases, the default case is optional, and will run fine if nothing is
placed there , that just means nothing will run if it hits that. The break
statements are optional, if you would like code to flow into another case, you
can omit the break statement and allow the code to continue. The break is just
used to terminate the switch sequence .Nesting switch statements is allowed, but
it does make the code a little more unreadable. And this is what I mean by
determining what you need to branch logic. In my opinion, if you have to check
a value and go based on that value, a switch case would work perfectly, however,
if what you need to do is determine boolean values, checking whether something
is true, maybe checking a number AND running another boolean check, then you
would most likely just use an if statement.
The last little thing i would like to go over is ternary operations. If you
have to check a boolean value to do one line of code, it maybe easier to just
use a ternary statement. It is written like the example below:


/************************************************************************
if example
************************************************************************/

int x = 5;
int y = 10;
int z;

if (x < y) {
    z = x;
}
else {
    z = y;
}
//more code here

/************************************************************************
Ternary example
************************************************************************/

int x = 5;
int y = 10;
int z;

z = (x < y) ? x : y;

As you can see the syntax goes: (condition) ? true : false;
In the next tutorial, I will be going over loops!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s