Hi all, I am bit confused about this question: What is the value of i after the for statement is done?

int n = 100;
int m;
n = m;
for (int i = 0; i < ; i++) {
..
}

I tried to write it down all the step from when i = 1,2,3,4,5,..and so on, when it comes to i = 100 the loop will exit - so meaning: i will print 99 times

Am I right here? But when I tried to use compiler to run it, all is, i = 100 and it printed 100 of times.

Can someone please clarify me about this.
Thanks

The problem with you logic is that i is starting out at 0 not 1. Since 0 is a number you have 0, 1, 2, 3, ... 98, 99. The reason i is 100 is because when i is 99 it get incremented to 100 and then it is checked against the condition of the for loop. It fails so the loop end but that doesn't change the fact that i is still 100.

The problem with you logic is that i is starting out at 0 not 1. Since 0 is a number you have 0, 1, 2, 3, ... 98, 99. The reason i is 100 is because when i is 99 it get incremented to 100 and then it is checked against the condition of the for loop. It fails so the loop end but that doesn't change the fact that i is still 100.

So the value of the first i is 0 and the value of last i is 100? am I correct to say this?

Is this what you mean??

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
	int n = 100;
	int m;

	m = n;

for (int i = 0; i < m; i++)
{
   cout << "Your number is: " <<  i << endl;
}
	system("pause");
	return 0;
}

Thanks Nathan and Zvjezdan23;

Yes I just need to know the value of i; but thanks for the coding, I am a very beginner in c++ so my understanding is very limited. But thanks.

One quick question though, I like to ask about char when it has e.g i+=2

For example If we have a string s which holing the character “a”, what is the effect of the statement: s + = 2?

Would s holds “aaa” or would it be compiler error?

I think error. Anyways, if i have fixed anything call this thing solved and vote up if i did what you needed or explained something somehow.

Thanks; are you sure it give an error? error run time or compiler error? or would s be hold “aaa” or “a2” ?

And how can I call this thread solve? Which button I have to press..

Thanks again.

Edited 4 Years Ago by siabenie: n/a

I am not entirely sure. ima noob too. but vote if i did good though. it helps out so it can show the forum that I am a help.

I believe i + 2 would be an error because of the fact that you used a number and string. But like I say again, i'm not entirely sure.

I believe i + 2 would be an error because of the fact that you used a number and string. But like I say again, i'm not entirely sure.

Ok thanks, well if it is an error what error would it be? Compiler error or run-time error?

Anyone please?

it would increase the ascii value of the variable s by 2...'a' is internally represented as dec 97, so adding 2 would give dec 99 which is 'c'.

it would increase the ascii value of the variable s by 2...'a' is internally represented as dec 97, so adding 2 would give dec 99 which is 'c'.

Thanks for the input; so is that mean that it would be compiler error or would it be run-time error?

it wouldnt be an error at all, if you printed the char s after doing s+=2, it would print as 'c'

it wouldnt be an error at all, if you printed the char s after doing s+=2, it would print as 'c'

I know; what I am trying to say here is, as Im trying this exercise on the book it gives me choices of 4: which are:

If s is a string holding the character "a", what is the effect of the statement: s += 2;
(a) s holds "a2”
(b) s holds "aaa”
(c) It's a compiler error
(d) It's a run-time error

It did not give me as you said…so I would assume it would be an error but not sure what sort of error it is?

Edited 4 Years Ago by siabenie: n/a

ahh, I was assuming that the variable 's' was a char, not a string.

std::string has a operator overload for the '+' operator which would append the internal representation of the char* with the specified new characters.

I am not on my dev computer at the moment, so cant test, but I would assume that it would give a run-time error.
It would depend on whether the compiler passes the 2 as a pointer representation (thinking it is a char *), or as a char literal (more likely). Either way, it is probably not print friendly.

I think that string objects have a private char* which represents the contents, and as 2 is still in the valid char range, the compiler should have no problems adding it to the string.

Long story short, I cant test it, but I would put my money on the run-time error

If you had

char ch = 'a';
a += 2;
cout << a;

It would display c . This is because a char is the same as a 1 byte integer. It actually holds a number between 0 and 255. If you are dealing with ascii characters then 'a' is equal to 97. If you add 2 to that you get 99 which is the value of 'c'.

You know, if you have a compiler you can just type in the code and see for yourself. Rather than say 'what would the value be' write the code and output what the value is. Then you'd know for sure in minutes rather than 3.5 hours and counting.

The problem with your logic is that I start at 0, not 1. Since 0 is a number you have 0, 1, 2, 3, ... 98, 99. The reason I was 100 because when i is incremented by 99 to get to 100 and it is checked against the condition of the for loop. So it fails at the end of loop, but that does not change the fact that I still have 100.

that is very bad code, m is uninitialised and your assigning it to n.

i dont know why you wouldnt just code

for(int i=0; i<100; i++){
cout << i;
}

Thanks Nathan and Zvjezdan23;

Yes I just need to know the value of i; but thanks for the coding, I am a very beginner in c++ so my understanding is very limited. But thanks.

One quick question though, I like to ask about char when it has e.g i+=2

For example If we have a string s which holing the character “a”, what is the effect of the statement: s += 2?

Would s holds “aaa” or would it be compiler error?

when adding a number to a character, it first checks the ASCII code of that character and then adds the number to it, 'a'+= means taking that the a's ASCII and adding 2 to it, i think the answer will be c then...u could try it by outputting the char ..
try this:

#include<iostream.h>
#include<stdio.h>

void main()
{
    char x;
    x='a';
    x+=2;
    cout<<"a has become :"<<x<<endl;
}

Edited 3 Years Ago by pyTony: fixed formating

This article has been dead for over six months. Start a new discussion instead.