Saturday, 12 October 2013

C++ course(tenth section)

C++(etenth section)
tenth section(Arrays)

One dimensional array

Array: is a collection of a fixed number of components wherein all of the components have the same data type
One-dimensional array - an array in which the components are arranged in a list form 
The general form of declaring a one-dimensional array is: 
dataType arrayName[intExp]; 
where intExp is any expression that evaluates to a positive integer.


Declaring an array
The statement
int num[5];

declares an array num of 5 components of the type int
The components are num[0], num[1], num[2], num[3], and num[4].
Accessing Array Components
The general form (syntax) of accessing an array component is:
arrayName[indexExp]
where indexExp, called index, is any expression whose value is a nonnegative integer
Index value specifies the position of the component in the array
The [] operator is called the array subscripting operator
The array index always starts at 0.

i.e: int arr[3];
arr[0]=58;
arr[1]=90;
arr[2]=60;

Processing One-Dimensional Arrays
Some basic operations performed on a one-dimensional array are:
−Initialize
−Input data
−Output data stored in an array
−Find the largest and/or smallest element
Each operation requires ability to step through the elements of the array
Easily accomplished by a loop.

Accessing Array Component
Consider the declaration
int list[100]; //list is an array of the size 100
int i;
This for loop steps-through each element of the array list starting at the first element
for (i = 0; i < 100; i++) //Line 1 
//process list[i] //Line 2
If processing list requires inputting data into list 
−the statement in Line 2 takes the from of an input statement, such as the cin statement 
for (i = 0; i < 100; i++) //Line 1 
cin >> list[i]; 
Array Index Out of Bounds

If we have the statements:
double num[10];
int i;
The component num[i] is a valid index if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9
The index of an array is in bounds if the index >=0 and the index <= ARRAY_SIZE-1
If either the index < 0 or the index > ARRAY_SIZE-1 
−then we say that the index is out of bounds 
There is no guard against indices that are out of bounds 
−C++ does not check if the index value is within range

Array Initialization
As with simple variables 
−Arrays can be initialized while they are being declared 
When initializing arrays while declaring them 
−Not necessary to specify the size of the array 
Size of array is determined by the number of initial values in the braces 
For example: 
double sales[] = {12.25, 32.50, 16.90, 23, 45.68};


Partial Initialization
The statement  int list[10] = {0}; 
declares list to be an array of 10 components and initializes all components to zero 
The statement  int list[10] = {8, 5, 12}; 
declares list to be an array of 10 components, initializes list[0] to 8, list[1] to 5, list[2] to 12 and all other components are initialized to 0

The statement
int list[] = {5, 6, 3};
declares list to be an array of 3 components and
initializes list[0] to 5, list[1] to 6, and list[2] to 3
The statement
int list[25]= {4, 7};
declares list to be an array of 25 components
−The first two components are initialized to 4 and 7 respectively
−All other components are initialized to 0
Arrays as Parameters to Functions
Arrays are passed by reference only
The symbol & is not used when declaring an array as a formal parameter
The size of the array is usually omitted.
If the size of one-dimensional array is specified when it is declared as a formal parameter 
−It is ignored by the compiler 
The reserved word const in the declaration of the formal parameter can prevent the function from changing the actual parameter.

i.e: void func1(int arr[6],int a)
{
.
.
}

C++ course(ninth section)

C++(tninth section)
ninth section(user-defined data types,and the "string" type)

Enumeration Type

Data type - a set of values together with a set of operations on those values
To define a new simple data type, called enumeration type, we need three things:
−A name for the data type
−A set of values for the data type
−A set of operations on the values.
A new simple data type can be defined by specifying its name and the values, but not the operations 
The values must be identifiers.

The syntax for enumeration type is:
value1, value2, … are identifiers called enumerators
value1 < value2 < value3 <...
enum typename {value1,value2,....};
Enumeration type is an ordered set of values 
If a value has been used in one enumeration type 
−It cannot be used by another in the same block 
The same rules apply to enumeration types declared outside of any blocks.

