Use of the conditional “if” Use of “else” with a conditional
Use of conditionals in python 3.
Use of conditionals in python 3.
Hello! Here is my #Mastery16 that is about using else in conditionals. An else statement is used on conditional to set the false part. If a value that is introduced does not fulfill the true condition, there is when the else takes part in the process. This is used to have a response to a false condition.
For example:
If you want to create a simple program for grading, that describes when a student is approved or has failed the semester, this program would have to make the sum of the three partials and calculate the average. Let´s say the approbatory grade is 70. To start, you can begin it in many ways. I would choose to do it with an if condition.
The goal here is for the program to print the phrase approved or failed depending on the sum of the partial grades. This program, of course, to proof the use of the else statement.
To begin, we would have to type what we regularly do in all of the coding processes.
using namespace std;
Then, we would have to begin with the input of each one of the partials. For the program to get the average of the three and then with an if condition, tell if the student is approved or has failed.
The code would be:
Remember that to use an else statement you have to make use of the conditional with the true condition. Al else has to follow a conditional to work. When you write an else, the response has to be following the else in between braces, this helps the condition to realize what to execute when the false condition is applied. For example:
If(average>=7){
cout
}
else {
cout
}
Remember to always make use of the correct punctuation, spaces and symbols for the code. The semicolon is very important to be included at the end of each line (That is my most common mistake).
Here is the running program. As you can see, I used the else statement to print the string when the student failed.
Tip: if your program does not work when you are trying to compile do not freak out, the compiler will try to tell you what he doesn’t understand. Here is an example of an error, the numbers show the line and the number of character in which the error is placed.
The compiler says there is a semicolon missing in line 5 space 31…
Remember to check #Mastery15 to learn how to use if conditionals.
96
Normal
0
21
false
false
false
ES-TRAD
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Tabla normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:Calibri;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-fareast-language:EN-US;}
WATCH MY VIDEO https://youtu.be/951plPJV2VE
The IF statement is used for conditional execution. What you do in IF is that you write a condition (a specific case) so when the program encounter that conditional it will do the command or function you gave to him (in my example if Z is bigger than X, in that case the program will […]
“If” is really simple. You write a parameter with variables such that if thats true, the program does something. The “else” makes it so that of the parameters of the conditional aren’t met, the program does something else. And an “elif” is like an “else” but with a conditional. Here’s an example.
Here is my SECOND video about the masteries.
Hope it helps.
It’s about:
- Use of the conditional “if”
- Use of “else” with a conditional
Hey! This is #Mastery15 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
96
Normal
0
21
false
false
false
ES-TRAD
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Tabla normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:Calibri;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-fareast-language:EN-US;}
La forma más simple del condicional if: Lo que se encuentra entre () después de if es la condición, si esta se cumple aparecerá la frase que se pone a continuación con COUT. Agregar iostream, namespace, int main. Dar un… Continue Reading →