Switch
Switch expression :The expression whose value determines which switch lable is selected. it cannot be a floating-point or string expression.switch(letter){
case 'x' : Statement 1;
break;
case 'L'
case 'x' : Statement 2;
break;
case 's' : Statement 3;
break;
default : Statement 4;
}
Statement 5;
In this example , letter is the switch expression. The statement means "If letter is 'x', execute Statement1 and break out of the Switch statement,continuing with Statement 5.If letter is 'L' or 'M',execute Statement2 and continue with Statement 5.If letter is none of the characters mentioned,execute Statement 4 and continue with Statement 5. "The Break statement causes an immediate exit from the Switch statement. We'll see shortly what happens if we omit the Break statements.
Example
int vehicle_class;
Switch (vehicle_class)
{
case 1 :
cout<< "Passenger car." ;
toll = 0.50 ;
break ;
case 2 :
cout<<"Bus." ;
toll = 1.50 ;
break;
case 3 :
cout<<"Truck." ;
toll = 2.00 ;
break ;
default :
cout << "Unknow vehicle class!" ;
}
Programming in C++ / Nell Dale,Chip Weems.
ISBN : 0-7637-3234-6(pbk.)
page : 320
Break
ExampleloopCount = 1 ;
while(true){
cin >> num1 ;
if ( !cin || num 1>= 100)
break ;
cin >> num2 ;
if ( !cin || num 2 <= 50 )
break ;
cout << sqrt(float(num1+num2)) << end1 ;
loopCount++;
if ( loopCount > 10)
break ;
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น