Example:
enum sports {football,basketball,baseball};
sports s;
Assignment
The statement:
popularSport = FOOTBALL;
stores the word FOOTBALL into popularSport
The statement:
mySport = popularSport;
copies the contents of the variable popularSport into mySport.
Operations
No arithmetic operation is allowed on enumeration types
The following statements are illegal:
mySport=s+2;
s=football+basketball;
The increment and decrement operations are not allowed on enumeration types
The following statements are illegal:
s++;
s--;
The string Type
To use the data type string, the program must include the header file <string>
The statement
string name = "William Jacob";
declares name to be a string variable and also initializes name to "William Jacob"
The first character in name, 'W', is in position 0, the second character, 'i', is in position 1, and so on.
The variable name is capable of storing any size string
Binary operator + (to allow the string concatenation operation), and the array subscript operator [], have een defined for the data type string
For example, if str1 = "Sunny", the statement stores the string "Sunny Day" into str2:
str2 = str1 + " Day";
length Function
Length returns the number of characters currently in the string 
The syntax to call the length function is: 
strVar.length()
where strVar is variable of the type string
length has no arguments 
length returns an unsigned integer 
The value returned can be stored in an integer variable.

cout<<variable.length();
size Function
The function size is the same as the function length
Both functions return the same value
The syntax to call the function size is:
strVar.size()
where strVar is variable of the type string
As in the case of the function length, the function size has no arguments.
find Function
find searches a string for the first occurrence of a particular substring
Returns an unsigned integer value of type string::size_type giving the result of the search
The syntax to call the function find is:
strVar.find(strExp)
where strVar is a string variable and strExp is a string expression evaluating to a string
The string expression strExp can also be a character.
If successful, find returns the position in strVar where the match begins 
For the search to be successful, the match must be exact 
If unsuccessful, find returns the special value string::npos (“not a position within the string”).

substr Function
substr returns a particular substring of a string 
The syntax to call the function substr is: 
strVar.substr(expr1,expr2)
where expr1 and expr2 are expressions evaluating to unsigned integers.

The expression expr1 specifies a position within the string (starting position of the substring)
The expression expr2 specifies the length of the substring to be returned.
swap Function
swap interchanges the contents of two string variables
The syntax to use the function swap is
strVar1.swap(strVar2);
where strVar1 and strVar2 are string variables
Suppose you have the following statements:
string str1 = "Warm";
string str2 = "Cold";
After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "Warm.

C++ course(eighth section)

C++ course(eighth section)

eighth section(Function with parameters)


Function definition syntax: 
void functionName(dataType& variable, dataType& variable, …)
{
statements
}
Value Parameter - a formal parameter that receives a copy of the content of the corresponding actual parameter 
Reference Parameter - a formal parameter that receives the location (memory address) of the corresponding actual parameter.


Formal Parameters
If a formal parameter is a value parameter, the value of the corresponding actual parameter is copied into it
The value parameter has its own copy of the data
During program execution, the value parameter manipulates the data stored in its own memory space.

Reference Parameter
If a formal parameter is a reference parameter, it receives the address of the corresponding actual parameter
A reference parameter stores the address of the corresponding actual parameter
During program execution to manipulate the data, the address stored in the reference parameter directs it to the memory space of the corresponding actual parameter.
A reference parameter receives the address of the actual parameter 
They can pass one or more values from a function and can change the value of the actual parameter 
Reference parameters are useful in three situations: 
Returning more than one value 
Changing the actual parameter 
When passing the address would save memory space and time.


Programming example:
#include <iostream>
using namespace std;
void funOne(int a, int& b, char v);
void funTwo(int& x, int y, char& w);
int main()
{
int num1, num2; 

char ch;
num1 = 10; //Line 1
num2 = 15; //Line 2
ch = 'A'; //Line 3
cout<<"Line 4: Inside main: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 4
funOne(num1, num2, ch); //Line 5
cout<<"Line 6: After funOne: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 6
funTwo(num2, 25, ch); //Line 7
cout<<"Line 8: After funTwo: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 8
return 0;
 }
void funOne(int a, int& b, char v)
{ int one;
one = a; //Line 9
a++; //Line 10
b = b * 2; //Line 11
v = 'B'; //Line 12
cout<<"Line 13: Inside funOne: a = "<<a<<", b = "<<b
<<", v = "<<v<<", and one = "<<one<<endl; //Line 13
}
void funTwo(int& x, int y, char& w)
{ x++; //Line 14
y = y * 2; //Line 15
w = 'G'; //Line 16
cout<<"Line 17: Inside funTwo: x = "<<x
<<", y = "<<y <<", and w = "<<w<<endl; //Line 17
}

Output is:
Line 4: Inside main: num1=10, num2=15, and ch=A
Line 13: Inside funOne: a=11, b=30, v=B, and one=10
Line 6: After funOne: num1=10, num2=30, and ch=A
Line 17: Inside funTwo: x=31, y=50, and w = G
Line 8: After funTwo: num1=10, num2=31, and ch= G

