View Full Screen


   Line Labels:
      Line labels are much like pascal's except that they do not
      need to be defined, however they cannot be numbers.
      EXAMPLE
      char where;
   
      void main()
        {
          start:
             cout << 'This is the beginning of the program\n';
             cout << 'Would you like to go to the start again? '
             cin >> where;
             if ((where == 'Y')||(where == 'y')) 
             goto start;
         }
              

Command: (void) in C++ commands/procedures (user defined commands) have two parts to it. the prototype, and the actual coding. The prototype is a way of defining the command for the program much like you define variables. void do_nothing(); //defining the prototype. void main() { do_nothing(); // command call. } void do_nothing() //actual command coding. { cout << "Inside do_nothing"; }
Functions: functions in C+ are much like pascal where you must define the type of return value of the function. You must also define a prototype for functions as you have to for commands. There are two ways of doing functions as well, if a function is simply returning a mathematical operation you can use an inline function which is simply the prototype and actual coding in one, up at the top. EXAMPLE //normal functions. int add(int, int); //defining function with two integers passed //to it, for use inside function. int a,b,answer; void main() { a = 7; b = 3; answer = add(a,b); } int add(int a, int b) { return a+b; } //inline functions int a,b,answer; inline int add(int a, int b) { return a+b; } void main() { a = 7; b = 3; answer = add(a,b); }
Control Structures: Like Pascal, C++ has similar control structures, but lacks the file scanning structure that TAS has. In C++ they must also be blocked off with {} if you wish to have more than one line executed within the control structures. EXAMPLE while ( a < b ) { cout << a << "\n" a++; // same as a = a + 1; }
do { cout << b << "\n"; b++; } while (b < a);
for(int x;x <= 20; x++) //x++ is the same as x = x + 1 { cout << x << "\n"; } //note: only one line was in this for-loop so it // would be ok to leave off the braces. {}
//in switch statements, on each different case you need // to include the work 'break' at the end of each one // to prevent it from executing all the cases. int num; cout << "Please enter a number under 10. " cin >> num; switch (num) { case 1: case 2: case 3: cout << "under 4";break; case 4: case 5: case 6: cout << "under 6 over 3";break; case 7: case 8: case 9: cout << "under 10 over 6";break; default: //default is the same as an 'else' //or an 'otherwise' in tas. cout << "You did not enter a number inside the desired range."; }
if (a > b) cout << "a is greater than b"; else if (a < b) { cout << "b is greater than a\n"; cout << "Two lines withing a if-then needs braces."; } else cout << "They are equal";
INPUTTING VALUES: In C++, it's much like Pascal, in that you cannot control your input without significant coding and label/udc (user-defined commands) to do the same that TAS can do in one line. You also cannot use the arrow keys to move from each input value, like you can in TAS. EXAMPLE int x; void main() { cout << "Please enter a number. "; cin >> x; }
Outputs: In C++ the output is simple, but hard to position. EXAMPLE cout << "This is a test"; //This leaves cursor at the end //of the output. cout << "This is a test\n"; //returns cursor to next line.
View Full Screen