For Loops | C++ for EmSAT Achieve PDF Download

Introduction

For Loops are a type of loop in C++ that allows a specific block of code to be executed repeatedly until a certain condition is met. It is especially useful when you know how many times the loop should run.
The For Loop is the most commonly used loop in C++, and is used to iterate through arrays, strings, and other collections of data. In this article, we will go over the syntax, flowchart, and examples of the For Loop.

Syntax of For Loops

The syntax of the For Loop is as follows:

for (initialization; condition; increment/decrement)

{

   // statement(s)

}

Here's a breakdown of each component of the For Loop:

  • Initialization: This is where you declare and initialize the loop control variable. This variable is used to keep track of how many times the loop has been executed.
  • Condition: This is the condition that is checked at the beginning of each iteration of the loop. If the condition is true, the loop continues to execute. If the condition is false, the loop stops executing.
  • Increment/Decrement: This is where you update the loop control variable. This is typically done by incrementing or decrementing the variable by 1, although you can increment or decrement by any value.
  • Statement(s): This is the code that is executed each time the loop iterates. It can be any valid C++ code, including conditional statements, function calls, and input/output statements.

Flowchart of For Loops

A flowchart is a graphical representation of the For Loop that shows the flow of control through the loop. Here's an example of a flowchart for a basic For Loop:

For Loops | C++ for EmSAT Achieve

Examples of For Loops

Example 1: Basic For Loop 

Here's an example of a basic For Loop that prints the numbers 1 to 5:

#include <iostream>

int main()

{

   for (int i = 1; i <= 5; i++)

   {

      std::cout << i << std::endl;

   }

   return 0;

}

Output

1

2

3

4

5

In this example, the loop control variable i is initialized to 1. The condition checks whether i is less than or equal to 5. If the condition is true, the loop executes and prints the value of i. The loop control variable is then incremented by 1, and the condition is checked again. This process continues until i is no longer less than or equal to 5.

Example 2: For Loop with Break Statement
Here's an example of a For Loop that uses a break statement to exit the loop:

#include <iostream>

int main()

{

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

   {

      std::cout << i << std::endl;

      if (i == 5)

      {

         break;

      }

   }

   return 0;

}

Output

1

2

3

4

5

In this example, the loop control variable i is initialized to 1. The condition checks whether i is less than or equal to 10. If the condition is true, the loop executes and prints the value of i. The loop control variable is then incremented by 1, and the condition is checked again. However, there is a break statement that is executed when i is equal to 5. This causes the loop to exit before i reaches 10.

Example 3: For Loop with Nested Loops
Here's an example of a For Loop with nested loops that prints the multiplication table for numbers 1 to 5:

#include <iostream>

int main()

{

   for (int i = 1; i <= 5; i++)

   {

      std::cout << "Multiplication table for " << i << std::endl;

      for (int j = 1; j <= 10; j++)

      {

         std::cout << i << " x " << j << " = " << i * j << std::endl;

      }

      std::cout << std::endl;

   }

   return 0;

}

Output

Multiplication table for 1

1 x 1 = 1

1 x 2 = 2

1 x 3 = 3

1 x 4 = 4

1 x 5 = 5

1 x 6 = 6

1 x 7 = 7

1 x 8 = 8

1 x 9 = 9

1 x 10 = 10

Multiplication table for 2

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

2 x 10 = 20

Multiplication table for 3

3 x 1 = 3

3 x 2 = 6

3 x 3 = 9

3 x 4 = 12

3 x 5 = 15

3 x 6 = 18

3 x 7 = 21

3 x 8 = 24

3 x 9 = 27

3 x 10 = 30

Multiplication table for 4

4 x 1 = 4

4 x 2 = 8

4 x 3 = 12

4 x 4 = 16

4 x 5 = 20

4 x 6 = 24

4 x 7 = 28

4 x 8 = 32

4 x 9 = 36

4 x 10 = 40

Multiplication table for 5

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50

In this example, the outer loop controls the multiplication table that is being printed. The inner loop prints the multiplication table for each number, from 1 to 10.

Conclusion

The For Loop is a useful and powerful construct in C++. It allows you to iterate over a set of data, executing a specific block of code each time. Understanding the syntax and flowchart of the For Loop is essential to using it effectively in your programs.

The document For Loops | C++ for EmSAT Achieve is a part of the EmSAT Achieve Course C++ for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
70 videos|45 docs|15 tests

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

practice quizzes

,

Objective type Questions

,

mock tests for examination

,

For Loops | C++ for EmSAT Achieve

,

shortcuts and tricks

,

MCQs

,

Viva Questions

,

Free

,

For Loops | C++ for EmSAT Achieve

,

Summary

,

ppt

,

Extra Questions

,

pdf

,

Exam

,

video lectures

,

past year papers

,

Important questions

,

For Loops | C++ for EmSAT Achieve

,

Previous Year Questions with Solutions

,

Semester Notes

,

study material

,

Sample Paper

;