Challenge Technical Guide - C++ Syntax

This page inlcudes syntax only and is intended for those with some programming experience. You will not learn when to use the code, why it works, or how to incorporate it into your program.

You may include any or all of the following syntax into your program to make your code handle increasingly complex problems.

Note: Notice how similar C++ and Java are in their syntax!

  • Comments
    // This is a single-line comment.
    /* This comment spans across
    more than one line. */
    
  • Conditional Statements
    if (boolean expression)
    {
    	statement 1;
    	statement 2;
    	statement 3;
    }
    else if (boolean expression)
    {
    	statement 1;
    }
    else
    {
    	statement 1;
    	statement 2;
    }
    
  • While Loop
    while (boolean expression)
    {
    	statement 1;
    	statement 2;
    }
    
  • For Loop
    for (initialize ; condition ; update)
    {
    	statements;
    }
    
  • Do Loop
    do
    {
    	statement 1;
    	// notice semicolon at the bottom
    	statement 2;
    } while (condition);
    
  • Arrays
    int my_array[10];
    	// Declaring Array w/10 Integers
    my_array[0]=4;
    my_array[1]=55;
    	// Initializing Array elements
    
    int my_array[10] = {6,5,4,3,2,1,7,8,9,0};
    	// Declaration & Initialization
    
  • Strings
    char my_string[3];
    	// Declaration
    my_string[0]='H';
    my_string[1]='i';
    my_string[2]='\0';
    	// Initialization
    char my_string[]="Hello Everybody!";
    	//Declaration & Initialization
    
  • Functions
    return_type functionName (arg types)
    	// function prototype 
    
    return_type functionName (args)
    {
    	statements;
    }
    
    /* example *************************
    int Cube (int);
    
    int Cube (int n)
    {
    	int cb=0;
    	cb = n*n*n;
    	return cb;
    }
    ********************************* */
    

New Mexico High School Supercomputing Challenge
Coordinated by Los Alamos National Laboratory and New Mexico Technet
Questions? e-mail: consult