1 / 14

Control

Control. Conditionals...Loops Comments...Constants. The simple case if(<boolean_value>) single_statement;. OR if(<boolean_value>) { statements; }. Conditionals. This can be dangerous!!!. The Classic Killer Error. if(<some_boolean>); { statement; statement; }. Conditionals.

Download Presentation

Control

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. Control Conditionals...Loops Comments...Constants

  2. The simple case if(<boolean_value>) single_statement; OR if(<boolean_value>) { statements; } Conditionals This can be dangerous!!!

  3. The Classic Killer Error if(<some_boolean>); { statement; statement; }

  4. Conditionals • Adding the else part: if(<boolean condition>) { statement; statement; } else { statement; statement; } Example: if( leapYear ) { febDays = 29; } else { febDays = 28; }

  5. Conditionals • Adding the else part: if(<boolean condition>) { statement; statement; } else { statement; statement; } Example: if( leapYear ) { febDays = 29; } else { febDays = 28; }

  6. Pseudocode total, i isoftype Num total <- 0 i <- 1 loop exitif(i > 10) total <- total + i i <- i + 1 endloop Java int total, i; total = 0; i = 0; while(i <= 10) { total = total + i; i = i + 1; } Loops -- Sentinel

  7. Pseudocode total, i isoftype Num total <- 0 i <- 1 loop total <- total + i i <- i + 1 exitif(i > 10) endloop Java int total, i; total = 0; i = 1; do { total = total + i; i = i + 1; } while(i < 10); Loops -- Test Last

  8. for Loops for(<init>; <while_boolean>; <inc>) statement; • <init> is done before entering loop • <while_boolean> is checked before entering and before each subsequent iteration and must be true • <inc> is performed at the end of each iteration. • Note: <init> and <inc> can be multiple statements but for now don’t do it!

  9. Classic while loop int total = 0; int i = 0; while(i < 10) { total = total + 1; i = i + 1; } for loop int total = 0; int i; for(i = 0; i < 10; i++) { total = total + 1; } Loops -- Convenience!

  10. Loops Summarized while(<boolean>) { statement(s); } do { statement(s); } while(<boolean>); for(<init>; <while_boolean>; <inc>) { statement(s); }

  11. Shorthand Operators • iCounter = iCounter + 1; OR iCounter++; • iCounter = iCounter - 1; OR iCounter--; • iCounter = iCounter + 2; OR iCounter += 2; • iCounter = iCounter * 5; OR iCounter *= 5;Last two examples: it’s “op” then “equals” (e.g., +=2), not “equals” then “op” (e.g., isn’t =+2) • These work with +, -, *, / plus others. • i++; // means increment i • i += 2; // means add 2 to the value of i • WARNING: THESE ARE NOT JUST CUTE TRICKS THEY ARE VERY POWERFUL AND CAN BE MISUSED. HANDLE WITH CARE!!!

  12. Documentation & Comments • Two ways to do it: // Double slashes comment out everything until the // end of the line /* Starts a comment which may extend multiple lines and eventually be terminated by */

  13. Constants • Pseudocode <CONST_ID> is <constant value> MIN_PASSING is 60 PI is 3.14159 • Java public final static <type> <IDer> = <value>; public final static int MIN_PASSING = 60; public final static float PI = (float) 3.14159; • Details on “why this syntax” to come soon...

More Related