Quote:
Originally Posted by kiiD2NiCe
well i have been using google and tutorials but i have problems with loop int and bool
|
Err... that's a strange range to have problems with. I'm assuming you're learning C#.
An "int" is a variable type that can store 4-byte (or 32-bit) values. To fully understand the storage capacity of variables, I'd advise you read an explanation of binary and hexadecimal values and a little about computer organization. I'll explain a little about them to you, but I'd suggest you read some materials on them.
A 4 byte value can contain a range (in hexadecimal) from 00000000 to FFFFFFFF. There are two main ways (4 if you count sign+magnitude and 1's complement) to represent a number. One is the unsigned representation, which has a minimum value of 0 and a maximum value of 2^n-1 (n being the maximum number of bits a variable can hold). The problem with this representation is that there is no way to represent a negative number. Of course, there's an implementation for this representation in C#, and it is useful if you do not want/expect negative number input (e.g. the variable type "uint"). The other way is the 2's complement representation. This representation allows negative numbers by having the most significant bit (the one with the highest magnitude [e.g. the leftmost]) denote a negative number when it is 1, and a positive when it is 0. For example, if I were to have a 5-bit number, 10010, this would represent -14 base-10 in 2's complement notation, or 18 in unsigned. Again, if you're lost, read some explanations of binary/hexadecimal values, it should be much clearer after doing so.
There are other variable types as well, and should be used if you expect an input to be higher/less than the minimum/maximum capacity. In C#, these are bytes (8-bit and are unsigned by default, you can use sbyte to denote a signed byte (2's complement)), shorts (16-bit), int (which you know are 32-bit), and long (64-bit). By using a smaller capacity variable size, you are saving memory and cutting down resources used by your program.
With that out of the way, let me explain bools. Boolean variables are stored as 8-bit types, but the only valid input to boolean variables are 0 (for deasserted, false) and 1 (for asserted, true). These are used primarily for condition testing, which I assume you know since you haven't stated you're having trouble with them.
Looping is very useful if you want to perform the same action a certain number of times. Programmers use them so that they don't need to write the same code over and over. There are 4 types of loops in C#; for, while, do/while, and foreach. A for loop initializes a variable, tests a condition, and performs an operation after each loop. A while loop simply tests a condition and loops if the condition is true, a do/while loop is almost exactly like a while loop, except it executes the code in the loop before testing the condition (useful for menus), and a foreach loop in used primarily in traversing containers (I think, I'm newish to C#, C++ is my language). Here's a quick example of the loops, with the exception of the do/while (self-explanatory), and the foreach loop, since I don't think you've gotten there yet.
int i = 0;
while (i < 10)
{
// Do something here
i++;
}
for (int i = 0; i < 10; i++)
{
// Do something here
}
Both of these loops do the exact same thing, and will execute the code in the curly braces 10 times.
If I made a mistake, and I probably did because I rushed it, tell me. Hopefully that's a bit clearer than before.