Sign in to follow this  
kingofthebraves

need some help with basic code definitions

Recommended Posts

couldnt find anything on the net that i could understand so i thought of posting these codes here, can anyone please explain to me what it does and what specific programs i could use it with? <math.h> <conio.h> flag? while(v<0 || v>3); getch(); about my other post that was just sad.....i meant well and if you are interested in knowing it was a twofold project meaning i did the other one while my partner did the 1st. anyways, a big sorry to the mods and to the people that flamed...... thanks in advance!

Share this post


Link to post
Share on other sites
Aardvajk    13207
Quote:
Original post by kingofthebraves
couldnt find anything on the net that i could understand so i thought of posting these codes here, can anyone please explain to me what it does and what specific programs i could use it with?

<math.h>
<conio.h>
flag?
while(v<0 || v>3);
getch();

about my other post that was just sad.....i meant well and if you are interested in knowing it was a twofold project meaning i did the other one while my partner did the 1st. anyways, a big sorry to the mods and to the people that flamed......

thanks in advance!



flag? - this won't compile, but could be a part of the use of the ternary operator, like:

int i=(flag ? 23 : 24);

which would set i to 23 if flag evaluated to true, and 24 if flag was false. flag could be replaced by any expression. The restriction is that the items on either side of the : must be the same type.

while(v<0 || v>3);

This would cause an infinite loop assuming v was less than zero or greater than three, or not execute at all if not. A while statement will execute its block until the condition between the brackets is true and would normally look like:

while(expr) execute_this();

or

while(expr)
{
execute_this();
execute_that();
}

Clearly if nothing happens inside the block (or inside the expression) to change it from true to false at some point, the loop will execute forever and your program will hang.

Your example, without any statements to operate on, can be of use, for example:

while(i++<10);

since the value of the expression is changed within the expression itself.

Equally, you might also see:

while(true)
{
do_this();
do_that();

if(something) break;
}

where the break statement causes the loop to terminate.

getch() - this is a non-standard, but very common, function, normally in <conio.h>, that is used in a console program to wait for a keypress then return the ASCII code of the key as the result.

The ternary operator and the while loop are fairly basic constructs in C and C++ so without meaning to sound rude, I'd suggest you get a good textbook on the language and make the time to study it properly.

getch() is not part of the C or C++ standard so you'd find more information in the documentation for the compiler whose library it shipped with.

Here's a very contrived example showing all three in action:


#include <conio.h>
#include <iostream>

int main()
{
int c=getch();
while(c!='x')
{
std::cout << (c=='a' ? "You pressed a" : "You didn't press a") << "\n";

c=getch();
}
}




HTH

Share this post


Link to post
Share on other sites
TheUnbeliever    963
I think EasilyConfused misunderstood you when you said 'flag?'. I presume you're looking for a definition of what a flag is.

A flag is basically just a code of some sort that indicates something. There may be a very specific technical definition, but I don't believe so. For example, the processor has an EFLAGS register where each bit indicates some status - such as the carry flag, indicating a carry occured during an arithmetic operation. Setting a flag is also known as raising a flag.

Another example might be if you were writing to a file in many filesystems and you wanted to add a certain degree of protection to a file by setting the read-only flag.

Share this post


Link to post
Share on other sites
Aardvajk    13207
Quote:
Original post by TheUnbeliever
I think EasilyConfused misunderstood you when you said 'flag?'.


Yep. You're probably right. Have a rate++ for a nice, sensible and down-to-earth answer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this