Parameters & Memory Allocation
When a function is called, memory for its formal parameters and variables declared in the body of the function (called local variables) is allocated in the function data area
In the case of a value parameter, the value of the actual parameter is copied into the memory cell of its corresponding formal parameter
In the case of a reference parameter, the address of the actual parameter passes to the formal parameter.
Content of the formal parameter is an address 
During execution, changes made by the formal parameter permanently change the value of the actual parameter 
Stream variables (for example, ifstream and ofstream) should be passed by reference to a function.


Scope of an Identifier
The scope of an identifier refers to where in the program an identifier is accessible (that is visible)
Local identifier - identifiers declared within a function (or block)
Global identifier – identifiers declared outside of every function definition
C++ does not allow the nesting of functions, this means that the definition of one function cannot be included in the body of another function.
Global identifiers (such as variables) are accessible by a function or a block if, 
the identifier is declared before the function definition (block) 
the function name is different from the identifier 
all parameters of the function have names different than the name of the identifier
all local identifiers (such as local variables) have names different than the name of the identifier.

An identifier declared within a block (Nested Block) is accessible
only within the block from the point it is declared until the end of the block and
by those blocks that are nested within that block if the nested block does not have an identifier with the same name as that of the outside block (the block that encloses the nested block)
The scope of a function name is similar to the scope of an identifier declared outside of any block.

Global VariablesSome compilers initialize global variables to default values 
The operator :: is called the scope resolution operator 
By using the scope resolution operator, a global variable declared before the definition of a function (block) can be accessed by the function (or block) even if the function (or block) has an identifier with the same name as the variable.

C++ provides a way to access a global variable declared after the definition of a function
In this case, the function must not contain any identifier with the same name as the global variable.

Global Variable Side Effects
Using global variables has side effects
Any function that uses global variables is not independent and usually it cannot be used in more than one program
If more than one function uses the same global variable and something goes wrong, it is difficult to find what went wrong and where
Problems caused by global variables in one area of a program might be misunderstood as problems caused in another area.

Static and Automatic Variables
Automatic variable - a variable for which memory is allocated at block entry and deallocated at block exit
Static variable - a variable for which memory remains allocated as long as the program executes
Variables declared outside of any block are static variables
By default variables declared within a block are automatic variables
We can declare a static variable within a block by using the reserved word static.
The syntax for declaring a static variable is: 
static dataType identifier;
The statement 
static int x;
declares x to be a static variable of the type int 
Static variables declared within a block are local to the block 
Their scope is the same as any other local identifier of that block.


Function Overloading
You can have several functions with the same name
Every function must have a different set of parameters
This is overloading a function name
The types of parameters determine which function to be executed
It is used when we have the same action for different sets of data
For it to work, the definition of each of the functions must be given.

Default Parameters
When a function is called, the number of actual and formal parameters must be the same
C++ relaxes this condition for functions with default parameters
You specify the value of a default parameter when the function name appears for the first time, such as in the prototype.
If you do not specify the value of a default parameter, the default value is used 
All the default parameters must be the rightmost parameters of the function 
In a function call where the function has more than one default parameter and a value to a default parameter is not specified, then you must omit all of the arguments to its right.

Default values can be constants, global variables, or function calls
The caller has the option of specifying a value other than the default for any default parameter
You cannot assign a constant value as a default value to a reference parameter.

Programming Example

Example about using functions:

// A value returned by a return statement in a function
#include <iostream>
using namespace std;
int funcRet1(void);

int funcRet2();
int funcRet3();
int funcRet4(int z); 
int main()
{
 int num = 4;
cout<<"Line 1: Value returned by funcRet1: "<<funcRet1()<<endl; // Line 1
cout<<"Line 2: Value returned by funcRet2: "<<funcRet2()<<endl; // Line 2
cout<<"Line 3: Value returned by funcRet3: "<<funcRet3()<<endl; // Line 3
cout<<"Line 4: Value returned by funcRet4: "<<funcRet4(num)<<endl; // Line 4
return 0;
}
int funcRet1()
 return 23, 45;                    //Only 45 is returned 
 }
int funcRet2()
{
 int x = 5; int y = 6;
return x, y;                            //Only the value of y is returned
 }
int funcRet3()
int x = 5; int y = 6;
return 37, y, 2 * x;                 //Only the value of 2 * x is returned
 }
