hello geys,

i have some codes and i tried to find the output for each of them. most of the codes i answered
but there are some codes which are strange for me (the output)

first, i want from every one is to find the output without test it, please tell us if find the corrcet answer directly or not? if you find the correct output please explain to me because i tried many times to understand the code but i couldn't

first code is

int x;
	for(x =1; x < 5; x++)
	x = x + 2;
	cout << x;

my answer was 3 but it's not correct.

* * * * *

int m[5] = {7,12,3,99,65};
	cout << *(m + 2);

no answer for me:sad:

* * * * *

int y =8;
	if(y != 8)
	y++;
	y++;

my answer was 10

* * * * *

char *word = "executing";
	word = word + 4;
	cout << word;

no answer, could you please tell me what is the name of the star? is it a pointer? because i want to search about it

please notice that i'm beginner and i'm learning

waiting for you

First code (reformatted for clarity):

int x;
for(x =1; x < 5; x++)
  x = x + 2;
cout << x;

I'll give you this one since you tried. The output is 7. The variable x starts at 1, then 2 is added. The value of x is now 3. At the end of the loop iteration, x is incremented, value is now 4. 4 is less than 5 so the loop executes again and another 2 is added making x = 6. The loop iteration ends and x is incremented, value is now 7. 7 is greater than 5, therefore the loop does not execute. The next command displays x, whose current value is 7.

Second code:
This is what is called "pointer arithmetic." The answer will be the same as m[2]. Remember, array indexes start at (0).

Third code:
The variable y is initialized to 8. Last I checked, 8 == 8. How do if statements work?

Fourth code:
This is a string output. But the first few characters are cut off. You need to figure out how many. Hint: look at my answer for code 2.

thank you for your replying


*sorry the code is working now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.