140 likes | 253 Vues
This guide provides an overview of essential programming control structures including conditionals and loops, along with documentation practices. Learn how to utilize if-else statements for decision-making in code, apply loops for repeated tasks, and understand the significance of constants. Explore classic examples such as leap years and summing numbers, and discover shorthand operators for efficiency. Additionally, learn best practices for writing comments and enhancing code readability, essential for any programming project.
E N D
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 • Adding the else part: if(<boolean condition>) { statement; statement; } else { statement; statement; } Example: if( leapYear ) { febDays = 29; } else { febDays = 28; }
Conditionals • Adding the else part: if(<boolean condition>) { statement; statement; } else { statement; statement; } Example: if( leapYear ) { febDays = 29; } else { febDays = 28; }
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
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
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!
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!
Loops Summarized while(<boolean>) { statement(s); } do { statement(s); } while(<boolean>); for(<init>; <while_boolean>; <inc>) { statement(s); }
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!!!
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 */
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...