JavaScript Do-While Loop
Do-While Loop Basics
The do-while loop is a variation of the while loop that guarantees the code block executes at least once, as the condition is checked after the execution. This makes it ideal for scenarios where you need to run code before evaluating the exit condition.
1// Basic do-while loop syntax
2let number = 1;
3do {
4 console.log("Number: " + number);
5 number++;
6} while (number <= 3);
7// Output: Number: 1, Number: 2, Number: 3
8
9// Execution flow breakdown:
10// 1. Initialize number = 1
11// 2. Execute code block: console.log("Number: 1")
12// 3. Increment number: number becomes 2
13// 4. Check condition: number <= 3 → true
14// 5. Execute code block: console.log("Number: 2")
15// 6. Increment number: number becomes 3
16// 7. Check condition: number <= 3 → true
17// 8. Execute code block: console.log("Number: 3")
18// 9. Increment number: number becomes 4
19// 10. Check condition: number <= 3 → false → EXIT LOOP
20
21// Key difference from while loop:
22let count = 5;
23
24// While loop - condition checked first
25while (count < 3) {
26 console.log("While loop: " + count);
27 count++;
28}
29// No output - condition false from start
30
31// Do-while loop - code runs first
32do {
33 console.log("Do-while loop: " + count);
34 count++;
35} while (count < 3);
36// Output: Do-while loop: 5 (runs once even though condition is false)
37
38// More examples:
39let counter = 10;
40do {
41 console.log("Countdown: " + counter);
42 counter--;
43} while (counter > 0);
44// Output: Countdown: 10, Countdown: 9, ... Countdown: 1
45
46let total = 0;
47do {
48 total += 3;
49 console.log("Running total: " + total);
50} while (total < 10);
51// Output: Running total: 3, Running total: 6, Running total: 9, Running total: 12Do-While Loop Core Characteristics
Guaranteed Execution- Code block always executes at least once, regardless of initial conditionPost-Condition Check- Condition is evaluated AFTER each iteration, not beforeInput Validation- Perfect for scenarios requiring at least one user input or data processingMenu Systems- Ideal for menu-driven applications where you always show menu first
Do-While Loop Syntax and Structure
Understanding the complete syntax and structure of do-while loops is essential for writing effective post-condition iteration logic. The do-while statement consists of a code block that executes first, followed by a condition check.
1// Complete do-while loop syntax breakdown
2
3// 1. Basic do-while loop
4let count = 0;
5do {
6 console.log("Basic: " + count);
7 count++;
8} while (count < 3);
9
10// 2. Complex condition do-while loop
11let x = 0, y = 10;
12do {
13 console.log(`x: ${x}, y: ${y}`);
14 x++;
15 y--;
16} while (x < 5 && y > 0);
17// Output: x: 0, y: 10; x: 1, y: 9; x: 2, y: 8; x: 3, y: 7; x: 4, y: 6
18
19// 3. Function-based condition
20function shouldContinue(attempts) {
21 return attempts < 3;
22}
23
24let attempts = 0;
25do {
26 console.log("Attempt: " + attempts);
27 attempts++;
28} while (shouldContinue(attempts));
29// Output: Attempt: 0, Attempt: 1, Attempt: 2
30
31// 4. Object property condition
32let user = { loginAttempts: 0, isLocked: false };
33do {
34 console.log("Login attempt: " + user.loginAttempts);
35 user.loginAttempts++;
36} while (user.loginAttempts < 3 && !user.isLocked);
37// Output: Login attempt: 0, Login attempt: 1, Login attempt: 2
38
39// 5. String processing do-while loop
40let text = "Hello";
41let index = 0;
42do {
43 console.log(`Character ${index}: '${text[index]}'`);
44 index++;
45} while (index < text.length);
46// Output: Character 0: 'H', Character 1: 'e', Character 2: 'l', Character 3: 'l', Character 4: 'o'
47
48// 6. Array processing do-while loop
49let numbers = [10, 20, 30, 40];
50let pos = 0;
51do {
52 console.log("Number at position " + pos + ": " + numbers[pos]);
53 pos++;
54} while (pos < numbers.length);
55// Output: Number at position 0: 10, Number at position 1: 20, etc.
56
57// 7. Multiple variable updates
58let a = 0, b = 100;
59do {
60 console.log(`a=${a}, b=${b}, difference=${b - a}`);
61 a += 10;
62 b -= 5;
63} while (a < b);
64// Continues until a >= b
65
66// 8. Mathematical condition
67let value = 1;
68do {
69 console.log("Value: " + value);
70 value *= 2;
71} while (value < 1000);
72// Output: Value: 1, Value: 2, Value: 4, Value: 8, Value: 16, Value: 32, Value: 64, Value: 128, Value: 256, Value: 512
73
74// Syntax rules:
75// - Code block executes first, before condition check
76// - Condition in parentheses after 'while'
77// - Code block in curly braces {}
78// - Semicolon required after while condition
79// - Loop variables must be initialized before the loop
80// - Loop variables must be updated inside the loopDo-While Loop Syntax Components
do { code }- Code block executes first, before any condition checkingwhile (condition);- Condition evaluated after execution, semicolon requiredGuaranteed Execution- Loop body always runs at least once, even if condition is initially falsePost-Condition Evaluation- Exit condition checked after each iteration, not before
Practical Do-While Loop Patterns and Use Cases
Do-while loops excel in specific scenarios where you need guaranteed first execution or when processing depends on the results of the first iteration. Understanding these patterns helps you apply do-while loops effectively in real-world applications.
1// Pattern 1: User input validation with guaranteed prompt
2let userInput;
3console.log("=== USER INPUT VALIDATION ===");
4
5do {
6 // Simulate getting user input (in real app, this would be prompt or form input)
7 userInput = userInput === undefined ? "" : "valid@email.com"; // First empty, then valid
8
9 if (userInput === "") {
10 console.log("Error: Input cannot be empty. Please try again.");
11 } else if (!userInput.includes("@")) {
12 console.log("Error: Please enter a valid email address.");
13 }
14} while (userInput === "" || !userInput.includes("@"));
15
16console.log("Valid email entered: " + userInput);
17// Output: Error: Input cannot be empty..., Valid email entered: valid@email.com
18
19// Pattern 2: Menu-driven applications
20let choice;
21console.log("\n=== MENU SYSTEM ===");
22
23do {
24 // Display menu (always show at least once)
25 console.log("\n=== MAIN MENU ===");
26 console.log("1. View Profile");
27 console.log("2. Edit Settings");
28 console.log("3. View Messages");
29 console.log("4. Exit");
30
31 // Simulate user choice
32 choice = choice === undefined ? "1" : "4"; // First view profile, then exit
33
34 switch (choice) {
35 case "1":
36 console.log("Displaying user profile...");
37 break;
38 case "2":
39 console.log("Opening settings...");
40 break;
41 case "3":
42 console.log("Showing messages...");
43 break;
44 case "4":
45 console.log("Goodbye!");
46 break;
47 default:
48 console.log("Invalid choice. Please try again.");
49 }
50} while (choice !== "4");
51// Output: Menu, "Displaying user profile...", Menu, "Goodbye!"
52
53// Pattern 3: Data processing with initial setup
54let data;
55let processedCount = 0;
56console.log("\n=== DATA PROCESSING ===");
57
58do {
59 // Initial data fetch or setup (always happens once)
60 if (processedCount === 0) {
61 console.log("Initializing data processing...");
62 data = ["item1", "item2", "item3", "DONE"];
63 }
64
65 // Process current data
66 let currentItem = data[processedCount];
67
68 if (currentItem !== "DONE") {
69 console.log(`Processing: ${currentItem}`);
70 processedCount++;
71 }
72
73} while (processedCount < data.length && data[processedCount] !== "DONE");
74
75console.log(`Processing complete. Items processed: ${processedCount}`);
76// Output: Initializing..., Processing: item1, Processing: item2, Processing: item3, Processing complete. Items processed: 3
77
78// Pattern 4: Retry mechanism with initial attempt
79let maxRetries = 3;
80let retryCount = 0;
81let success = false;
82console.log("\n=== RETRY MECHANISM ===");
83
84do {
85 retryCount++;
86 console.log(`Attempt ${retryCount}...`);
87
88 // Simulate API call that might fail
89 let apiSuccess = retryCount >= 2; // Succeeds on 2nd attempt
90
91 if (apiSuccess) {
92 console.log("✅ Operation successful!");
93 success = true;
94 } else {
95 console.log("❌ Operation failed");
96
97 if (retryCount < maxRetries) {
98 let waitTime = retryCount * 1000;
99 console.log(`Waiting ${waitTime}ms before retry...`);
100 }
101 }
102} while (!success && retryCount < maxRetries);
103
104if (!success) {
105 console.log("🚫 All retry attempts failed.");
106}
107// Output: Attempt 1..., ❌ Operation failed, Waiting 1000ms..., Attempt 2..., ✅ Operation successful!
108
109// Pattern 5: Configuration loading with fallback
110let config;
111let attempt = 0;
112console.log("\n=== CONFIGURATION LOADING ===");
113
114do {
115 attempt++;
116
117 // Try different configuration sources
118 if (attempt === 1) {
119 console.log("Attempting to load from primary config...");
120 config = null; // Simulate primary config failure
121 } else if (attempt === 2) {
122 console.log("Attempting to load from secondary config...");
123 config = null; // Simulate secondary config failure
124 } else {
125 console.log("Loading default configuration...");
126 config = { theme: "dark", language: "en" }; // Default config
127 }
128
129} while (config === null && attempt < 3);
130
131console.log("Configuration loaded:", config);
132// Output: Attempting to load from primary..., Attempting to load from secondary..., Loading default..., Configuration loaded: {theme: "dark", language: "en"}
133
134// Pattern 6: Game loop with initial setup
135let gameActive = true;
136let round = 0;
137let playerScore = 0;
138console.log("\n=== GAME LOOP ===");
139
140do {
141 round++;
142
143 // Initial game setup (only on first round)
144 if (round === 1) {
145 console.log("🎮 Game starting! Initializing players...");
146 }
147
148 // Game round logic
149 console.log(`\n--- Round ${round} ---`);
150 let roundScore = Math.floor(Math.random() * 10) + 1;
151 playerScore += roundScore;
152
153 console.log(`Round score: ${roundScore}`);
154 console.log(`Total score: ${playerScore}`);
155
156 // Check game end condition
157 if (playerScore >= 20) {
158 console.log("🎉 You reached the target score! Game over.");
159 gameActive = false;
160 } else if (round >= 5) {
161 console.log("⏰ Maximum rounds reached. Game over.");
162 gameActive = false;
163 }
164
165} while (gameActive);
166
167console.log(`Final score: ${playerScore} in ${round} rounds`);
168// Plays at least one round, continues until score >= 20 or 5 rounds
169
170// Pattern 7: File/stream reading simulation
171let fileContent = "";
172let bytesRead = 0;
173const chunkSize = 1024;
174console.log("\n=== FILE READING SIMULATION ===");
175
176do {
177 // Always attempt to read at least one chunk
178 bytesRead += chunkSize;
179
180 // Simulate reading chunk (in real app, this would be file I/O)
181 let chunk = `chunk_${bytesRead / chunkSize}`;
182 fileContent += chunk + " ";
183
184 console.log(`Read chunk: ${chunk} (total bytes: ${bytesRead})`);
185
186 // Simulate end of file
187 let moreDataAvailable = bytesRead < 4096; // 4KB file
188
189} while (bytesRead < 4096);
190
191console.log(`File reading complete. Total bytes: ${bytesRead}`);
192console.log(`Content preview: ${fileContent.substring(0, 50)}...`);Control Statements: Break and Continue in Do-While Loops
The break and continue statements work similarly in do-while loops as they do in while loops, but with the important distinction that continue still guarantees at least one execution before skipping iterations.
1// Break statement in do-while loops
2console.log("=== BREAK STATEMENT EXAMPLES ===");
3
4// Example 1: Break when target found
5let numbers = [1, 3, 5, 7, 9, 11, 13];
6let index = 0;
7let target = 7;
8
9do {
10 if (numbers[index] === target) {
11 console.log(`Found ${target} at index ${index}`);
12 break; // Exit loop immediately
13 }
14 console.log(`Checking index ${index}: ${numbers[index]}`);
15 index++;
16} while (index < numbers.length);
17// Output: Checking index 0: 1, Checking index 1: 3, Checking index 2: 5, Found 7 at index 3
18
19// Example 2: Break with complex condition
20let total = 0;
21let count = 1;
22
23do {
24 total += count;
25
26 if (total > 50) {
27 console.log(`Total exceeded 50 at count ${count}. Breaking.`);
28 break;
29 }
30
31 console.log(`Count: ${count}, Total: ${total}`);
32 count++;
33} while (count <= 100);
34// Output: Count: 1, Total: 1 ... until total exceeds 50
35
36// Example 3: Break in nested do-while loops
37let i = 0;
38outer: do {
39 let j = 0;
40 console.log(`Outer loop: i = ${i}`);
41
42 do {
43 if (i === 1 && j === 1) {
44 console.log("Breaking both loops at i=1, j=1");
45 break outer; // Break outer loop with label
46 }
47 console.log(` Inner loop: j = ${j}`);
48 j++;
49 } while (j < 3);
50
51 i++;
52} while (i < 3);
53
54// Continue statement in do-while loops
55console.log("\n=== CONTINUE STATEMENT EXAMPLES ===");
56
57// Example 1: Skip even numbers
58let num = 0;
59do {
60 num++;
61
62 if (num % 2 === 0) {
63 continue; // Skip even numbers
64 }
65
66 console.log("Odd number: " + num);
67} while (num < 10);
68// Output: Odd number: 1, Odd number: 3, Odd number: 5, Odd number: 7, Odd number: 9
69
70// Example 2: Skip invalid data with guaranteed first check
71let data = [null, "valid", "", "also valid", undefined];
72let dataIndex = 0;
73
74do {
75 let current = data[dataIndex];
76 dataIndex++;
77
78 // Skip null, undefined, and empty strings
79 if (!current) {
80 console.log(`Skipping invalid data at index ${dataIndex - 1}`);
81 continue;
82 }
83
84 console.log(`Processing: "${current}"`);
85} while (dataIndex < data.length);
86// Output: Skipping invalid data..., Processing: "valid", Skipping..., Processing: "also valid"
87
88// Example 3: Continue with input validation
89let userInput;
90let validationAttempts = 0;
91
92do {
93 validationAttempts++;
94
95 // Simulate user input
96 userInput = validationAttempts === 1 ? "" : "username";
97
98 if (userInput === "") {
99 console.log("Input cannot be empty. Please try again.");
100 continue; // Skip to next iteration
101 }
102
103 if (userInput.length < 3) {
104 console.log("Input must be at least 3 characters.");
105 continue;
106 }
107
108 console.log("Valid input: " + userInput);
109 break; // Exit when valid
110
111} while (validationAttempts < 5);
112
113// Example 4: Practical use case - data processing with conditions
114let users = [
115 { name: "Alice", active: true, role: "admin" },
116 { name: "Bob", active: false, role: "user" },
117 { name: "Charlie", active: true, role: "moderator" },
118 { name: "Diana", active: true, role: "user" }
119];
120
121let userIndex = 0;
122console.log("\nActive non-admin users:");
123
124do {
125 let user = users[userIndex];
126 userIndex++;
127
128 // Skip inactive users
129 if (!user.active) {
130 continue;
131 }
132
133 // Skip admin users
134 if (user.role === "admin") {
135 continue;
136 }
137
138 console.log(`- ${user.name} (${user.role})`);
139} while (userIndex < users.length);
140// Output: - Charlie (moderator), - Diana (user)
141
142// Example 5: Break and continue together in menu system
143let choice;
144let menuDisplayCount = 0;
145
146do {
147 menuDisplayCount++;
148
149 // Display menu
150 console.log("\n=== MENU (Display #" + menuDisplayCount + ") ===");
151 console.log("1. Option A");
152 console.log("2. Option B");
153 console.log("3. Skip this round");
154 console.log("4. Exit");
155
156 // Simulate user choice
157 if (menuDisplayCount === 1) {
158 choice = "3"; // Skip first round
159 } else if (menuDisplayCount === 2) {
160 choice = "1"; // Choose option A
161 } else {
162 choice = "4"; // Exit
163 }
164
165 switch (choice) {
166 case "1":
167 console.log("Executing Option A...");
168 break;
169 case "2":
170 console.log("Executing Option B...");
171 break;
172 case "3":
173 console.log("Skipping this round...");
174 continue; // Skip to next iteration
175 case "4":
176 console.log("Exiting...");
177 break;
178 default:
179 console.log("Invalid choice");
180 }
181
182} while (choice !== "4");
183// Output: Menu, "Skipping this round...", Menu, "Executing Option A...", Menu, "Exiting..."Common Do-While Loop Mistakes and Pitfalls
Understanding common mistakes with do-while loops is crucial for writing robust code. Do-while loops share some pitfalls with while loops but also have unique considerations due to their guaranteed first execution.
1// Mistake 1: Forgetting the semicolon
2let count = 0;
3// WRONG: Missing semicolon after while condition
4// do {
5// console.log(count);
6// count++;
7// } while (count < 3) // Syntax error - semicolon required
8
9// CORRECT: Semicolon required
10do {
11 console.log("Correct: " + count);
12 count++;
13} while (count < 3); // Semicolon essential!
14
15// Mistake 2: Infinite loops due to condition never becoming false
16// WRONG: Condition always true with no break mechanism
17// let flag = true;
18// do {
19// console.log("This runs forever!");
20// // No way to set flag to false
21// } while (flag);
22
23// CORRECT: Ensure condition can become false
24let safeFlag = true;
25let iterations = 0;
26do {
27 console.log("Safe iteration: " + iterations);
28 iterations++;
29 if (iterations >= 5) {
30 safeFlag = false; // Condition becomes false
31 }
32} while (safeFlag);
33
34// Mistake 3: Using assignment (=) instead of comparison (==/===)
35let value = 0;
36
37// WRONG: Assignment in condition (always true)
38// do {
39// console.log("This runs forever");
40// // value = 5 sets value to 5, which is truthy
41// } while (value = 5); // Infinite loop!
42
43// CORRECT: Use comparison operator
44do {
45 console.log("This runs once then stops");
46 value = 5;
47} while (value === 5); // Comparison, not assignment
48
49// Mistake 4: Not considering the guaranteed first execution
50// WRONG: Assuming loop might not run based on initial data
51let emptyArray = [];
52let arrayIndex = 0;
53
54do {
55 console.log("Processing: " + emptyArray[arrayIndex]); // Error: undefined
56 arrayIndex++;
57} while (arrayIndex < emptyArray.length);
58// Error: tries to access emptyArray[0] which is undefined
59
60// CORRECT: Check for empty conditions before accessing
61do {
62 if (emptyArray.length === 0) {
63 console.log("Array is empty, nothing to process");
64 break;
65 }
66 console.log("Processing: " + emptyArray[arrayIndex]);
67 arrayIndex++;
68} while (arrayIndex < emptyArray.length);
69
70// Mistake 5: Modifying loop variable in unpredictable ways
71let counter = 0;
72
73// WRONG: Unpredictable modification can cause issues
74// do {
75// console.log(counter);
76// counter += Math.random() * 3; // Might skip over termination condition
77// } while (counter < 10);
78
79// CORRECT: Predictable increment
80do {
81 console.log("Predictable: " + counter);
82 counter++; // Always increases by 1
83} while (counter < 5);
84
85// Mistake 6: Not handling edge cases in user input scenarios
86let userInput = "";
87
88// WRONG: No proper validation for edge cases
89// do {
90// // If userInput starts as empty, might cause issues
91// console.log("Processing: " + userInput);
92// userInput = "quit";
93// } while (userInput !== "quit");
94
95// CORRECT: Handle edge cases explicitly
96let input = "";
97do {
98 if (input === "") {
99 console.log("Please enter a command");
100 } else if (input === "help") {
101 console.log("Available commands: start, stop, quit");
102 } else {
103 console.log("Processing: " + input);
104 }
105 input = "quit"; // Simulate user input
106} while (input !== "quit");
107
108// Mistake 7: Complex conditions that are hard to debug
109let x = 0, y = 10, z = 5;
110
111// WRONG: Overly complex condition
112// do {
113// console.log(`x=${x}, y=${y}, z=${z}`);
114// x++;
115// z--;
116// } while (x < y && (z > 0 || x % 2 === 0) && !(x === 5 && z === 3));
117
118// CORRECT: Break complex conditions into variables
119let continueLoop = true;
120do {
121 console.log(`x=${x}, y=${y}, z=${z}`);
122 x++;
123 z--;
124
125 // Calculate conditions separately
126 let condition1 = x < y;
127 let condition2 = z > 0 || x % 2 === 0;
128 let condition3 = !(x === 5 && z === 3);
129
130 continueLoop = condition1 && condition2 && condition3;
131
132} while (continueLoop);
133
134// Mistake 8: Not using break for early termination when appropriate
135let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
136let searchIndex = 0;
137let targetValue = 7;
138
139// LESS EFFICIENT: Continues after finding target
140// do {
141// if (data[searchIndex] === targetValue) {
142// console.log("Found at index: " + searchIndex);
143// }
144// searchIndex++;
145// } while (searchIndex < data.length);
146
147// MORE EFFICIENT: Break when target found
148do {
149 if (data[searchIndex] === targetValue) {
150 console.log("Found at index: " + searchIndex);
151 break; // Stop searching
152 }
153 searchIndex++;
154} while (searchIndex < data.length);
155
156// Mistake 9: Assuming do-while is always better than while
157// Consider this scenario:
158
159// While loop might be better when initial check is needed
160let shouldProcess = false;
161
162// While loop - no execution (correct behavior)
163while (shouldProcess) {
164 console.log("This should not run");
165}
166// No output - correct
167
168// Do-while loop - executes once (might be wrong)
169do {
170 console.log("This runs once even though it shouldn't!");
171} while (shouldProcess);
172// Output: This runs once... (potentially incorrect)
173
174// Mistake 10: Not considering performance with large datasets
175let bigArray = new Array(1000000).fill(0);
176
177// LESS EFFICIENT: Repeated function calls in condition
178// let i = 0;
179// do {
180// bigArray[i] = i;
181// i++;
182// } while (i < bigArray.length); // length checked every iteration
183
184// MORE EFFICIENT: Cache length
185let j = 0;
186const arrayLength = bigArray.length;
187do {
188 bigArray[j] = j;
189 j++;
190} while (j < arrayLength); // Length cachedDo-While Loop Best Practices
Following established best practices for do-while loops leads to more readable, maintainable, and reliable code. These guidelines help you write effective do-while loops that leverage their unique guaranteed execution characteristic.
1// Best Practice 1: Use descriptive variable names
2let menuChoice;
3const MAX_MENU_ATTEMPTS = 5;
4
5// GOOD - descriptive names
6do {
7 console.log("Menu displayed");
8 menuChoice = "exit"; // Simulate user choice
9} while (menuChoice !== "exit" && menuChoice !== "quit");
10
11// AVOID - unclear names
12// let x;
13// do {
14// console.log(x);
15// x = "q";
16// } while (x !== "q");
17
18// Best Practice 2: Always ensure loop termination
19let iteration = 0;
20const SAFETY_LIMIT = 1000;
21
22// GOOD - safety limit to prevent infinite loops
23do {
24 // Process data
25 iteration++;
26
27 if (iteration >= SAFETY_LIMIT) {
28 console.warn("Safety limit reached - breaking loop");
29 break;
30 }
31} while (someCondition);
32
33// Best Practice 3: Use constants for magic numbers
34const MAX_LOGIN_ATTEMPTS = 3;
35const PASSWORD_MIN_LENGTH = 8;
36
37let loginAttempts = 0;
38let loginSuccessful = false;
39
40do {
41 loginAttempts++;
42 console.log(`Login attempt ${loginAttempts}`);
43
44 // Simulate login logic
45 let password = loginAttempts === 1 ? "short" : "longenoughpassword";
46
47 if (password.length < PASSWORD_MIN_LENGTH) {
48 console.log(`Password must be at least ${PASSWORD_MIN_LENGTH} characters`);
49 continue;
50 }
51
52 loginSuccessful = true;
53 console.log("Login successful!");
54
55} while (!loginSuccessful && loginAttempts < MAX_LOGIN_ATTEMPTS);
56
57// Best Practice 4: Keep loop conditions simple and clear
58let shouldContinue = true;
59let processCount = 0;
60
61// GOOD - simple, clear condition
62do {
63 processCount++;
64 console.log(`Processing item ${processCount}`);
65
66 // Clear condition to stop
67 shouldContinue = processCount < 5;
68
69} while (shouldContinue);
70
71// Best Practice 5: Use break for early termination when appropriate
72let numbers = [1, 2, 3, -1, 4, 5];
73let numIndex = 0;
74
75// GOOD - break when condition met
76do {
77 if (numbers[numIndex] < 0) {
78 console.log("Negative number found, stopping processing");
79 break;
80 }
81 console.log("Processing:", numbers[numIndex]);
82 numIndex++;
83} while (numIndex < numbers.length);
84
85// Best Practice 6: Document complex loop logic
86// Process configuration until either:
87// - Valid config loaded, OR
88// - All fallback options exhausted, OR
89// - Maximum attempts reached
90let config = null;
91let configAttempts = 0;
92const MAX_CONFIG_ATTEMPTS = 3;
93
94do {
95 configAttempts++;
96
97 // Try different configuration sources
98 if (configAttempts === 1) {
99 console.log("Loading primary configuration...");
100 config = loadConfig("primary");
101 } else if (configAttempts === 2) {
102 console.log("Loading secondary configuration...");
103 config = loadConfig("secondary");
104 } else {
105 console.log("Loading default configuration...");
106 config = { default: true };
107 }
108
109} while (config === null && configAttempts < MAX_CONFIG_ATTEMPTS);
110
111function loadConfig(source) {
112 // Simulate config loading
113 return source === "primary" ? null : { loaded: true };
114}
115
116// Best Practice 7: Consider when do-while is appropriate
117// Use do-while when:
118// - You need guaranteed first execution
119// - Processing depends on results of first iteration
120// - Menu-driven applications
121// - Input validation with retry
122
123// Use while when:
124// - Initial condition might prevent execution
125// - You don't need guaranteed first run
126// - Processing doesn't depend on first execution
127
128// Best Practice 8: Handle edge cases gracefully
129let userData = []; // Could be empty array
130
131// GOOD - check for edge cases
132do {
133 if (userData.length === 0) {
134 console.log("No data to process");
135 break;
136 }
137
138 console.log("Processing user data...");
139
140} while (false); // Single execution with break for control
141
142// Best Practice 9: Test both normal and edge cases
143function testDoWhileLoop() {
144 let testCases = [
145 { input: 1, expected: 1 },
146 { input: 3, expected: 3 },
147 { input: 0, expected: 1 } // Do-while always runs at least once
148 ];
149
150 for (let testCase of testCases) {
151 let result = 0;
152 let counter = 0;
153
154 do {
155 result++;
156 counter++;
157 } while (counter < testCase.input);
158
159 console.assert(result === testCase.expected,
160 `Test failed: input ${testCase.input}, expected ${testCase.expected}, got ${result}`);
161 }
162
163 console.log("All do-while loop tests passed!");
164}
165
166testDoWhileLoop();
167
168// Best Practice 10: Use meaningful condition variables
169let isDataValid = false;
170let validationAttempts = 0;
171
172// GOOD - meaningful condition name
173do {
174 validationAttempts++;
175
176 // Simulate data validation
177 let data = validationAttempts === 1 ? "invalid" : "valid data";
178
179 isDataValid = data !== "invalid" && data.length > 0;
180
181 if (!isDataValid) {
182 console.log(`Validation attempt ${validationAttempts} failed`);
183 }
184
185} while (!isDataValid && validationAttempts < 3);
186
187// Best Practice 11: Consider extracting complex loop bodies to functions
188function validateInput(input) {
189 // Complex validation logic
190 return input && input.length >= 5 && input.includes("@");
191}
192
193function processInput(input) {
194 // Complex processing logic
195 return input.toUpperCase();
196}
197
198let userInput;
199let processingAttempts = 0;
200
201do {
202 processingAttempts++;
203
204 // Simulate user input
205 userInput = processingAttempts === 1 ? "test" : "valid@input.com";
206
207 if (!validateInput(userInput)) {
208 console.log("Invalid input, please try again");
209 continue;
210 }
211
212 let processed = processInput(userInput);
213 console.log("Processed input:", processed);
214
215} while (!validateInput(userInput) && processingAttempts < 3);
216
217// Best Practice 12: Use appropriate loop control statements
218let values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
219let valueIndex = 0;
220
221do {
222 let value = values[valueIndex];
223 valueIndex++;
224
225 // Skip even numbers
226 if (value % 2 === 0) {
227 continue;
228 }
229
230 // Stop if value exceeds 7
231 if (value > 7) {
232 break;
233 }
234
235 console.log("Processing odd number:", value);
236} while (valueIndex < values.length);
237
238// Best Practice 13: Consider readability with complex conditions
239let shouldProcessData = true;
240let dataItemsProcessed = 0;
241let errorOccurred = false;
242
243// GOOD - clear conditions with descriptive variables
244do {
245 dataItemsProcessed++;
246
247 // Simulate processing that might fail
248 errorOccurred = dataItemsProcessed === 3; // Error on 3rd item
249
250 if (errorOccurred) {
251 console.log("Error occurred, stopping processing");
252 shouldProcessData = false;
253 continue;
254 }
255
256 console.log(`Processed data item ${dataItemsProcessed}`);
257
258 // Continue until 5 items or error
259 shouldProcessData = dataItemsProcessed < 5 && !errorOccurred;
260
261} while (shouldProcessData);