1 / 13

Branching

Branching. & Decision Making Algorithms BY: Aakash Indurkhya. The If/else statements.

brian-russo
Download Presentation

Branching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Branching & Decision Making Algorithms BY: Aakash Indurkhya

  2. 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.

  3. 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

  4. 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

  5. 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.

  6. 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 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. 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.

  8. 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.

  9. 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}

  10. 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.

  11. 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; }

  12. 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 cases System.out.println(“Invalid Input”); }

  13. 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

More Related