cplusplus:selection_examples
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:selection_examples [2013/02/03 01:57] – created mithat | cplusplus:selection_examples [2013/02/08 01:59] – [validating input] mithat | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== if ===== | ===== if ===== | ||
- | <file c++ if_example.cpp># | + | <file c++ if_example.cpp> |
+ | #include < | ||
using namespace std; | using namespace std; | ||
Line 14: | Line 15: | ||
if (grade > 60) | if (grade > 60) | ||
- | cout << " | + | cout << " |
return 0; | return 0; | ||
Line 21: | Line 22: | ||
===== if/else ===== | ===== if/else ===== | ||
+ | <file c++ if_else_example.cpp>/ | ||
+ | #include < | ||
+ | using namespace std; | ||
- | ===== Nested | + | int main() |
+ | { | ||
+ | int num1; | ||
+ | cout << "Enter an integer, and I will tell you "; | ||
+ | cout << "if the number is positive: "; | ||
+ | cin >> num1; // read an integer | ||
+ | |||
+ | if (num1 > 0) | ||
+ | cout << num1 << " is positive. "<< | ||
+ | else | ||
+ | cout << num1 << " is not positive. "<< | ||
+ | |||
+ | return 0; | ||
+ | }</ | ||
+ | |||
+ | |||
+ | ===== 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; | ||
+ | }</ | ||
cplusplus/selection_examples.txt · Last modified: 2013/02/11 01:00 by mithat