[Beginner] C++ Questions

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 773 Views - Last Post: 12 May 2008 - 06:02 PM Rate Topic: -----

#1 SebKom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 16-June 07

[Beginner] C++ Questions

Posted 12 May 2008 - 04:28 PM

Hello there,

I am going through some past exams of my course and I am stuck on this:

In the following the output for x and y is different. State what is obtained and explain why the outputs are different.

#include <iostream> 
using namespace std; 
int main() 
{ 
int i; 
double x=0, y=0; 
for(i=1; i<=5; i++) x=x+i; 
for(i=1; i<=5; i++); y=y+i; 
cout << "x=" << x << endl; 
cout << "y=" << y << endl; 
return 0; 
}


Apart from the ";" on the second for-loop I can find no difference, I just can't! Any help will be appreciated...

This post has been edited by SebKom: 12 May 2008 - 04:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [Beginner] C++ Questions

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 04:35 PM

That's all it's asking for - to spot the semi-colon.

Basically, by adding a semi-colon after the for loop, it wont add i to the value of y each time, i will just become 5 and then that will be added on to y. That doesn't really sound too good, so let's annotate the code:
#include <iostream>
using namespace std;
int main()
{
    int i;
    double x=0, y=0;
    for(i=1; i<=5; i++)
        x = x + i; // adds i to the value of x
    for(i=1; i<=5; i++); // by adding a semi-colon, nothing
                        // is run by this loop. In other words,
                        // it will iterate, but it won't do anything
                        // to any code that follows
    
    // so now i = 5 because the for loop was executed successfully
    y=y+i;
    
    cout << "x=" << x << endl;
    cout << "y=" << y << endl;
    
    return 0;
}

If anything is still unclear, I'll do my best to break it down further.

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 SebKom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 16-June 07

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 04:41 PM

Are you saing that if the second loop was like that "for (i=1; i<=7; i++);" the "y=y+i" would be "y=0+7"?

My second question now:

Can I have something like "if just_a_number_here statement"? When is that true? I mean, is "if 0" true or false and why?

This post has been edited by SebKom: 12 May 2008 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 04:51 PM

You can, but it would be pointless. It would always execute. Take this as an example:
#include <iostream>

using namespace std;

int main ()
{
    if (4)
        cout << "statement executed." << endl;

    system ("pause");
    return 0;
}

So, because 4 isn't an acceptable condition, it is always true. So, no matter what you put in that if () statement, it will always be executed. That's why we use conditions such as:
bool Again = true;
if (Again == false)
    cout << "See you then." << endl;

int a = 6;
if (a != 6)
    cout << "a is not equal to 6" << endl;

The first example would execute only if Again was false.
The second would only execute if a was not equal to 6, and since we assigned 6 to it right before the if statement, this one WON'T be carried out.
Was This Post Helpful? 0
  • +
  • -

#5 SebKom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 16-June 07

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:05 PM

What about this then?

The following program outputs the word TRUE 7 times and the word FALSE 3 times. Explain why this happens.

#include <iostream> 
using namespace std; 
int main() 
{ 
int i, j; 
for(i=0; i<=9; i++) 
{ 
j=i; 
if(j=i/3) cout << "TRUE" << endl; 
else cout << "FALSE" << endl; 
} 
return 0; 
} 


(j=i/3) is 0 for i=0,1,2
(j=i/3) is 1 for i=3,4,5
(j=i/3) is 2 for i=6,7,8
and (j=i/3) is 3 for i=9

Where do the 7 trues and 3 falses come from?

From what you said (or, from what I understood from what you said, to be precise and sincere :P), it should be 10 trues.

This post has been edited by SebKom: 12 May 2008 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:12 PM

That's because of if (j=i/3)
It kinda has the same effect as if j < 3, basically, if at least ONE 3 can go into i, it will print TRUE, otherwise, it will print FALSE :P
Was This Post Helpful? 0
  • +
  • -

#7 SebKom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 16-June 07

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:19 PM

:huh: I think I am lost... :unsure:

Do you "translate" the j=i/3 in a way I don't understand? How would you translate j=i/x (x being an integer) then?
Was This Post Helpful? 0
  • +
  • -

#8 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:27 PM

Here is what the ISO C Specification says about the if statement

Quote

WG14/N843 Committee Draft -- August 3, 1998 147


selection-statement:
if ( expression ) statement
if ( expression ) statement else statement
switch ( expression ) statement

Semantics

[#2] A selection statement selects among a set of statements
depending on the value of a controlling expression.

6.8.4.1 The if statement

Constraints

[#1] The controlling expression of an if statement shall
have scalar type.

Semantics

[#2] In both forms, the first substatement is executed if
the expression compares unequal to 0. In the else form, the
second substatement is executed if the expression compares
equal to 0. If the first substatement is reached via a
label, the second substatement is not executed.

[#3] An else is associated with the lexically nearest
preceding if that is allowed by the syntax.


In particular read constraint #2.

"(j = i / 3)" is being interpreted as integer division meaning
j = 1 / 3 == 0
j = 2 / 3 == 0
j = 3 / 3 == 1
j = 4 / 3 == 1
j = 5 / 3 == 1
j = 6 / 3 == 2
...

This post has been edited by skaoth: 12 May 2008 - 05:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:31 PM

EDIT: Dammit skaoth, you always seem to go one better =/

Oh crap, I misinterpreted that one. In the if statement, you have a = (assignment operator), where you need a == (equal to) operator. The = will assign j a value, and if it isn't 0, then it will be executed.

So, if you have if (j=i/4), it will only execute if j can equal i / 4 (in other words, if i is greater than 4.)

It's kinda hard to explain, sorry :(

This post has been edited by gabehabe: 12 May 2008 - 05:33 PM

Was This Post Helpful? 0
  • +
  • -

#10 SebKom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 16-June 07

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:33 PM

I see. So (j=i/3) is false when j=0 and true when j><0, right?
Was This Post Helpful? 0
  • +
  • -

#11 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:37 PM

Absolutely right :)
Was This Post Helpful? 0
  • +
  • -

#12 SebKom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 16-June 07

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:44 PM

Yay, I was worried of getting it wrong again and you thinking I am stupid! :P

Does the same apply if we have no algebra in? I mean, j=0 is false and j=x (any integer except 0) is right, right?

What about negatives? (Hope I am not becoming annoying! :$)

This post has been edited by SebKom: 12 May 2008 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

#13 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:46 PM

That's right, and it can apply to negatives exactly the same as it applies to positives.
Was This Post Helpful? 0
  • +
  • -

#14 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:52 PM

Quote

I see. So (j=i/3) is false when j=0 and true when j><0, right?

oops.. minor bout of dyslexia
Ignore ::blink:

This post has been edited by skaoth: 12 May 2008 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

#15 SebKom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 16-June 07

Re: [Beginner] C++ Questions

Posted 12 May 2008 - 05:57 PM

lol, ok, glad to read the edit! :P

I am going for a brake, hope to catch ya later, I think the night will be long... ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2