Sie sind auf Seite 1von 7

Chapter 3: Program Statements

The creation of software 4 basic activities:

Requirements
Design
Implementing code
Testing implementation

Requirements- specify tasks programs must accomplish


(what to do)
Design- specify how a program will accomplish its
requirements
Includes 1+ algorithms
Algorithms- Step-by-step process for solving
problems
o Can be expressed as pseudoccode, which is
code-like
Implementation- process of translating a design into
source code
Least creative, lack of freedom
A program should be executed multiple times with
carious input in an attempt to find errors
Debugging- the process of discovering the causes of
problems and fixing them
only one more bug to fix
Flow of Control
Boolean expression (T or F)
One statement after another in sequence
IF AND IF/ELSE ARE CONDITIONAL STATEMENTS

Conditional statement- lets us choose which statement will be


executed next
If statement syntax:
If (condition)
statement;
If the condition is true, its executed, if not goes to else
When you have a semicolon, it breaks things up, so
everything else has nothing to do with the condition
No matter what, print line is executed
No semicolon after condition
Only Ifs get conditions
Boolean expressions
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
If/Else Statement
An else clause can be added to an if statement to make
an if-else statement
If condition is true, then statement1 is executed: if
condition is false, statement2 is executed
Block Statement
-If you want to say multiple things, you must put them in brackets
{.}
Nested if Else Statements
Logical operators

! Logical NOT
&& Logical AND
|| Logical OR
DONT
DO

If 90 <= grade <=100)


If (90 <= grade && grade <=100)

They all take Boolean operands and produce Boolean results


Logical NOT is a unary operator
Logical AND and logical OR
Short-circuiting OR-when you find your first true value
AND- when you find a false
Comparing Characters
We can use the relational operators on character data
The uppercase alphabet
Comparing Strings
Remember that a character string in Java is an object
We cant use relational operators to compare strings
The equals method can be called with strings to
determine if two strings contain exactly the same
characters in the same order
o If (x == y): String X =hello String y= Bye; If
(x.equals(y))
The compareTo method deter mines if one string
comes before another (based on the Unicode
character set)
o Boolean
o x.compareTo (y)
X comes before y --- (negative)
If same 0 (zero)
X comes after y + (positive)
Lexicographic ordering- comparing characters and
strings based on a character set

o Not strictly alphabetical when upper and


lowercase are mixed
o All uppercase comes before lowercase in Unicode
o Short strings come before longer strings with the
same prefix
More Operators
Increment and decrement operators are arithmetic and
operate on one (1) operand
The increment operator (++) adds one to its operand
The decrement operator (--) subtracts one from its operand
Count ++ is equivalent to Count + count = 1
Operator
+=

Example
Num += count

Equivalent To
Num = num +
count
DO everything in the right side first , then the result is
combined with the original variable
If += are strings, then you should slide them together to get
a string
Repetition Statements- allows us to execute a statement
multiple times
The two types are
o The while loop

While (condition)
statement;
o If the condition is true, you keep going in the condition
until its false
o Infinite loops will eventually make the condition false
o Must make sure your program terminated normally
o The for loop
The While Loop:
The for statement- has the following syntax:

for (initialization; condition; increment; statement)


for (int count =1 (Step 1); count<=5 (Step 2) ; count ++
(Step 4))
o s.o.p (count); (Step 3)
o it does the cycle, and then goes 2,3,4 until it reaches
count
used generally when we know many times the program
executed
its a loop
initialization can be left out if somewhere Step 1 is initialized
(int count = 1)
if the condition is left out, its an infinite loop
TIP:
If ! is outside of parentheses, you must distribute it
! (a||b) = !a && !b

QUALITY AND RELATIONAL OPERATOR


KNOW WHEN A PROGRAM HAS ERRORS
Block statement
o Requirements- what
o Design- how, plan
o Implementing code- program, least creative
o Testing implementation- go through it a bunch of
times, a bunch of possibilities
System.out.println
Wont work logically
KNOW THE FOUR STAGES OF PROGRAMMING AND
EXPLANATION
KNOW THE && and || and what they mean
KNOW ABOUT NESTED IF/ELSE

KNOW ABOUT OPERATORS

Das könnte Ihnen auch gefallen