Conditionals

Conditionals are choices the computer makes. They are pretty similar to if-then statements you would learn in geometry or even English. They are very useful. If you wanted the computer to do something under a certain condition, you would write code like in the example. Conditionals perform different actions or computations depending if the condition evaluates true or false. If-then statements lines are printed with no semicolon at the end but the body of the if-then statements are printed with semicolons.
 * __Defined__**

Conditionals always start with the word "if". If the condition is true, then the action within the body is applied producing the appropriate results. More than one action can be applied, as long as the multiple actions are within enclosed brackets ( { } ). If only a single action is being called, then brackets enclosing the line is unnecessary. If the condition is false, then the else-action ("else if" or simply "else", which is also in the body, is applied producing the appropriate results. If statements only evaluate if a function is true, then it just proceeds to the next condition or line of code.

__**Compound If's**__ Compound If's allow for multiple conditions to be tested for a true/false condition. Combining conditions makes for the use of "or" ( || ) and "and" ( && ) in the if-then statement.

examples: code if (puppy == dog || kitty == cat); //returns true if either puppy is a dog OR kitty is a cat code code if (puppy == dog && kitty == cat); //returns true is both puppy is a dog AND kitty is a cat code code { if (you <= 2 || me <=2) //returns true is you is less than or equal to 2, or me is less than or equal to 2 {   return 0;	// returns 0 if the above statement is true } else	//if the above statement is false, the following occurs if (you >=8 || me >=8)	//returns true if you is greater than or equal to 8, or me is greater than or equal to 8 {   return 2;	//returns 2 if the above statement is true } else {   return 1;	//returns 1 if all of the statements above are false } }

code

1. if (**a** == **b**) //are **a** & **b** equal? 2. if (**a** > **b**)// is **a** greater than **b**? 3. if (**a** < **b**) //is **a** less than **b**? 4. if (**a** >= **b**)// is **a** greater than or equal to **b**? 5. if (**a** <= **b**) //is **a** less than or equal to **b**? 6. if (**a** != **b**)// is **a** not equal to **b**? 7. if (**a** && **b**) are __**a AND b both true**__? 8. if (**a** || **b**) is __**a OR b true**__?
 * __Types of Comparisons__**

If-then statements require you to put 2 equal signs (==). Putting 1 equal means you would like to store whatever it is on the right of the equal sign into whatever it is on the left of the equal sign.
 * __Things to Remember__**

code if (a == b); code
 * example:**

! = not (negates said action) (in geometry: ~ = not) when there is ! in an if statement you need to distribute it if its in the outside.

code if(a > !(b && c) || !(b < a));
 * example:**

is the same as:

if(a > (b || c) || (b > a)); code

code if (some kind of comparison or some true/false) {     something you want done; }
 * __Examples__**

code

code format="java" public void onMouseDrag(Location Point) {  if(circle.contains(point)) {     circle.move(0,-5); } } code You can put two conditionals together. For example, if you wanted the computer to do something if two things were true at the same time you could put an "and" in between two statements, which is written in java as &&. Or, if you wanted the computer to do something if either of two statements were true, you could put an "or" in between the two statements, which is written in java as ||.
 * __"And" and "Or"__**

code format="java" if( x<= 40 && x>= 10) {  return true; }

//in this code, you want the computer to return true if x is both less than or equal to 40 AND greater than or equal to 10.

if( x == 20 || x == 15) {  return true; }

//in this code, you want the computer to return true if x is either equal to 20 or equal to 15. code -Switch statements can take the place of many consecutive if statements -When doing conditionals, else is the default (when nothing else works) -Many people neglect the fact that return __(stuff)__ can be used instead of an if statement at times - >= asks if first variable is greater than or equal second variable - <= asks if first variable is less than or equal second variable - comparisons of less than/equal to or greater than/equal to made that deviate from said symbols will lead to an error
 * __Very nice things to remember__**

What value would the if statements value inside the parenthesis be true or false? (Answers are below each chunk of code)
 * __Practice Problems__**

code if(7 < 17); code

True
code int x = 9;

if((9 / 2) == 4); code

code boolean state = !true; if (state
true) code======