Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 12-02-2004, 09:24 AM   #1 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default While(!x) ???...in c++

there was a loop in the midterm (c++) like this:

x = 0;
y= 0;
while(!x)
{
etc...etc...
}
etc....



what is important to me is what da **** does not X mean?
i think y != x would sound much understandable but !x alone i don't understand...
can some1 help me plz?
DemonEdge is offline  
Old 12-02-2004, 09:26 AM   #2 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

DemonEdge,

while (!x) means while (1) which means the while condition is true and evaluates the while function.
intercodes is offline  
Old 12-02-2004, 09:26 AM   #3 (permalink)
 
Junior Techie

Join Date: Oct 2004

Posts: 63

See Plus Plus

Default

!x is any non-zero number.
__________________
I hate Pi
See Plus Plus is offline  
Old 12-02-2004, 09:50 AM   #4 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default

thx for the fast replies..
hmmm...
you say that while(!x) means while(1)
is that because the prof. declared the x = 0 @ the beginning?
and does that mean that the loop will run for ever?
DemonEdge is offline  
Old 12-02-2004, 09:57 AM   #5 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

DemonEdge,

yes, aslong as the while function has an integer value of '1' , the while condition is satisfied. So this loop runs infinitly, unless the 'etc' is replaced with some code.

Any value other than 1 in the 'while' will exit from the function.
intercodes is offline  
Old 12-02-2004, 10:27 AM   #6 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default

k the whole program is as follows:
( of course i didn't even manage to solve it)
**********************************************
#include <iostream.h>
void main()
{
int x = 0;
int y = 0;
while(!x)
{
switch(x)
{
case 0:
for(x=3;x>0;x--)
{
switch(y)
{
case 0:
cout<<"yes"<<endl;
break;
default:
cout<<"no"<<endl;
break;
}
}
if(y>0)
x = y;
break;
case 1:
break;
case 2:
breal;
}
y++;
}
}
*******************************************
so...can some1 explain this program to me in detail plz??
DemonEdge is offline  
Old 12-02-2004, 11:00 AM   #7 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

DemonEdge,

Quote:
int x = 0;
int y = 0;
while(!x)
{
'while' condition is true, and the function is evaluated..so it follows...

Quote:
while(!x)
{
switch(x)
{
case 0:
for(x=3;x>0;x--)
{
Now, here at the switch statement ,value of x is 0. So switch is executed i.e case 0 condition. ..

Quote:
for(x=3;x>0;x--)
{
switch(y)
{
case 0:
cout<<"yes"<<endl;
break;
default:
cout<<"no"<<endl;
break;
}
}
For 3 loops the above block will be executed [value of y is 0 , switch(y) satisfies].Produces output "yes" "yes" "yes"

Quote:
if(y>0)
x = y;
break;
case 1:
break;
case 2:
breal;
}
y++;
}
}
none of the above statement will be excuted except for y++ .so y=1 now.

The while loop continues , and enters the switch(y) fn where case 'default' is satisified ,so prints "no" no" "no".

y>0 satisifies, x=y is done , the while loop is terminated as while(!x) is false
intercodes is offline  
Old 12-02-2004, 11:11 AM   #8 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Code:
#include <iostream.h>
void main()
{
    int x = 0;         // Initialize the vars to zero
    int y = 0;
    // the NOT operator just means the oposite...since 0 can mean false and 1 can be mean true
    // He's esentially saying "If x means true, make it mean false...if x means false, make it mean true"
    // In this case, it means true, because it originally meant false
    while(!x)          
    {
	// Now he's trying to figure out, what x was originally
        switch(x)
        {
	    // If x was originally 0 (which, in this particular case it was do this...
            case 0:
		// He's now initializing x to 3 and decrementing it until it reaches 1
		// esentially that is 3 times. And since y is never changed, it will 
		// output "yes" to the screen 3 times.
               for(x=3;x>0;x--)
               {
		     // he now wants to find out what y equals
                     switch(y)
                     {
			  // in this case y (look at top of page) equals 0 so the first one will output
                          case 0:
			      // "yes" will be output
                              cout<<"yes"<<endl;
                              break;
                          default:
                              cout<<"no"<<endl;
                              break;
                     }
               }
		// since y is NOT greater than 0, x does not get changed
               if(y>0)
                   x = y;
               break;
	    // this fails because x = 0;
            case 1:
                break;
	    // same here, x does not = 2;
            case 2:
                breal;
        }
	// y now equals 1
        y++;
    }
}
You can take the rest from there..that should give you some basic idea about how the code operates.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On