More C/C++ Knowledge

2 ways to
comment code

  1. // C++ Single line
    comment.  Does not work in C.

  2. /* comments */ C
    Multi-line comment works in most C++ compilers


6 Relational
– Often,
but not always, used as part of an expression for a conditional or a looping

  1. > Greater than

  2. >= Greater than or
    Equal To

  3. > Less than

  4. >= Less than or Equal

  5. == (double =) Equal To

  6. != Not Equal To


3 Logical

  1. && – AND

  2. || (double pipe) – OR

  3. ! – Not

Conditional Statements and Commands

conditional statements

  1. Switch

  2. If (else-if, else)

Switch – used to check for equality against (usually) a
large number of options.  Good only
for whole numbers (integers) and characters. 
Cannot check ranges of numbers.  Can
have nested switch statements.



switch(x) {

case 1:

// do stuff if x equals 1, or whatever is to the left of the case

break; // failure to include break will let the code int the

following section to also execute


// what ever is not caught above, gets executed here.

// similar to the else clause of an if statement



If – Used to run a section of code only under if certain
criteria is met.  Can have nested
switch statements.




if ( expression ) {

// executes only if x is greater than 10


else if ( expression2 ) {

// the else if (notice space between words) is optional

// when using multiple else if else combos, once a criteria is met, 

// C/C++ will skip to the end of that section


else {

// also optional used as a catch all, similar to the default

// under the switch




2 General
Repetition Types

may never execute loop – checks expression, if true it executes loop body. 
For, and While under C/C++

always executes loop at least 1 time, checks expression to execute again. Do-while
under C/C++


For – general form

expression; increment) {

loop body


  • The initialization
    stage occurs before the loop is executed, and occurs only that one time. 
    You can initialize multiple variables. 
    Even ones not used in the loop.

  • The expression
    is checked each time before looping. 
    If it evaluates to true, then the body of the loop executes. 
    If it evaluates to false then the body is skipped and the loop

  • The increment
    stage occurs after the loop body executes, but before the expression is
    evaluated again.  Occurs every
    time the body is executed.  You
    can modify multiple variables.  Even
    ones not used in the loop.

  • Often used
    for counting, or when something needs to occur a set number of times.  i.e. Print 10 blank lines. 
    Display all numbers from 12 to 12000.

While – general form


// loop body


to write than the for loop, but variable initialization must occur
outside the loop before the expression is evaluated and modification must occur
inside the loop.


Often used
when the programmer does not know how many times the operation will occur.  i.e.: While the user doesn’t enter X, while the user presses
‘y’ to continue.


  • Every
    console C/C++ application has a main function. 
    In C, it usually returned void, in C++ it usually returns an int, 0
    if the program exits normally without error. 
    It may have 0 parameters, or 2, to handle command line arguments.

  • Function
    names follow the same rules of naming variables.

  • One way to
    reuse the same piece of code multiple times.

  • Can receive
    none, one, or multiple variables/values (called parameters) and return a
    single value (value returning function) or no value (void function).

  • Functions
    can call other functions.  The
    function that calls the other function is called the calling function.

  • A function
    can call itself.  This is called
    recursion, and should normally be avoided as it is highly inefficient
    in both memory and time to call, compared to looping.

  • Functions
    can simplify the look of the main function by passing off groups of related
    task to other functions, which reduce the "clutter" within the
    main function.  This makes it
    easier to read/debug.

  • By isolating
    task within functions, it is easier to read the code, debug.

  • Using the return
    command within a function exits the function immediately from that function,
    even if commands follow the return command. 
    This is commonly used if a condition is met early, or an error

  • Developers
    can write their own functions (often called UDFs, User Defined Functions).

  • Many
    functions come built into C/C++.  These
    ready made functions are stored in libraries, and usually have their header
    file included at the top of the source code by using the #include statement.

  • Developers/Third
    Parties can also write groups of functions in libraries that can be later
    distributed to other developers, or the same developer on a different

  • Look at the
    following example…definition for each part of the general form is given:

someFunction(long x, float y, char z) {

// do some stuff

return something;



  • The whole thing is called the function declaration

  • SomeFunction:
    is the function name

  • int: is
    the return type

  • long x, float y, char z:
    are the parameters

  • int someFunction(long x, float y, char z) – function header

  • If int someFunction(long, float, char ); is
    found at the top of the source file, this is the function prototype.




  • While you
    can use global variables, this should be avoided under almost all
    circumstances as it can lead to the same variable being used for different
    task accidentally, especially when multiple developers work on the same

  • Names should
    be concise, but explain what the variable stores.


Copyright ©2001-2002, Walter Wimberly РInstructor and all
’round Cool Guy – IADT