cplusplus:selection_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:selection_examples [2013/02/03 02:50] – mithat | cplusplus:selection_examples [2013/02/11 01:00] (current) – [switch] mithat | ||
---|---|---|---|
Line 15: | Line 15: | ||
if (grade > 60) | if (grade > 60) | ||
- | cout << " | + | cout << " |
return 0; | return 0; | ||
Line 21: | Line 21: | ||
===== if/else ===== | ===== if/else ===== | ||
+ | |||
<file c++ if_else_example.cpp>/ | <file c++ if_else_example.cpp>/ | ||
#include < | #include < | ||
Line 41: | Line 42: | ||
- | ===== Nested | + | ===== nested |
+ | |||
+ | <file c++ nested_if_else_example.cpp>/ | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int grade; | ||
+ | cout << "Enter a numerical grade, and I will tell you" << endl; | ||
+ | cout << "how well the student performed: "; | ||
+ | cin >> grade; | ||
+ | |||
+ | cout << "The student received a letter grade of "; | ||
+ | if (grade >= 90 ) | ||
+ | cout << " | ||
+ | else if (grade >= 80) | ||
+ | cout << " | ||
+ | else if (grade >= 70) | ||
+ | cout << " | ||
+ | else if (grade >= 60) | ||
+ | cout << " | ||
+ | else | ||
+ | cout << " | ||
+ | cout << " | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== validating input ===== | ||
+ | |||
+ | <file c++ validating_input_example.cpp>/ | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int grade; | ||
+ | cout << "Enter a numerical grade, and I will tell you" << endl; | ||
+ | cout << "how well the student performed: "; | ||
+ | cin >> grade; | ||
+ | |||
+ | if (grade >= 0 && grade <= 100) | ||
+ | { | ||
+ | cout << "The student received a letter grade of "; | ||
+ | if (grade >= 90 ) | ||
+ | cout << " | ||
+ | else if (grade >= 80) | ||
+ | cout << " | ||
+ | else if (grade >= 70) | ||
+ | cout << " | ||
+ | else if (grade >= 60) | ||
+ | cout << " | ||
+ | else | ||
+ | cout << " | ||
+ | cout << " | ||
+ | } | ||
+ | else | ||
+ | cout << "The grade must be between 0 and 100." << endl; | ||
+ | |||
+ | return 0; | ||
+ | }</ | ||
+ | |||
+ | ===== comparing characters ===== | ||
+ | |||
+ | <file c++ comparing_characters.cpp>/ | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | char ch; | ||
+ | |||
+ | // Get a character from the user. | ||
+ | cout << "Enter a letter: "; | ||
+ | ch = cin.get(); | ||
+ | |||
+ | // Did user enter a letter? | ||
+ | if (ch >= ' | ||
+ | cout << "You entered a character between ' | ||
+ | | ||
+ | cout << "You entered something else." << endl; | ||
+ | |||
+ | | ||
+ | }</ | ||
+ | |||
+ | ===== switch ===== | ||
+ | <file c++ simple_switch_example.cpp>/ | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int selection; | ||
+ | |||
+ | // Prompt the user for a selection: | ||
+ | cout << " | ||
+ | << | ||
+ | << | ||
+ | << | ||
+ | << | ||
+ | cin >> selection; | ||
+ | |||
+ | // Give the user what they asked for: | ||
+ | switch (selection) | ||
+ | { | ||
+ | case 1: | ||
+ | cout << "You entered 1." << endl; | ||
+ | cout << "A foo on all your houses!" | ||
+ | break; | ||
+ | case 2: | ||
+ | cout << "You entered 2." << endl; | ||
+ | cout << "A bar on all your houses!" | ||
+ | break; | ||
+ | case 3: | ||
+ | cout << "You entered 3." << endl; | ||
+ | cout << "A baz on all your houses!" | ||
+ | break; | ||
+ | default: | ||
+ | cout << "You did not enter 1, 2, or 3." << endl; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | }</ | ||
cplusplus/selection_examples.1359859851.txt.gz · Last modified: 2013/02/03 02:50 by mithat