Episode 7 — DSA with JavaScript / 7.2 — Loops Nested Loops Pattern Programming

7.2 — Quick Revision: Loops, Nested Loops & Patterns

<< Overview


Loop types cheat sheet

for (init; cond; update) { body }   ← known count
while (cond) { body }               ← unknown count
do { body } while (cond);           ← at least once

Key formulas for patterns

PatternSpaces per rowStars/chars per row
Right triangle0i
Inverted triangle0n - i
Centered pyramidn-1-i2i+1
Diamond (upper)n-1-i2i+1
Diamond (lower)n-1-i (counting down)2i+1

break vs continue

StatementEffect
breakExit innermost loop
continueSkip to next iteration
labeled breakExit named loop

Complexity

Loop patternComplexity
Single loopO(n)
Nested (n×n)O(n²)
Loop to √nO(√n)
Halving loopO(log n)
Triple nestedO(n³)

Infinite loop prevention

  1. Always update the loop variable
  2. Ensure condition will eventually be false
  3. Use a max iteration safeguard
  4. Avoid floating-point exact comparisons

Common pitfalls

  1. Off-by-one errors (< vs <=)
  2. Forgetting to increment counter
  3. Wrong loop variable in nested loops (i vs j)
  4. Modifying array while iterating
  5. Using = instead of === in condition

Self-check drill

SC-001

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-002

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-003

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-004

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-005

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-006

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-007

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-008

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-009

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-010

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-011

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-012

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-013

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-014

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-015

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-016

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-017

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-018

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-019

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-020

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-021

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-022

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-023

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-024

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-025

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-026

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-027

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-028

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-029

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-030

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-031

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-032

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-033

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-034

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-035

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-036

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-037

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-038

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-039

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-040

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-041

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-042

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-043

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-044

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-045

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-046

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-047

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-048

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-049

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-050

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-051

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-052

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-053

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-054

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-055

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-056

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-057

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-058

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-059

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-060

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-061

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-062

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-063

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-064

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-065

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-066

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-067

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-068

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-069

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-070

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-071

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-072

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-073

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-074

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-075

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-076

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-077

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-078

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-079

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-080

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-081

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-082

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-083

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-084

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-085

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-086

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-087

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-088

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-089

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-090

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-091

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-092

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-093

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-094

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-095

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-096

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-097

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-098

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-099

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-100

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-101

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-102

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-103

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-104

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-105

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-106

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-107

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-108

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-109

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-110

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-111

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-112

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-113

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-114

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-115

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-116

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-117

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-118

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-119

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-120

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-121

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-122

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-123

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-124

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-125

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-126

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-127

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-128

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-129

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-130

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-131

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-132

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-133

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-134

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-135

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-136

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-137

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-138

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-139

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-140

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-141

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-142

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-143

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-144

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-145

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-146

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-147

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-148

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-149

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-150

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-151

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-152

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-153

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-154

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-155

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-156

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-157

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-158

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-159

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-160

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-161

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-162

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-163

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-164

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-165

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-166

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-167

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-168

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-169

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-170

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-171

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-172

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-173

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-174

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-175

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-176

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-177

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-178

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-179

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-180

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-181

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-182

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-183

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-184

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-185

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-186

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-187

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-188

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-189

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-190

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function

SC-191

  • Q: What are the three parts of a for loop?
  • A: initialization; condition; update

SC-192

  • Q: When does a do-while body execute?
  • A: At least once, before the condition is checked

SC-193

  • Q: What does break do?
  • A: Exits the innermost enclosing loop

SC-194

  • Q: What does continue do?
  • A: Skips the rest of the current iteration

SC-195

  • Q: Time complexity of nested n×n loop?
  • A: O(n²)

SC-196

  • Q: How many stars in pyramid row i?
  • A: 2*i + 1 (0-indexed)

SC-197

  • Q: What is Floyd's triangle?
  • A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6

SC-198

  • Q: How to prevent infinite loops?
  • A: Ensure loop variable changes and condition becomes false

SC-199

  • Q: Difference between for...of and for...in?
  • A: for...of: values; for...in: enumerable property names/keys

SC-200

  • Q: How to exit outer loop from inner?
  • A: Labeled break (JS) or return from function