Looping Statements in C++ (while, do-while, for Loop) Notes | Basic Computer Engineering | RGPV BTech First Year

Looping Statements in C++ (while, do-while, for Loop) Notes | Basic Computer Engineering | RGPV BTech First Year


Looping Statements in C++

Programming рдореЗрдВ рдХрдИ рдмрд╛рд░ рдХрд┐рд╕реА рдПрдХ рд╣реА Statement рдпрд╛ Statements рдХреЗ рд╕рдореВрд╣ рдХреЛ рдмрд╛рд░-рдмрд╛рд░ Execute рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реЛрддреА рд╣реИред рдпрджрд┐ рдкреНрд░рддреНрдпреЗрдХ Statement рдХреЛ рдмрд╛рд░-рдмрд╛рд░ рд▓рд┐рдЦрд╛ рдЬрд╛рдП рддреЛ Program рдмрд╣реБрдд рдмрдбрд╝рд╛ рддрдерд╛ Difficult рд╣реЛ рдЬрд╛рдПрдЧрд╛ред рдЗрд╕ рд╕рдорд╕реНрдпрд╛ рдХрд╛ рд╕рдорд╛рдзрд╛рди Looping Statements рджреНрд╡рд╛рд░рд╛ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Loop рдХреА рд╕рд╣рд╛рдпрддрд╛ рд╕реЗ рдХрд┐рд╕реА рднреА Statement рдХреЛ рдирд┐рд╢реНрдЪрд┐рдд Condition рдХреЗ рдЖрдзрд╛рд░ рдкрд░ рдмрд╛рд░-рдмрд╛рд░ Execute рдХрд░рд╛рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред

C++ рдореЗрдВ рдореБрдЦреНрдп рд░реВрдк рд╕реЗ рддреАрди рдкреНрд░рдХрд╛рд░ рдХреЗ Loops рдЙрдкрд▓рдмреНрдз рд╣реИрдВ тАФ while, do-while рддрдерд╛ for Loopред рдЗрдирдХрд╛ рдЙрдкрдпреЛрдЧ Counting, Searching, Data Processing, Pattern Printing, Array Traversing рддрдерд╛ Repetitive Calculations рдЬреИрд╕реЗ рдХрд╛рд░реНрдпреЛрдВ рдореЗрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред


Introduction

Loop Programming рдХрд╛ рдПрдХ рдорд╣рддреНрд╡рдкреВрд░реНрдг Control Structure рд╣реИред рдпрд╣ Program рдореЗрдВ Code Reusability рдмрдврд╝рд╛рддрд╛ рд╣реИ рддрдерд╛ рдмрд╛рд░-рдмрд╛рд░ рд▓рд┐рдЦреЗ рдЬрд╛рдиреЗ рд╡рд╛рд▓реЗ Statements рдХреЛ рдПрдХ рд╣реА Block рдореЗрдВ Execute рдХрд░рдиреЗ рдХреА рд╕реБрд╡рд┐рдзрд╛ рджреЗрддрд╛ рд╣реИред

Loop рддрдм рддрдХ Execute рд╣реЛрддрд╛ рд░рд╣рддрд╛ рд╣реИ рдЬрдм рддрдХ рдЙрд╕рдХреА Condition true рд░рд╣рддреА рд╣реИред рдЬреИрд╕реЗ рд╣реА Condition false рд╣реЛрддреА рд╣реИ, Program Loop рд╕реЗ рдмрд╛рд╣рд░ рдирд┐рдХрд▓рдХрд░ рдЕрдЧрд▓рд╛ Statement Execute рдХрд░рддрд╛ рд╣реИред


Definition of Loop

Loop рдРрд╕рд╛ Control Structure рд╣реИ рдЬреЛ рдХрд┐рд╕реА Statement рдЕрдерд╡рд╛ Statement Group рдХреЛ рдХрд┐рд╕реА Condition рдХреЗ рдЖрдзрд╛рд░ рдкрд░ рдмрд╛рд░-рдмрд╛рд░ Execute рдХрд░рддрд╛ рд╣реИред

