Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active February 6, 2023 13:54
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save udacityandroid/9d84adf75c536b436767 to your computer and use it in GitHub Desktop.
Save udacityandroid/9d84adf75c536b436767 to your computer and use it in GitHub Desktop.
Android for Beginners : If/Else Smoothie Quiz
int numberOfSmoothiesTillPrize = 10;
if (numberOfSmoothiesTillPrize > 9) {
Log.v("SmoothieActivity", "Congratulations, you get a free smoothie!");
numberOfSmoothiesTillPrize = numberOfSmoothiesTillPrize - 10;
} else {
Log.v("SmoothieActivity", "No free smoothie this time.");
}
Log.v("SmoothieActivity", "You currently have " + numberOfSmoothiesTillPrize + " out of 10 smoothies needed for your next free smoothie.");
@xbarakota
Copy link

C
SmoothieActivity: Congratulations, you get a free smoothie!
SmoothieActivity: You currently have 0 out of 10 smoothies needed for your next free smoothie.

@NatanshBajpai
Copy link

All those writing answers, you are going to miserably fail in all your future ventures cause you can't follow simple instructions written by the owner.

@ahmedalfalahi
Copy link

C

@SuhailAlmarri
Copy link

Good exercise to understand if statement step by step. thanks

@ahmedmofreh
Copy link

Congratulations, you get a free smoothie!
You currently have 0 out of 10 smoothies needed for your next free smoothie.

@shoooka
Copy link

shoooka commented May 15, 2018

good exercise

@novaDev315
Copy link

novaDev315 commented Jun 18, 2018

she said no answers :( ... , we now your a java master and you know the answer in sec... but keeeeeeeeeeeeeeeep it for your self dummy -_-

@Alouush
Copy link

Alouush commented Jun 21, 2018

why are you posting the answer anyway? its freaking annoying.

@ahossam88
Copy link

I solve it

@MohEbaid
Copy link

MohEbaid commented Jun 26, 2018

Nice code; It's ok

@Fury4hwxc
Copy link

thanks 👍

@bimbolanko
Copy link

Nice!

@dantj007
Copy link

Option C is the correct answer.
Quite tricky.

@bildo17
Copy link

bildo17 commented Jul 29, 2018

I think I spotted a problem with this code. Let's assume that numberOfSmoothiesTillPrize is 6 going by this code the final log will show a negative value since the if statement has numberOfSmoothiesTillPrize-10 instead of 10- numberOfSmoothiesTillPrize.

@emanhamad
Copy link

C

@anjin83
Copy link

anjin83 commented Sep 21, 2018

I think I spotted a problem with this code. Let's assume that numberOfSmoothiesTillPrize is 6 going by this code the final log will show a negative value since the if statement has numberOfSmoothiesTillPrize-10 instead of 10- numberOfSmoothiesTillPrize.

Remember that code inside of the { } after an if statement is only executed when the condition inside the if statement is true.

The code "numberOfSmoothiesTillPrize = numberOfSmoothiesTillPrize - 10" is only executed when the if clause is true. The if statement in this case is:

  if(numberOfSmoothiesTillPrize >9)

This statement will be true only when the value of the variable numberOfSmoothiesTillPrize is greater than 9. Therefore the code inside the if statement will only execute when numberOfSmoothiesTillPrize is 10 or higher. This is why it will never be assigned a negative value.

@Seliem997
Copy link

c

@AhmedMohammed11
Copy link

the correct answer is c

@Abdulmunaim
Copy link

Option C win

@waseemminhas
Copy link

Please do not post the answer to this quiz in the comments. This gives others a chance to learn on their own. Thank you for your cooperation!

I support you!

@SDIDSA
Copy link

SDIDSA commented Dec 4, 2019

if you think you fully understand the if/else syntax, take a look at this line of code that i have written for my app
int total = (5 + (whippedCream.isChecked() ? 2 : 0) + (chocolate.isChecked() ? 1 : 0)) * quantity;

@waseemminhas
Copy link

if you think you fully understand the if/else syntax, take a look at this line of code that i have written for my app
int total = (5 + (whippedCream.isChecked() ? 2 : 0) + (chocolate.isChecked() ? 1 : 0)) * quantity;

syntax is fine and you managed well the implementation of if else statement using tertiary operator but cannot comment on desire output that you expect. price that's 5 is hard coded, on whipped cream checked it will add 2 and on chocolate checked it will add 1 to price/5 and will multiply result with quantity. A good approach though!

@waseemminhas
Copy link

waseemminhas commented Dec 6, 2019 via email

@rajhritik
Copy link

Its the best Course that I found on "Android Development"....
All the udemys' Android Development courses are trash made by some random guys just to loot our money, with no knowledge and idea of what the heck even they are talking in the course.

@allouane
Copy link

allouane commented Jun 4, 2020

Option c

@hussam1998
Copy link

Salam awlad 9hab hh

@mixspark
Copy link

ok done

@Pcr-dev
Copy link

Pcr-dev commented Apr 2, 2022

I have some python skills, I feel it's easier to understand and migrating my old python skills into the java conditional statements. I realize that, after understand the sudo code it's easier to migrate over to any language. Thank You Udacity for make this course available for free.

@Harriskobia
Copy link

Option C is correct:

V/SmoothieActivity : Congratulations, you get a free smoothie!
V/SmoothieActivity : You currently have 0 out of 10 smoothies needed for your next free smoothie.

@adiupd123
Copy link

V/SmoothieActivity: Congratulations, you get a free smoothie!
V/SmoothieActivity: You currently have 0 out of 10 smoothies needed for your next free smoothie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment