Saturday, 12 October 2013

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;

Unknown

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment

 
biz.