Sei sulla pagina 1di 5

C-Free is a professional C/C++ Integrated Development Environment (IDE) that supports multi-compilers like Borland C++ Compiler, Microsoft

C++ Compiler and Intel C++ Compiler besides MinGW. Using this software, user can edit, build, run and debug programs freely. With C/C++ source parser included, although C-Free is a lightweight C/C++ development tool, it has powerful features to let you make use of it in your project. #include <iostream> using namespace std; int main() { cout<<"HEY, you, I'm alive! Oh, and Hello World!\n"; cin.get(); }

#include <iostream> using namespace std; int main() { cout<<"HEY, you, I'm alive! Oh, and Hello World!\n"; cin.get(); return 1; } Declaring Variables in C++ To declare a variable you use the syntax "type <name>;". Here are some variable declaration examples: int x; char letter; float the_float; It is permissible to declare multiple variables of the same type on the same line; each one should be separated by a comma. int a, b, c, d;

#include <iostream> using namespace std; int main() { int thisisanumber; cout<<"Please enter a number: "; cin>> thisisanumber; cin.ignore(); cout<<"You entered: "<< thisisanumber <<"\n"; cin.get(); }
1

a = 4 * 6; // (Note use of comments and of semicolon) a is 24 a = a + 5; // a equals the original value of a with five added to it a == 5 // Does NOT assign five to a. Rather, it checks to see if a equals 5.

a < 5 // Checks to see if a is less than five a > 5 // Checks to see if a is greater than five a == 5 // Checks to see if a equals five, for good measure

Here are the relational operators, as they are known, along with examples: > greater than 5 > 4 is TRUE < less than 4 < 5 is TRUE >= greater than or equal 4 >= 4 is TRUE <= less than or equal 3 <= 4 is TRUE == equal to 5 == 5 is TRUE != not equal to 5 != 4 is TRUE Basic If Statement Syntax The structure of an if statement is as follows: if ( TRUE ) Execute the next statement Here is a simple example that shows the syntax: if ( 5 < 10 ) cout<<"Five is now less than ten, that's a big surprise"; Let's look at a simple program for you to try out on your own. #include <iostream> using namespace std; int main() // Most important part of the program! { int age; // Need a variable... cout<<"Please input your age: "; // Asks for age cin>> age; // The input is put in age cin.ignore(); // Throw away enter if ( age < 100 ) { // If the age is less than 100 cout<<"You are pretty young!\n"; // Just to show you it works... } else if ( age == 100 ) { // I use else just to show an example cout<<"You are old\n"; // Just to show you it works... } else { cout<<"You are really old\n"; // Executed if no other statement is } cin.get(); }

FOR - for loops are the most useful type. The syntax for a for loop is

for ( variable initialization; condition; variable update ) { Code to execute while the condition is true }

Example:

#include <iostream> using namespace std; // So the program can see cout and endl int main() { // The loop goes while x < 10, and x increases by one every loop for ( int x = 0; x < 10; x++ ) { // Keep in mind that the loop condition checks // the conditional statement before it loops again. // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; } cin.get(); }

Example: #include <iostream> using namespace std; // So we can see cout and endl int main() { int x = 0; // Don't forget to declare variables while ( x < 10 ) { // While x is less than 10 cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get(); }

#include <iostream> using namespace std; int main() { int x; x = 0; do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!\n"; } while ( x != 0 ); cin.get(); }

switch ( <variable> ) { case this-value: Code to execute if <variable> == this-value break; case that-value: Code to execute if <variable> == that-value break; ... default: Code to execute if <variable> does not equal the value following any of the cases break; }

int a = 10; int b = 10; int c = 20; switch ( a ) { case b: // Code case c: // Code default: // Code

break; break; break; }

Below is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program. #include <iostream> using namespace std; void playgame() { cout << "Play game called"; } void loadgame() { cout << "Load game called"; } void playmultiplayer() { cout << "Play multiplayer game called"; } int main() { int input; cout<<"1. Play game\n"; cout<<"2. Load game\n"; cout<<"3. Play multiplayer\n"; cout<<"4. Exit\n"; cout<<"Selection: "; cin>> input; switch ( input ) { case 1: // Note the colon, not a semicolon playgame(); break; case 2: // Note the colon, not a semicolon loadgame(); break; case 3: // Note the colon, not a semicolon playmultiplayer(); break; case 4: // Note the colon, not a semicolon cout<<"Thank you for playing!\n"; break; default: // Note the colon, not a semicolon cout<<"Error, bad input, quitting\n"; break; } cin.get(); }

Potrebbero piacerti anche