just wondering

This is a discussion on just wondering within the C++ Programming forums, part of the General Programming Boards category; ok i have the book teach yourself C++ in 21 days and one of the sections is this Blocks and ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    176

    just wondering

    ok i have the book teach yourself C++ in 21 days and one of the sections is this

    Blocks and Compound Statements
    Any place you can put a single statement, you can put a compound statement, also called a block. A block begins with an opening brace ({) and ends with a closing brace (}). Although every statement in the block must end with a semicolon, the block itself does not end with a semicolon. For example

    {
    temp = a;
    a = b;
    b = temp;
    }

    This block of code acts as one statement and swaps the values in the variables a and b.

    and i was wondering what you would need that for? it seems like its just a circle.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    This could be used for sorting, for example

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "and i was wondering what you would need that for?"

    The code in your example switches the values in a and b. Let's say you started out with:

    a=5
    b=10

    and you wanted to switch the values. If you tried this:

    a=b;
    b=a;

    the first statement would assign 10 to a, so you would have:

    a=10
    b=10

    The second statement assigns a to b, and since a=10, you assign 10 to b, so you have:

    a=10
    b=10

    So, in the end were you able to switch the values? No, you wanted to end up with:

    a=10
    b=5

    To switch values in two variables, you have to create a temp variable because your first assignment is going to overwrite the value in that variable and it will be lost forever unless you assign the value to temp first.

    temp = a; ----->temp=5, a=5, b=10
    a=b; ---------->temp=5, a=10, b=10
    b=temp-------->temp=5, a=10, b=5
    Last edited by 7stud; 04-26-2003 at 06:23 PM.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    sreetvert83, ignore this post. I'm just trying to confuse others



    This code:
    a=b;
    b=a;
    will work in a certain case. What's this case? (Hint: the variables don't have to be the same value)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch/case - Just wondering
    By Snip in forum C Programming
    Replies: 3
    Last Post: 05-22-2005, 08:04 AM
  2. wondering about people here
    By moemen ahmed in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-08-2002, 10:33 PM
  3. wondering
    By xlordt in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 03-01-2002, 07:05 AM
  4. Just wondering
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-13-2002, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21