Do loop:
do loop example:
int count = 0;
do
{
count++;
System.out.println(count);
}
while (count <5);
Nested loops:
Similar to nested for statements, while loops and do loops can be nested as well
"Nested" means the inner loop must complete reside in the outer loops - no overlapping
For each iteration of the outer loop, the inner loop iterates completely
How to hand trace a program for debugging:
Your code will go wrong so debugging and testing are always needed
You can debug (troubleshoot) by:
Hand tracing
Adding print statements to your code
Adding testing methods to the class
Hand tracing (tracing by hand) is a simulation of code execution in which you step through statements and tack the values of certain variables. It is effective with a short program or part of a method. When you hand trace code or pseudocode, you write the names of the variables on a sheet of paper, mentally execute each step of the code, and update the variable values accordingly.
When you use print statements for debugging, you insert print statements at key locations to show values of significant variables and how far your code got before there was a problem. In the print statement, it is a good idea to specify the location of the trace (what method) and the variable name and its value.
All Integrated Development Environments (IDE) such as Eclipse have an interactive debugger feature. You can single-step step through your code (one statement at a time), see what is stored in variables, set breakpoints, and watch a variable or expression during execution.
Phases of Programming
A typical programming task can be divided into multiple phases
Analyzing and solution design phase: Analyze the problem to understand data and constraints, produce an ordered sequence of steps that describe the solution of a problem
This sequence of steps is called an algorithm
Implementation phase: implement the program in some programming language
Debugging and testing phase: test the program thoroughly, debug and resolve defects
Steps in Solution design phase
Produce a general algorithm
You can use pseudocode (pseudocode is an artificial and informal language that helps programmers develop algorithms, it is very similar to everyday English)
Refine the algorithm successively to get a step by step detailed algorithm that is very close to a computer language
Turn pseudocode into flowchart to verify the designed solution works correctly in all cases
Flowchart
A schematic representation of the algorithm or problem-solving process
A graphical representation of the sequence of operations in a program (it can have different levels of granularity)
Shows logic of an algorithm
Emphasizes individual steps and their interconnections
Different symbols are used to draw each type of flowchart
HW solution: https://replit.com/@liizz/15-HW-Solution#Main.java