I'm Having Difficulties in Understanding Looping Statement

closed account (Gw5fjzwU)
//Can Someone Explain How For-Loop Statement Work Using the following codes below?

//I'm also having difficulties in understanding how loop statement work especially the word "ctr++" and "a++; b+=5; c+=a;" bellow. Please explain how this work


#include <iostream>

using namespace std;

int main()
{
int a=1,b=1,c=1;
for (int ctr=c;ctr<=10;ctr++)
{

cout <<ctr<<" iteration" << endl;
a++; b+=5; c+=a;

cout << "\t a= "<<a<<endl;

cout << "\t b= "<<b<<endl;

cout << "\t c= "<<c<<endl;
}

return 0;
}

I'm also having difficulties on how does the output end up like this. I'm having a difficulty in understanding it also.

The output:

1 iteration
a= 2
b= 6
c= 3
2 iteration
a= 3
b= 11
c= 6
3 iteration
a= 4
b= 16
c= 10
4 iteration
a= 5
b= 21
c= 15
5 iteration
a= 6
b= 26
c= 21
6 iteration
a= 7
b= 31
c= 28
7 iteration
a= 8
b= 36
c= 36
8 iteration
a= 9
b= 41
c= 45
9 iteration
a= 10
b= 46
c= 55
10 iteration
a= 11
b= 51
c= 66
Last edited on

a++, same as a=a+1
b+=5, same as b=b+5
c+=a, same as c=c+a

Some languages don't use this type of short cut, it is a C/ (C++) thing.
Of course it doesn't make sense in the language of maths, put a=a+5 in an exam paper and you might just fail.

You can use this as a template
["code"] ["/code"]
Paste it into the box (minus the quotes) and write your code inside it.
Last edited on
I'm also having difficulties on how does the output end up like this.


Use the debugger to trace through the code and see the changes to the variables etc as each statement is executed.

It looks a bit better this way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main()
{
int a=1,b=1,c=1;
cout<<"\t a=1     "<<"b=1     "<<"c=1   "<<endl;
cout<<"\t a=a+1   "<<"b=b+5   "<<"c=c+a   "<<endl;
for (int ctr=c;ctr<=10;ctr++)
{

cout <<ctr<<" iteration" << endl;
a++; b+=5; c+=a;


cout << "\t a= "<<a;

cout << "\t b= "<<b;

cout << "\t c= "<<c<<endl;
}
cout<<"press return to end"<<endl;
cin.get();

return 0;
}  
no, it doesn't
the idea of the tags is to preserve indentation, the colours are just a bonus.
and you added noise at the end.
oggin wrote:
It looks a bit better this way

It could look a whole lot better, easier to read and understand with some formatting that includes indentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main()
{
   int a = 1, b = 1, c = 1;
   cout << "\t a=1     " << "b=1     " << "c=1   " << endl;
   cout << "\t a=a+1   " << "b=b+5   " << "c=c+a   " << endl;
   for (int ctr = c; ctr <= 10; ctr++)
   {

      cout << ctr << " iteration" << endl;
      a++; b += 5; c += a;


      cout << "\t a= " << a;

      cout << "\t b= " << b;

      cout << "\t c= " << c << endl;
   }
   cout << "press return to end" << endl;
   cin.get();

   return 0;
}

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

(The part about editing a post really applies to the starting post of a thread. The format buttons and preview don't work until AFTER the thread has been created.)

Just as there are code tags you should use, there are output tags as well, as explained in the two links. You really should read the articles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <ranges>

int main()
{
   std::string cat { "cat" };

   // https://en.cppreference.com/w/cpp/language/range-for
   for (const auto& itr : cat)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';

   // https://www.fluentcpp.com/2020/02/11/reverse-for-loops-in-cpp/
   for (const auto& itr : cat | std::views::reverse)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';

   // range-based for loops work with regular arrays as well
   int arr[] { 5, 10, 15, 20, 25 };

   for (const auto& itr : arr)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';

   for (const auto& itr : arr | std::views::reverse)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';
}
c a t
t a c
5 10 15 20 25
25 20 15 10 5
Last edited on
Topic archived. No new replies allowed.