C Programming Tutorial 58 - Multiple-If Vs Else-If

preview_player
Показать описание


~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

🅑 Bitcoin - 3HnF1SWTzo1dCU7RwFLhgk7SYiVfV37Pbq
🅔 Eth - 0x350139af84b60d075a3a0379716040b63f6D3853
Рекомендации по теме
Комментарии
Автор

you got a new mic and everything look at that come up!! its been really great going through these videos watching slight variations that occur with time

ColbusMaximus
Автор

If the AGE is less than 18 for the IF condition, it will never be greater than or equal to 65 for the ELSE-IF. If the age IS greater than/equal to 18, the ELSE-IF will also not be run because the first condition was true.

Sorthious
Автор

Relatable explanation dude! Thanks! I right away hit the subscribe button!

darshanasapkal
Автор

Caleb you're right im here bcoz I'm not paying attention in the class HAHHAHAHA keep it up men I'm really learning I'd rather watch your tutorials than to pay attention in class

paulchristiangutierrez
Автор

First off, these are great videos, and the course itself overall is outstanding. Compared to any of the others out there this is hands down the best set of instructional videos that I have come across.

That being said, there is a bit of a disconnect, of sorts, starting with the coding challenge video which is tutorial44. It appears that there was some time that passed between tutorial43 and the rest of the videos and the overall teaching isn't quite the same standard of explanation and preparedness with the earlier videos, and this video is a good example of this.

For this code to work properly you have to have a comparison of some sort in the first if() statement such as:

if(input is >= 18 && < 65)

If you go back to the tutorials explaining the fundamentals of boolean logic and follow along then this becomes apparent; there must be a comparison in that first if() statement. If you try to get this to work in the manner that Caleb has up on the board, it just wont.

I noticed some comments here in which others found this out, so here's the method that I used to get this to work. It's fairly straight forward but uses a comparison in the first if() statement in order to determine which branch in the program the input is fulfilled as True.

The first if() statement needs a comparison such as:

if(input >= 18 && input < 65)

The else if() would need to look something like:

else if(input >= 65)

This would provide the person with the senior discount and not the standard adult discount. There is likely a different manner to code it, but this works for a demonstration.

a further else() statement can be used if the persons input does not meet the requirements for a discount based upon their age.

A good way to test this would be to put the appropriate printf() and scanf() statements with the if(), else if() and else statements such as:


printf("How old are you? "); //asks for user age
int input;
scanf("%d", &input); //stores user input into input variable

if(input >= 18 && input < 65) //compare user input to determine discount if any

{
printf("You get an over 18 discount\n"); //if user input was T for first condition
}else if(input >= 65)

{
printf("You get a senior discount.\n"); //if user input was T for second condition
}else
{
printf("Sorry but you don't qualify for a discount.\n"); //if neither condition was met; under age of 18
}

Doing it this way by comparing the value of the user input in the first if() statement will show the appropriate discounts applied with the the appropriate printf() statement.

ronpipes
Автор

I try to write crystal-clear conditionals, but sometimes things get a bit…iffy.

But in all seriousness, thanks again so much for making and sharing these wonderful videos. Keep up the amazing work!

PunmasterSTP
Автор

at the end when we use else-if
we need the expression for "if" to be ( age > 18 && age < 65) for the else-if to trigger if user inputs age above 65, because if we don't do that then anything greater than 65 is also greater than 18 hence, fulfilling the condition for "if" and the else-if will never get the chance to get executed

avocadolimbu
Автор

what if I wanted an age and a grade for example ( grade out of 20 )

Snecky
Автор

CAN we use more than 2 independent ifs ?

shashipalsaini
Автор

okay I know how to solve it now, by putting if(input >=65) first, not second - else if (input>=65)
if(input >= 65 )
{
printf("Senior Discount\n");
}else if(input >= 18 )
{
printf("Adult Discount\n");
}

rittenbrake
Автор

Caleb Curry thank you very much from Ukraine. Love your tutorial. God bless.

impresso
Автор

hey Caleb

how can I only get "Senior Discount"
I tried the code, it's only counted as" Adult Discount" when i type any number >=65(for example 67)

rittenbrake
Автор

Suppose I m using two choices if ( ch==1) printf ("yes"); if ( ch==2) printf ( "no") ; what is the difference if I use else if?

priyanjali
Автор

I need to stop procrastinating on coding, any advise on how to get motivated to push my coding?

morapedimasima
Автор

If you are 70, than first statement is true and else if is not evaluated. Than you are simply an adult not seniour. What am I midding here?

fatihbilgeylmaz