int funcRet4(int z)
{
 int a = 2; int b = 3;
return 2 * a + b, z + b;           //Only the value of z + b is returned
 }

C++ course(seventh section)

C++ course(seventh section) 

seventh section(Functions)

What is a function ?
Functions are like building blocks
They allow complicated programs to be divided into manageable pieces.
Some of the advantages of functions are: 
A programmer can focus on just that part of the program and construct it, debug it, and perfect it 
Different people can work on different functions simultaneously 
If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times.

Functions are often referred to as modules.
are like miniature programs.
can be put together to form a larger program.

Standard Functions
In college algebra a function is defined as a rule or correspondence between values, called the function’s arguments, and the unique value of the function associated with the arguments
If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and f(3) = 11
1, 2, and 3 are arguments
7, 9, and 11 are the corresponding values.

Predefined Functions
Void functions - functions that do not have a data type.
Value-returning functions - functions that have a data type.
To use these functions you need to:
include the correct header file
know the name of the function
know the number of parameters, if any
know the data type of each parameter
know the data type of the value computed by the function, called the type of the function

Standard Functions
Some of the predefined mathematical functions are:
sqrt(x)
pow(x,y)
floor(x)
Predefined functions are organized into separate libraries
I/O functions are contained in the header file iostream
Math functions are contained in the header file cmath

1)The Power Function (pow)
pow(x,y) calculates xy, for example: pow(2,3) = 8.0 
Is of the type double or the function pow returns a value of the type double 
x and y are called the parameters (or arguments) of the function pow 
Function pow has two parameters


2)The sqrt and floor Functions
The square root function sqrt(x) 
calculates the non-negative square root of x for x >= 0.0 
sqrt(2.25) is 1.5 
It is of the type double and has only one parameter 
The floor function floor(x) 
calculates the largest whole number not greater than x 
floor(48.79) is 48.0 
It is of the type double and has only one parameter


User-Defined Functions

Void functions - functions that do not have a data type 
Value-returning functions - functions that have a data type 
To build these functions you need to: 
know the name of the function 
know the number of parameters, if any 
know the data type of each parameter 
know the data type of the value computed by the function, called the type of the function.

Since the value returned by a value-returning function is unique, we:
save the value for further calculation
use the value in some calculation
print the value
A value-returning function is either used in an assignment statement or in an output statement such as cout
There is one more thing that is associated with a function:
the code that is required to accomplish the task.
Formal Parameter - A variable declared in the function heading 
Actual Parameter - A variable or expression listed in a call to a function.


Program example:
#include <iostream>
using namespace std;
int sum (int x, int y);      //function prototype
int square (int x);       //function prototype
int main()
{

 int one, two; //Line 1
cout<<"Line 2: sum of 5 and 10 is "<<sum(5,10)<<endl; //Line 2
cout<<"Line 3: Enter two numbers: "; //Line 3
cin>>one>>two; //Line 4
cout<<endl; //Line 5
cout<<"Line 6: sum of "<<one<<" and "<<two<<" is "<<sum(one,two)<<endl; //Line 6
cout<<"Line 7: square of 2, 3, and 4 is "<<square(2)<<"\t"<<
<<square(3)<<"\t"<<square(4)<<endl; //Line 7
return 0;
}
int sum (int x, int y)
{ return x+y; } // end of sum
int square (int x)
{ return x*x; } // end of sqaure

Function Prototype
Function Prototype - function heading without the body of the function
The syntax is:
functionType functionName(parameter list);
It is not necessary to specify the variable name in the parameter list
The data type of each parameter must be specified

Value-returning Functions
The syntax is:
functionType functionName(formal parameter list)
{
statements
}
functionType - type of the value returned by the function
functionType - also called the data type of the value-returning function

Syntax

The syntax of the formal parameter list is: 
dataType identifier, dataType identifier, ...
The syntax for a function call is: 
functionName(actual parameter list)
The syntax for the actual parameter list is: 
expression or variable,expression or variable, ...


Functions
The formal parameter list can be empty
If the formal parameter list is empty
the parentheses are still needed
the function heading of the value-returning function takes either of the following forms:
functionType functionName()
functionType functionName(void)
in a function call the actual parameter is empty
A call to a value-returning function with an empty formal parameter list is: functionName()

