C and C++Post your C and C++ Related Tutorial Here
Notices
You are currently viewing AIOFORUM as a guest which gives you VERY limited access to view most discussions, Applications, Latest Movies & Tutorials and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload and Download content and access many other special features. Registration is fast, simple and absolutely free so please, Join our community today!
The break keyword is used to exit loops at arbitrary spots. As soon as the computer reaches the break statement it exits the loop. A break command is used simply by typing:
break;
It might be implemented in a loop as follows.
while( n )
{
printf("N equals %d", n);
if( n == 5 )
break;
n--;
}
If n where to become equal to five, the program would jump out of this loop right after it checked the if statement. If you are using break inside of a nested loop, it will only jump out of the loop you are currently in. Consider at the following example.
while( n )
{
for( i=0; i<10; i++)
{
if( n == 5 )
break;
printf("%d", i);
}
n--;
}
In the code above, every time the while loop is taken, we go through a for loop. The for loop is set to run 10 times, but if n equals five, then the break will be executed, and the for loop will be exited immediately. However, the while loop will continue, and the next iteration, the for loop will work like it should. TECHNICAL NOTES
The char keyword sets aside storage space for a single character variable. The declaration format is similar to the others, and look like this,
char A;
This only creates enough room for a single character (not necessarily a letter of the alphabet) and that is all. To set aside room for words, or even sentences (called a string), you must enclose a number in brackets, after the variable name. The number should be the number of characters you want to have room for. It would look like this,
char A[13];
Now the variable A, can hold thirteen characters. To set a char equal to something, you type the name of the variable, an equal sign (spaces buffering the equal sign are optional), then a value. The value must be in single quotes (' '). The single character value we looked at first could look like this,
char A;
A = 'j';
However with a string, you use the double quotes (" ") like this,
char A[13] = "Howdy partner";
If you want to assign something to a string, like I did above, you must do it when you declare the string. The following is not allowed.
char A[13];
A = "Howdy partner";
That will just get you a big fat error! NOTE:Even spaces count as a character! With each of the above examples, you declare and set a value all in one line. You just combine the lines like this,
char A = 'j';
char B[13] = "Howdy partner";
When you use this method with strings, you can leave the brackets empty, and the compiler will automatically set aside, just enough room. If you do fill in a number, but then do not use all the space you set aside, that is ok, but you will be wasting space. TECHNICAL NOTES
*If you assign a number to a character value, like this,
char A = '5';
A will not take on the numeric value 5. It now contains the character 5, not the mathematical value 5.
The continue keyword is used in loops to skip right to the test condition. Any code in the body that is after a continue statement will be skipped. It is important to note that a continue statement does not force the loop to perform another iteration. If the test condition is false, the loop will stop. The continue statement is implemented as follows.
continue;
Like the break command, when continue is used in a nested loop, it only jumps to the testing condition of the current loop. Here is an example of how continue might be used in a loop.
do
{
printf("Enter a number: ");
scanf("%d", &n);
if( n == 0 )
continue;
printf("You entered %d", n)
x--;
}while( x );
The loop in this sample continues to collect numbers from the user, and print them on the screen as long as x does not equal zero. If the user enters a zero, the printf statement is skipped, as well as the x decrement statement. The program will jump down to the while statement and test for continuation. TECHNICAL NOTES
The delete keyword is used to clean out a chunk of memory. The syntax looks like this:
delete object;
In this example an object can be any kind of data storage. It can be a regular variable, a structure or an array. It can also be a pointer. When delete is done the previously allocated memory will be freed up for later use, and its contents will be lost. Technically delete is a C++ thing, so some older C compilers may not support it.
The do keyword is used to start off a do..while loop. Do while loops are post-test loops. That means the code in the body is executed, then a condition is checked to see if the loop should continue. This guarantees that the body is executed at least once. The format for a do while loop looks like this.
do
{
statements;
}
while( condition );
The keyword do gets the loop started. The body is executed once, then the while statement checks the condition to see if the loop should continue. In do while loops, the while part MUST end with a semicolon. If we want to use more than one statement in the body, they must be enclosed in curly brackets (a compound statement). Typically, curly brackets are used with do while loops even if there is only one statement. This is done for readability.
The enum keyword is used to define C enumeration types. Enumerations allow you to create a list of of symbolic names for integers called enumeration constants. These enumeration constants can be used throughout the program in place of integers. Enumerations are usually declared as follows:
// Declare the enumerated type
enum months { january = 1, february, march, april, may, june
july, august, september, october, november, december };
// Create an instance of that type
enum months birthMonth;
In the example above, the months are listed starting with one. The values for the other months will increment by one, so February will be 2, March will be 3 and so on. You can also explicitly set the value for each enumeration constant manually by assigning them all values instead of just January. If you do not specify any values for any of the constants, the default will be zero for the first element, then one, two, etc. . . Once an enumeration constant has been declared in one enumerated type, it cannot be used again in another. The enumeration constants, and any variables of the enumerated type can be used any where in the program an integer can be used.
The keyword float sets aside memory for a floating point number. Floats are numbers with decimal points, like 3.456. As far as decimals go floats are accurate up to about 8 places, after that, the computer just makes stuff up. The format is just like with integers except you type float instead of int. It should look like this.
float a;
Just like all common C lines it requires a semicolon at the end. To assign a value to a float type its name then an equal sign, then the value, like this,
float a;
a = 56.37654;
Like an integer you can declare and assign a value to a float all in one line.
float a = 56.37654;
TECHNICAL NOTES
* Floats need more processing than ints. Don't use a float, when you can get away with an integer!
* Floats cousin, keyword double can be used for astronomical sized numbers and more precise decimal points (it's good for at least 12 places). It is declared just like a float, except with the word double instead of float.
For loops are used to execute segments of code a specific number of times. A for loop starts with for keyword, then has three expressions in parenthesis. A for loop follows this general form:
for( initialize; while true; do this)
statement; NOTE:The first two expressions must be ended by a semicolon. The first expression takes care of any required initialization concerning the loop. The second tells the computer when to stop (when that expression is false). The third takes care of any updating that should be done. The statement section of a for loop is called the body.
A for loop can have any number of statements in it's body, but multiple statements must be enclosed in curly brackets. Here is an example of a proper for loop. The declaration of i is assumed.
for( i=0; i<10; i++)
{
statement1;
statement2;
statement3;
}
And that's how a very simple for loop is made. Note that you can have just about any kind of expression you can imagine in the for loop parts. You are not restricted to simple expressions like the ones above. TECHNICAL NOTES
*The middle expression of a for loop is formally called the terminating expression.
*Groups of statements enclosed by curly brackets are called compound statements. Essentially, the compiler sees these compound statements all as one.
*This is necessary because technically for loops can only have one statement in their body.
The free keyword is used to release a chunk of memory. The syntax looks like this:
free object;
In this example an object can be any kind of data storage. It can be a regular variable, a structure or an array. It can also be a pointer. When free is done the previously allocated memory will be freed up for later use, and its contents will be lost.