Loop = Repeated Execution of Statements Until Condition Becomes False

Need of Loop

  • Repeated Tasks рдХреЛ Automatically Execute рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Code Duplication рдХрдо рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Program рдХреЛ рдЫреЛрдЯрд╛ рдПрд╡рдВ Readable рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Large Data Processing рдХреЗ рд▓рд┐рдПред
  • Iteration рдЖрдзрд╛рд░рд┐рдд Problems Solve рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Execution Time рдХрдо рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред

Characteristics of Loop

  • Repeated Execution.
  • Condition Based Execution.
  • Uses Loop Control Variable.
  • Improves Code Reusability.
  • Reduces Program Length.
  • Essential for Iterative Programming.

Types of Looping Statements

  1. while Loop
  2. do-while Loop
  3. for Loop

1. while Loop

while рдПрдХ Entry Controlled Loop рд╣реИред рдЗрд╕рдореЗрдВ рдкрд╣рд▓реЗ Condition Check рд╣реЛрддреА рд╣реИ рдФрд░ рдпрджрд┐ Condition True рд╣реЛрддреА рд╣реИ рддрднреА Loop Execute рд╣реЛрддрд╛ рд╣реИред

Syntax

while(condition) {   // Statements }

Example

int i = 1; while(i <= 5) {   cout << i << endl;   i++; }

Features of while Loop

  • Entry Controlled Loop.
  • Condition Checked First.
  • May Execute Zero Times.
  • Suitable for Unknown Iterations.

2. do-while Loop

do-while рдПрдХ Exit Controlled Loop рд╣реИред рдЗрд╕рдореЗрдВ рдкрд╣рд▓реЗ Loop Body Execute рд╣реЛрддреА рд╣реИ рдФрд░ рдЙрд╕рдХреЗ рдмрд╛рдж Condition Check рдХреА рдЬрд╛рддреА рд╣реИред рдЗрд╕рд▓рд┐рдП рдпрд╣ Loop рдХрдо-рд╕реЗ-рдХрдо рдПрдХ рдмрд╛рд░ рдЕрд╡рд╢реНрдп Execute рд╣реЛрддрд╛ рд╣реИред

Syntax

do {   // Statements } while(condition);

Example

int i = 1; do {   cout << i << endl;   i++; } while(i <= 5);

Features of do-while Loop

  • Exit Controlled Loop.
  • Executes At Least Once.
  • Condition Checked at End.
  • Suitable for Menu Driven Programs.

Entry Controlled Loop vs Exit Controlled Loop

Entry Controlled Exit Controlled
Condition Checked Before Execution Condition Checked After Execution
May Execute Zero Times Executes At Least Once
Example : while, for Example : do-while

3. for Loop

for Loop рд╕рдмрд╕реЗ рдЕрдзрд┐рдХ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рдиреЗ рд╡рд╛рд▓рд╛ Loop рд╣реИред рдЗрд╕рдореЗрдВ Initialization, Condition рддрдерд╛ Increment/Decrement рдПрдХ рд╣реА Statement рдореЗрдВ рд▓рд┐рдЦреЗ рдЬрд╛рддреЗ рд╣реИрдВред рдпрд╣ рдирд┐рд╢реНрдЪрд┐рдд (Fixed) Iterations рд╡рд╛рд▓реЗ Programs рдХреЗ рд▓рд┐рдП рд╕рдмрд╕реЗ рдЙрдкрдпреБрдХреНрдд Loop рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИред

Syntax

for(initialization; condition; increment) {   // Statements }

Example of for Loop

for(int i = 1; i <= 5; i++) {   cout << i << endl; }

Features of for Loop

  • Entry Controlled Loop.
  • Initialization, Condition and Increment in One Statement.
  • Suitable for Fixed Iterations.
  • Easy to Read and Maintain.
  • Most Frequently Used Loop in C++.

Infinite Loop

рдпрджрд┐ Loop рдХреА Condition рдХрднреА false рдирд╣реАрдВ рд╣реЛрддреА, рддреЛ Loop рд▓рдЧрд╛рддрд╛рд░ Execute рд╣реЛрддрд╛ рд░рд╣рддрд╛ рд╣реИред рдЗрд╕реЗ Infinite Loop рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред Infinite Loop рд╕рд╛рдорд╛рдиреНрдпрддрдГ Programming Mistake рдХреЗ рдХрд╛рд░рдг рдмрдирддрд╛ рд╣реИ, рд▓реЗрдХрд┐рди рдХреБрдЫ рд╡рд┐рд╢реЗрд╖ Applications рдЬреИрд╕реЗ Servers рддрдерд╛ Operating Systems рдореЗрдВ рдЗрд╕рдХрд╛ рдЙрдкрдпреЛрдЧ рднреА рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

while(true) {   cout << "Running..."; }

Nested Loop

рдЬрдм рдПрдХ Loop рдХреЗ рдЕрдВрджрд░ рджреВрд╕рд░рд╛ Loop рд▓рд┐рдЦрд╛ рдЬрд╛рддрд╛ рд╣реИ, рддрдм рдЙрд╕реЗ Nested Loop рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕рдХрд╛ рдЙрдкрдпреЛрдЧ Pattern Printing, Matrix Operations рддрдерд╛ Multi-Dimensional Data Processing рдореЗрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

for(int i = 1; i <= 3; i++) {   for(int j = 1; j <= 3; j++)   {     cout << "* ";   }   cout << endl; }

Loop Control Variable

Loop рдХреЛ рдирд┐рдпрдВрддреНрд░рд┐рдд рдХрд░рдиреЗ рд╡рд╛рд▓реЗ Variable рдХреЛ Loop Control Variable рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред рдпрд╣ Variable Initialization, Condition Checking рддрдерд╛ Increment/Decrement рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

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

рдКрдкрд░ рджрд┐рдП рдЧрдП рдЙрджрд╛рд╣рд░рдг рдореЗрдВ i Loop Control Variable рд╣реИред


Difference between while, do-while and for

Feature while do-while for
Loop Type Entry Controlled Exit Controlled Entry Controlled
Condition Check Beginning End Beginning
Minimum Execution 0 Times 1 Time 0 Times
Best Use Unknown Iterations Menu Driven Programs Fixed Iterations

Flowchart of Loop

Loop рдХрд╛ Flow рд╕рд╛рдорд╛рдиреНрдпрддрдГ рддреАрди Steps рдкрд░ рдЖрдзрд╛рд░рд┐рдд рд╣реЛрддрд╛ рд╣реИ тАФ Initialization, Condition Checking рддрдерд╛ Increment/Decrementред Condition True рд░рд╣рдиреЗ рддрдХ Loop Body рдмрд╛рд░-рдмрд╛рд░ Execute рд╣реЛрддреА рд░рд╣рддреА рд╣реИ рдФрд░ Condition False рд╣реЛрддреЗ рд╣реА Program Loop рд╕реЗ рдмрд╛рд╣рд░ рдирд┐рдХрд▓ рдЬрд╛рддрд╛ рд╣реИред


Common Errors

  • Missing Increment or Decrement Statement.
  • Wrong Loop Condition.
  • Infinite Loop Creation.
  • Using Incorrect Loop Variable.
  • Missing Curly Braces.
  • Incorrect Initialization.

Best Practices

  • Initialize Loop Variables Properly.
  • Always Update Loop Control Variable.
  • Avoid Infinite Loops.
  • Use for Loop for Fixed Iterations.
  • Use while Loop for Unknown Iterations.
  • Keep Loop Body Simple and Readable.

Advantages of Looping Statements

  • Reduces Code Duplication.
  • Improves Code Reusability.
  • Simplifies Repetitive Tasks.
  • Enhances Program Readability.
  • Supports Large Data Processing.
  • Reduces Development Time.

Limitations

  • Wrong Condition May Create Infinite Loop.
  • Nested Loops Can Reduce Performance.
  • Difficult to Debug Complex Loops.

Applications

  • Pattern Printing.
  • Array Traversing.
  • Searching Algorithms.
  • Sorting Algorithms.
  • Database Record Processing.
  • Scientific Calculations.
  • Game Development.
  • Artificial Intelligence.
  • Operating Systems.

Industrial Importance

Looping Statements рдкреНрд░рддреНрдпреЗрдХ Professional Software рдХрд╛ рдорд╣рддреНрд╡рдкреВрд░реНрдг рднрд╛рдЧ рд╣реИрдВред Operating Systems, Database Management Systems, Banking Software, Artificial Intelligence, Machine Learning, Scientific Computing рддрдерд╛ Embedded Systems рдореЗрдВ рдмрд╛рд░-рдмрд╛рд░ рд╣реЛрдиреЗ рд╡рд╛рд▓реЗ Operations рдХреЛ Efficient рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдП Loops рдХрд╛ рд╡реНрдпрд╛рдкрдХ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред


Viva Questions

  1. What is a Loop?
  2. Why are Loops used?
  3. What is while Loop?
  4. Explain do-while Loop.
  5. What is for Loop?
  6. What is an Infinite Loop?
  7. What is a Nested Loop?
  8. Differentiate while and do-while.
  9. Which Loop is Entry Controlled?
  10. Which Loop is suitable for Fixed Iterations?

Exam Oriented Important Questions

  1. Define Loop with suitable examples.
  2. Explain while Loop with syntax and example.
  3. Explain do-while Loop.
  4. Describe for Loop with syntax.
  5. Differentiate while, do-while and for Loop.
  6. Explain Infinite Loop and Nested Loop.
  7. Discuss the applications of Looping Statements.
  8. Explain the Industrial Importance of Loops.

Conclusion

Looping Statements C++ Programming рдХрд╛ рдПрдХ рдЕрддреНрдпрдВрдд рдорд╣рддреНрд╡рдкреВрд░реНрдг Control Structure рд╣реИрдВред while, do-while рддрдерд╛ for Loop рдХреА рд╕рд╣рд╛рдпрддрд╛ рд╕реЗ Repetitive Tasks рдХреЛ рд╕рд░рд▓, рддреЗрдЬ рддрдерд╛ Efficient рдмрдирд╛рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред рд╕рд╣реА Loop рдХрд╛ рдЪрдпрди Program рдХреА Readability, Performance рддрдерд╛ Maintainability рдХреЛ рдмреЗрд╣рддрд░ рдмрдирд╛рддрд╛ рд╣реИред рдЗрд╕рд▓рд┐рдП рдкреНрд░рддреНрдпреЗрдХ Computer Engineering Student рдХреЗ рд▓рд┐рдП Looping Statements рдХреА рдЧрд╣рди рд╕рдордЭ рдЖрд╡рд╢реНрдпрдХ рд╣реИред

Related Articles

Arrays in C++ Notes | One Dimensional & Two Dimensional Arrays with Programs | Basic Computer Engineering | RGPV BTech First Year

Arrays ...

Read More тЖТ

Control Statements in C++ Notes | Decision Making & Looping Statements | Basic Computer Engineering | RGPV BTech First Year

Control...

Read More тЖТ

Expressions in C++ Notes | Types of Expressions, Evaluation & Expression Statements | Basic Computer Engineering | RGPV BTech First Year

Express...

Read More тЖТ

Operators in C++ Notes | Arithmetic, Relational, Logical, Assignment, Bitwise Operators | Basic Computer Engineering | RGPV BTech First Year

Operato...

Read More тЖТ

Program Structure in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

Program...

Read More тЖТ