Value-Returning Functions
To call a value-returning function:
Use its name, with the actual parameters (if any) in parentheses
There is a one-to-one correspondence between actual and formal parameters
A value-returning function is called in an expression
The expression may be part of an assignment statement or an output statement
A function call in a program results in the execution of the body of the called function.

The return Statement
Once the function computes the value, the function returns this value via the return statement
The syntax of the return statement is:
return expression or variable;
When a return statement executes in a function, the function immediately terminates and the control goes back to the caller
When a return statement executes in the function main, the program terminates.


Flow of Execution
A function call statement results in the transfer of control to the first statement in the body of the called function 
After the last statement of the called function is executed, the control is passed back to the point immediately following the function call 
A value-returning function returns a value 
After executing the function when the control goes back to the caller, the value that the function returns replaces the function call statement.

C++ course (sixth section)

C++ course(sixth section)

Break & Continue Statements


A break and continue statement alters the flow of control 
The break statement, when executed in a switch structure, provides an immediate exit from the switch structure 
The break statement can be used in while, for, and do...while loops 
When the break statement executes in a repetition structure, it immediately exits from these structures.


The break statement is typically used for two purposes: 
1. To exit early from a loop 
2. To skip the remainder of the switch structure 
After the break statement executes, the program continues to execute with the first statement after the structure 
The use of a break statement in a loop can eliminate the use of certain (flag) variables.


The continue statement is used in while, for, and do-while structures 
When it is executed in a loop, it skips the remaining statements and proceeds with the next iteration of the loop 
In a while and do-while structure, the expression (loop-continue test) is evaluated immediately after the continue statement 
In a for structure, the update statement is executed after the continue statement, and then the loop condition executes.

Examples:
1)
for (int x=1;x<=5; x++) 
if (x!=2)
cout<<x<<“\t”;
else
break;

 cout<<x<<endl;
Output:
  1 2

2)
for (int x=1;x<=5; x++) 
if (x!=2)
cout<<x<<“\t”;
else
continue;
cout<<x<<endl;
Output:

1 3 4 5 6


C++ course(fifth section 2)

C++ course(fifth section 2)
Fifth section(continued  Loops)


The for loop executes as follows:
The initial statement executes
The loop condition is evaluated
if the loop condition evaluates to true
execute the for loop statement
execute the update statement (the third expression in the parentheses)
Repeat the previous step until the loop condition evaluates to false
The initial statement initializes a variable .
The initial statement in the for loop is the first to be executed and is executed only once.
If the loop condition is initially false, the loop body does not execute
The update expression, when executed, changes the value of the loop control variable which eventually sets the value of the loop condition to false
The for loop executes indefinitely if the loop condition is always true.
Example:
for (int x=1; x <= 5; x++)
 cout<<x<<“\t”;

The output is:
1     2     3    4     5

Fractional values:
can be used for loop control variables of the double type or any real data type
should be avoided because different computers can give different results for the variables
A semicolon at the end of the for statement (just before the body of the loop) is a semantic error, in this case, the action of the for loop is empty
If the loop condition is omitted it is assumed to be true.
In a for statement, all three statements—initial statement, loop condition, and update statement can be omitted
The following is a legal for loop:
for(;;)
cout<<"Hello"<<endl;

The do…while Loop
The general form of a do...while statement is:
do
statement
while(expression);
The statement executes first, and then the expression is evaluated
If the expression evaluates to true, the statement executes again
As long as the expression in a do...while statement is true, the statement executes
To avoid an infinite loop, the loop body must contain a statement that ultimately makes the expression false and assures that it exits
The statement can be a simple or a compound statement
If it is a compound statement, it must be enclosed between braces
Because the while and for loop has an entry condition, it may never activate, whereas, the do...while loop, which has an exit condition, always goes through at least once
Example:
int x = 1;
do
cout<<x++<<“\t”;
while (x<=5);

output is:
1     2    3   4    5

*Important note: the difference between (while loop statement) and (do while statement) that the do while must be executed at least one time , but the while statement can be used without being executed.

C++ course (fifth section 1)

C++ course(fifth section 1)

Fifth section(counter control structures 1)

Why do we use repetition ?

Repetition allows you to efficiently use variables
Can input, add, and average multiple numbers using a limited number of variables
For example, you can add five numbers together by:
declaring a variable for each number, inputting the numbers and adding the variables together
creating a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers and looping until all numbers are read.

1)The (while) loop:
The general form of the while statement is: 
while(expression)
statement1;
statement2;
while is a reserved word 
Statement1 can be simple or compound 
The expression acts as a decision maker and is usually a logical expression 
Statement1 is called the body of the loop 
The parentheses are part of the syntax 
Statement2 is executed in any case.

