& decision making algorithms by: aakash indurkhya
TRANSCRIPT
BRANCHING& Decision Making Algorithms
BY: Aakash Indurkhya
The If/else statements
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.
Breaking it down
SOURCE CODE PSUEDO-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
Looking at else
SOURCE CODE PSEUDO-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
Analyzing
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.
Nested Statements
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 numberif (num % 2 == 0){ // if num is even
if (num % 6 == 0){ // AND it is divisible by 6num+= 4; // add for to num}else{ // OTHERWISE (meaning it is even but not divisible by 6)num = num*num; // square num}
}
Dangling Else
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.
De Morgan’s Law
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.
De Morgan’s Cont. Here is a real example...while (distance < 3.5 && 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
}
De Morgan’s Cont.
This was part of the random walk program and DeMorgans Law was used in the loop condition. while (distance < 3.5 && 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.
Switch Statement
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;
}
Switch example
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 casesSystem.out.println(“Invalid Input”);
}
Questions:
Here are some good sources I found on these topics:
http://www.lexjansen.com/pharmasug/2005/posters/po25.pdf
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html
www.cs.umd.edu/~clin/MoreJava/ControlFlow/dangling.html
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.html