SELECTION
IF STATEMENTS
- if statements use logical statements to make a choice
if (condition)
{
if condition is true then do this;
}
else
{
if condition is false do this;
}
- This is an exactly example of how If statements work:
#include <iostream>
using namespace std;
string grades(int p)
{
if (p<40)
return(” fail”);
else if (p<50)
return(” Thrid”);
else if (p<60)
return(” lower second”);
else if (p<70)
return(” higher second”);
else
return (“first”);
}
SWITCH STATEMENTS
- Switch case is used when you have several choices. It says “switch the path you take through the code based on the value of some variable”. A switch can be done only on the value of a single variable.
- In C++ this is wrote like this:
int choice;
switch (choice)
{
case 0:
if choice = 0 then do this;
break;
case 1:
if choice = 1 then do this;
break;
default:
if choice is anything else do this;
}