-1
#include <iostream>

using namespace std;

int main() {
  int n = 1; // initialising n with 1
  int num = 2 * n;
  do {
    cout << "value =" << num << endl;
    n = n++;
  } while (n <= 10); // intents to print "value = 2n for all 'n' from 1 to 10

  return 0;
}

It just keeps on displaying 2.

n is getting incremented, but the value of num is not changing. Also the counting doesn't terminate.

Output:

value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2
value =2

I guess it has something to do with the logic I used. Would love to know how to go about questions that uses loops.

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 4
    You always print value `num` without changing it. – vahancho Apr 09 '21 at 08:05
  • 2
    You never calculate the new value of `num` anywhere. Code is executed in a top-to-bottom fashion, and code outside of loops will not be evaluated or executed again. – Some programmer dude Apr 09 '21 at 08:05
  • 2
    `int num=2*n` means `num` is set to `2*n`. It keeps that value until you set it to something else. It does not 'track' changes to `n`. If you want that, use a function. I can only recommend a good book or tutorial before asking on SO. – underscore_d Apr 09 '21 at 08:05
  • 4
    `int num=2*n;` is not an equation that defines a relationship between `num` and `n`. Get yourself a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Apr 09 '21 at 08:06
  • 2
    There are several ways to incremement a variable, but `n = n++;` is not one of them. Choose from `n++`, `++n`, `n += 1`, `n = n + 1`. – molbdnilo Apr 09 '21 at 08:17

4 Answers4

2
  1. You print num but you increment n
  2. Incrementing n with n = n++ is fishy, it should be n++;, n = n + 1; or n += 1;

So you actually want this:

#include <iostream>
using namespace std;
int main()
{
  int n = 1; //initialising n with 1
 
  do
  {
    cout << "value =" << n * 2 << endl;
    n++; // or 'n = n + 1;'
  } while (n <= 10);  //intents to print "value = 2n for all 'n' from 1 to 10

  return 0;
}

Be aware that int num=2*n; defines and initializes num with 2*n. After that num will keep its value until you assign something else to it which you never do in your code.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
2

After this two lines:

int n=1; //initialising n with 1
int num=2*n;

The value of num is 2. You never change is after that. int num=2*n; does not make num be twice the value of n when n gets modified later. It makes num equal twice the value of num at the point of that initialization. You want to assign num inside the loop, or simply print the value directly:

do
{
  cout << "value =" << n*2 << endl;
  n+=1;
}
while(n<=10);

If you want num to update depending on the value of n you can make it a function:

int num(int x) { return 2*x; }

and then

do
{
  cout << "value =" << num(n) << endl;
  n+=1;
}
while(n<=10);
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • After the first two lines, the value of `n` is not `2`. It is `1`. The value of `num` is `2`. And, for most C or C++ developers with even a basic knowledge, `++n` is understandable as an operation that increments `n` and gives a result that is the incremented value (i.e. is equivalent to `n = n + 1` or to `n += 1`) – Peter Apr 09 '21 at 08:55
  • @Peter fixed the typo. Yes, `n +=1` and `++n` are equivalent, but `n = n++` was wrong, thats why I fixed it – 463035818_is_not_an_ai Apr 09 '21 at 08:58
  • thanks for the explanation. i get it now –  perfectly_ flawed Apr 09 '21 at 10:03
1

Two mistakes are there

  1. if you see, You have written

    n=n++;

Here You have used Post Increment so because of it the value of n is not changing through out of the loop. You can use Pre Increment n = ++n instead of n = n++, but I will recommend you to use just,n++ or n+=1 or n=n+1

2.You have initialized num = n * 2 But you did not updated it in do while, so below is the code in which I had updated your first mistake as well as your second mistake,

do {
cout << "value =" << num << endl;
n = n+1;
num = n*2;
}while (n <= 10); // intents to print "value = 2n for all 'n' from 1 to 10
0

just do multiplication operation inside do block and use n++ or simply n=n+1 that's it

#include <iostream>
using namespace std;
int main() 
{
 int n=1; //initialising n with 1
 do
 {
  int num=2*n;
  cout<<"value ="<<num<<endl;
  n=n+1;
 }
 while(n<=10);  //intents to print "value = 2n for all 'n' from 1 to 10
 
return 0;
  
}