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.
WATCH MY VIDEO https:/ / youtu.be/ 951plPJV2VE
#Mastery16 – Use of “else” with a conditional by Ricardo Newton is licensed under a Creative Commons Attribution 4.0 International License.