Hey! This is about using the conditional “if”. First things first we need to know what is it for. The conditional If is used when you want to give the program command a condition. This means that when you enter some kind of information to the program, it gives back a response depending on it´s condition.

 

Conditionals have their own symbology, here are the basics you need to know in order to begin with conditional coding.

 

> Greater than

>= Bigger than or equal to

! = Unequal to

== Equals to

 

What is the structure of an If statement?

 

To program something using the conditional “If” you have to begin with…

if (true statement) execute the next.

The true statement refers to a condition you choose to be done if what is introduced fulfills the condition. The true statement has to be always in between parenthesis. If what is introduced is true, the program proceeds to execute what is given to the statement. For example:

x = 1

If(x>o) {

cout

 

If you ran the program with this conditional, the resulting action would be displaying the string given, because 1 fulfills the condition 1>0.

 

If the condition is not true, then we would prefer to use the else option. What does else do? Else does the action of the false response. If what is introduced to the program doesn’t fulfill the true condition, then the else would be executed. For example:

x = -1

If(x>o) {

cout

 

else {

cout

 

If you ran this program with this conditional, the resulting action would be displaying the string given in the else command. This because -1 does not fulfill the true condition so the else command is executed.

 

For example, imagine you are doing a program for an ice-cream store. This store has two ranges of flavor which differ in price. If you are getting a flavor from group number one, the price of each cone is $20.00, but if you are getting a cone with flavor from group number two, the price of each cone is $30.0.

 

You would, first, need an action for the cashier to input in which group of  flavor the ice cream is, and then use a conditional to display which type of price is it depending on the type of ice-cream. Here is the example code:

 

 

WATCH MY VIDEO https://youtu.be/Q6ysIRGGoaQ

 

CC BY 4.0 #Mastery15 – Use of the conditional “if” by Ricardo Newton is licensed under a Creative Commons Attribution 4.0 International License.