1 / 9

Conditional Branching

Conditional Branching. If Be Or Not If Be (then else). booleans hold a true/false value We take advantage of this by using them to decide which route our program will take. Examples: stinky holds the boolean value for whether or not Mr. Mayewsky is stinky.

edan-mccoy
Télécharger la présentation

Conditional 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. Conditional Branching

  2. If Be Or Not If Be (then else) • booleans hold a true/false value • We take advantage of this by using them to decide which route our program will take. • Examples: • stinky holds the boolean value for whether or not Mr. Mayewsky is stinky. • If stinky… then Mr. Mayewsky takes a shower. • redLight holds the boolean value for whether or not the light is red • If redLight… then stop car… otherwise (else) step on it to make the light.

  3. If Else Syntax if (boolean){ …code… //executes if boolean is true }else if (boolean2){ …code… //executes if boolean if false //and boolean2 is true }else{ …code… //executes if all booleans are false }

  4. Relational Operators • == • equal to • != • not equal to • > • greater than • >= • greater than or equal to • < • less than • <= • less than or equal to • **Only use these with primitive variables!!!!!!!!!!!!!!!!

  5. If Else Syntax if (time < 7){ mayewsky.sleep(); }else if (time < 5){ mayewsky.work(); }else{ mayewsky.playComputerGames(); }

  6. Boolean Operations • ! – the not operator which will negate the boolean value(true becomes false and vice versa) • && – the and operator which will and two boolean values (both need to be true for the result to be true) • || – the or operator which will or two boolean values (if either is true then the result is true) • Order of Operation: ! Then && Then ||

  7. If Else Syntax if (time < 7 || isWeekEnd && time < 10){ mayewsky.sleep(); }else if (time < 5 && !isWeekEnd){ mayewsky.work(); }else if(!isWeekEnd){ mayewsky.playComputerGames(); }else{ mayewsky.watchFootball();

  8. Nesting • You can put an if statement inside another if statement • This can be used to better organize your code. • It makes it both easier to write, read, and edit!

  9. If Else Syntax if (isWeekEnd)){ if (time < 10){ mayewsky.sleep(); }else{ mayewsky.watchFootball(); } }else{ if (time < 7){ mayewsky.sleep(); }else if (time < 5){ mayewsky.work(); }else{ mayewsky.playComputerGames(); } }

More Related