14
Control Conditionals...Loops Comments...Constants

Control

Embed Size (px)

DESCRIPTION

Control. Conditionals...Loops Comments...Constants. The simple case if() single_statement;. OR if() { statements; }. Conditionals. This can be dangerous!!!. The Classic Killer Error. if(); { statement; statement; }. Conditionals. - PowerPoint PPT Presentation

Citation preview

Page 1: Control

Control

Conditionals...Loops

Comments...Constants

Page 2: Control

Conditionals

• The simple case

if(<boolean_value>)

single_statement;

This can bedangerous!!!

This can bedangerous!!!

• OR

if(<boolean_value>)

{

statements;

}

Page 3: Control

The Classic Killer Error

if(<some_boolean>);

{

statement;

statement;

}

Page 4: Control

Conditionals

• Adding the else part:

if(<boolean condition>)

{

statement;

statement;

}

else

{

statement;

statement;

}

Example:

if( leapYear ) {

febDays = 29;}else{

febDays = 28;}

Example:

if( leapYear ) {

febDays = 29;}else{

febDays = 28;}

Page 5: Control

Conditionals

• Adding the else part:

if(<boolean condition>) {

statement;

statement;

} else {

statement;

statement;

}

Example:

if( leapYear ) {febDays = 29;

} else {febDays = 28;

}

Example:

if( leapYear ) {febDays = 29;

} else {febDays = 28;

}

Page 6: Control

Loops -- Sentinel

• 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;

}

Page 7: Control

Loops -- Test Last

• 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);

Page 8: Control

for Loops

for(<init>; <while_boolean>; <inc><inc>)

statement;

• <init> is done before entering loop• <while_boolean> is checked before entering and before each subsequent iteration and must be true

• <inc><inc> is performed at the end of each iteration.

• Note: <init> and <inc><inc> can be multiple statements but for now don’t do it!

Page 9: Control

Loops -- Convenience!

• Classic while loop

int total = 0;

int i = 0;

while(i < 10)

{

total = total + 1;total = total + 1;

i = i + 1i = i + 1;

}

• for loop

int total = 0;

int i;

for(i = 0; i < 10; i++i++)

{

total = total + 1;total = total + 1;

}

Page 10: Control

Loops Summarized

while(<boolean>){statement(s);

}

do{statement(s);

} while(<boolean>);

for(<init>; <while_boolean>; <inc>){statement(s);

}

Page 11: Control

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!!!

Page 12: Control

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 */

Page 13: Control

Constants

• Pseudocode<CONST_ID> is <constant value>

MIN_PASSING is 60

PI is 3.14159

• Javapublic 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...

Page 14: Control