Presentation is loading. Please wait.

Presentation is loading. Please wait.

& Decision Making Algorithms BY: Aakash Indurkhya.

Similar presentations


Presentation on theme: "& Decision Making Algorithms BY: Aakash Indurkhya."— Presentation transcript:

1 & Decision Making Algorithms BY: Aakash Indurkhya

2  The if statement allows for a boolean condition to be set on a section of code. The else statement requires an if statement to before it. The else statement offers the replacement code if the boolean condition is not true.

3 SOURCE CODEPSUEDO-CODE int num = kb.nextInt(); if(num % 3 == 0){ num++; } num is a variable set by a user if num is divisible by three: increment num by 1

4 SOURCE CODEPSEUDO-CODE int num = kb.nextInt(); if(num % 3 == 0){ num++; } else{ num+= 5; } num is a variable set by a user if num is divisible by three: increment num by 1 Otherwise: increment num by five

5  You will find that Boolean algebra is easiest to understand when you talk it out to yourself.  Now you may begin to ask your self what if I want to have multiple conditions or two possible conditions.  There are several different ways to do all of this.  These topics involve nested if else statements, Boolean Algebra and De Morgan’s Law, and dangling else.

6  If statements can be nested such that another condition is part of the code that has already passed the original (or parent) condition. Let’s look at some code. int num = kb.nextInt(); // user entered number if (num % 2 == 0){ // if num is even if (num % 6 == 0){ // AND it is divisible by 6 num+= 4; // add for to num } else{ // OTHERWISE (meaning it is even but not divisible by 6) num = num*num; // square num }

7 if (num > 30){ if (num-1 >= 31){ System.out.println(num);} else{ System.out.println(“Not 31”); } } // It is important not to be fooled by the indentation. The else is attached to the second if because else statements are attached to the last Finished if statement. This is why you should always indent with care.

8  This is a very handy thing to remember.DeMorgans Law literally translates to "De Morgan's laws are rules relating the logical operators "and" and "or" in terms of each other via negation, namely:  NOT (P OR Q) = (NOT P) AND (NOT Q)NOT (P AND Q) = (NOT P) OR (NOT Q)" Much of this has to do with the Boolean Algebra behind our logic. Note that this topic is very if you say what you want to do before you start coding.

9  Here is a real example...while (distance -3.5) // this loop will go on as long as the person hasnt stepped off the bridge { int direction = rndm.nextInt(2);  // direction is a random number 0 or 1 if (direction == 1) // if direction = 1 distance++; // the person steps forward 1 foot if (direction == 0) // if direction = 0 distance--; // the person steps backwards 1 foot counter++; // number of steps goes up each time }

10  This was part of the random walk program and DeMorgans Law was used in the loop condition. while (distance - 3.5) states that the loop will continue while the distance is less than 3.5 and greater than -3.5. If you look at this statement on a number line then it will seem obvious why it is true, but in your mind you tend to think of things like that in terms of or (||).This is an important aspect of the conditions that you put on loops.

11 Switch statements are really useful when you have several different scenarios. Switch statements are great substitutes for lengthy if statements. Also, switch statements are great for menus in bigger programs. Here is the concept: switch (numeric case){ case (some number): do something; break; case (some other number): do something; break; default: do something; }

12 Scanner kb = new Scanner(System.in); int choice = kb.nextInt(); switch (choice){ case 1: playGame1; break; case 2: playGame2; break; default: // if choice is not described by any of the cases System.out.println(“Invalid Input”); }

13 Here are some good sources I found on these topics: http://www.lexjansen.com/pharmasug/2005/p osters/po25.pdf http://java.sun.com/docs/books/tutorial/java/ nutsandbolts/if.html www.cs.umd.edu/~clin/More Java /ControlFlow/ dangl ing.html http://java.sun.com/docs/books/tutorial/java/ nutsandbolts/branch.html


Download ppt "& Decision Making Algorithms BY: Aakash Indurkhya."

Similar presentations


Ads by Google