The expression provides an entry condition
The statement executes if the expression initially evaluates to true
The loop condition is then reevaluated
If it is still true, the statement executes again
The statement continues to execute until the expression is no longer true
An infinite loop continues to execute endlessly and can be avoided by making sure that the loop’s body contains statement(s) that assure that the exit condition will eventually be false
Examples:
int x=1; 
while (x <= 5)
{ cout<<x<<“\t”; x++;}

The output is:   1      2    3    4     5
  • Programming example:
#include <iostream>
using namespace std;
int main()
{
 int number; // variable to store the number
int sum = 0; // variable to store the sum
int count = 1; // variable to store the total number read
while(count <= 5)
 cout<<"enter integer: ";
cin>>number; sum = sum + number;
 count++;
}
count--;
cout<<"Line 7: The sum of "<<count<<" numbers is "<<sum<<endl;
 double average= static_cast<double> (sum) / count;
cout<<"average = "<<average<<endl;
return 0;
}
*Note: this statement  (static_cast<double> ) converts integer values to the identifier betwen < >.
i.e we have float variable and we want to convert it to int we type 
static_cast <int> (variable name)

The output of the past example is:
enter integer: 4
enter integer: 3
enter integer: 5
enter integer: 7
enter integer: 8
Line 7: The sum of 5 numbers is 27
average = 5.4

Sentinel-Controlled while Loops:
A sentinel variable is tested in the condition and the loop ends when the sentinel value is encountered
The syntax is:
cin>>variable;
while(variable != sentinel)
{
.
cin>> variable;
.
}
Flag-Controlled while Loops
A flag-controlled while loop uses a Boolean variable to control the loop.
It is suitable for searching Arrays.
The flag-controlled while loop takes the form:
found = false;
while(!found)
{
.
if(expression)
found = true;
.
}

C++ course (fourth section)

C++ course (fourth section)

fourth section(control structures)

One-Way (if) Selection

The syntax of one-way selection is:
if (expression)
statement1;
statement2;
If the value of the expression is true the statement1 is executed
If the value is false the statement1 is not executed and the computer goes on to the next statement in the program, which is statement2.
The expression is usually a logical expression
statement is any C++ statement
if is a reserved word

Two-Way (if…else) Selection



Two-way selection takes the form: 

if(expression) 
statement1;
else
statement2;
statement3;
If the value of the expression is true, statement1 is executed otherwise statement2 is executed 
statement1 and statement2 are any C++ statements 
else is a reserved word 
Statement3 is executed in any case


Compound Statement Example:
if(age > 18)
{
cout<<" Eligible to vote."<<endl;
cout<<" No longer a minor."<<endl;
}
else
{
cout<<"Not eligible to vote."<<endl;
cout<<"Still a minor."<<endl;
}


Nested if
When one control statement is within another, it is said to be nested 
An else is associated with the most recent if that has not been paired with an else

if (score > 100 || score < 0)
cout<<“Score is out of range”<<endl;
else if(score >= 90)
cout<<"The grade is A"<<endl;
else if(score >= 80)
cout<<"The grade is B+"<<endl;
else if(score >= 70)
cout<<"The grade is B"<<endl;
else if(score >= 60)
cout<<"The grade is C+"<<endl;
else if(score >= 50)
cout<<"The grade is C"<<endl;
else
cout<<"The grade is F"<<endl;



switch Structures
In a switch structure, the expression is evaluated first
Next, the value of the expression is used to perform the corresponding action
The expression is usually an identifier
It is sometimes called the selector
The expression value can be only integral
Its value determines which statement is selected for execution
A particular case value should appear only once
One or more statements may follow a case label 
Braces are not needed to turn multiple statements into a single compound statement 
The break statement may or may not appear after each statement 
The break statement has a special meaning and may or may not appear after each statement 
switch, case, break, and default are reserved words

When the value of the expression is matched against a case value, the statements execute until a break statement is found or the end of the switch structure is reached 
If the value of the expression does not match any of the case values, the statements following the default label execute. If there is no default label, and if the value of the expression does not match any of the case values, the entire switch statement is skipped 
A break statement causes an immediate exit from the switch structure.

switch (integer expression)
case value1: case value2:
statement; break;
case value3: case value4:
statement;
break;
.
.
.
default: statement;

 
biz.