Question about boolean expression..

Posted on 2005-04-26
Last Modified: 2010-04-01
I am studying c++ and I am trying to figure out a question for a practice exam. It goes as follows:

Assume you have three int variables x =2, y=6 and z. Choose the value of z in the following:

z = (y/z>0) ? x : y;

am I correct in stating z=3 because the "?" is basically stating "if the previous expression is true (y/z > 0) then z =x, if it is false then y = z ? I am just making sure I am clear on what this expression is saying...

Thank you for your help...
Question by:Calv1n
    LVL 30

    Accepted Solution

    Hi Calv1n,
    > >m I correct in stating z=3
    z would equal 2

    David Maisonave :-)
    LVL 30

    Expert Comment

    > >if the previous expression is true (y/z > 0) then z =x
    That's right, but x equals 2, and therefore z will equal 2

    David Maisonave :-}
    LVL 15

    Expert Comment

    How do you know y/z > 0 when you don't know what the value of z is?  It seems to me that y/z > 0 is true only when z is in the range from 1 to 6.  Am I missing something?
    LVL 10

    Expert Comment

    Ok, the expression says as u said above
    But the final value of z would depend on the value of z befoe the expr is evaluated
    For example,
    if initially z = 1 to 6, then y/z >0, thus, z = x, i.e. z = 2 in this case
    if initially z > 6 or z < 0, then, then y/z <= 0, thus z = y, i.e. z = 6 in this case


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now