README
if ( test-expression )
{
// statements-to-be-executed-when-true
}
if ( test-expression )
{
// statements-to-be-executed-when-true
}
else
{
// statements-to-be-executed-when-false
}
switch ( variableName )
{
case value1:
// do something if value1
break;
case value2:
// do something if value2
break;
default:
// if none of the above values
}
for (initialiser; text-expression; incrementer)
{
// do something ...
}
while (test-expression)
{
// do something iteratively ...
}
do {
// do something iteratively ...
} while (test-expression)
- In the do-while loop, there will always be at least 1 iteration because the expression is not evaluated until after the first evaluation
- A break statement can be used in the code to break out of the loop
- A continue statement can be used to skip the current iteration and move to the next
- Benefits of using functions:
- Functions make program code easier to understand and maintain
- Tried and Tested functions can be re-used by other programs
- Several programmers can divide workload in large projects by working on different functions of the program.
float functionName(float, float);
float functionName(float float1, float float2)
{
return float1 + float2;
}
- Variables that are declared in a function can only be used locally within that function - this is known as variable scope
- cin function can be used with the >> input stream operator to pass a value from the console to a variable in a program
float num;
cout << "Please enter a number: ";
cin >> num;
- When declaring a function, you can optionally provide a default value to use for input arguments, e.g. ...
float fToC (float degreesF = 32.0);
- Overloading means that we can have multiple functions with the same name, as long as they have different arguments signatures. For example, we could have multiple functions called computeArea for circles, with diameter as the parameter, and rectangles, with height and width as the parameters
// function declaration
float computeArea (float diameter);
float computeArea (float height, float width);