User Tools

Site Tools


cplusplus:selection_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
cplusplus:selection_examples [2013/02/03 01:57] – created mithatcplusplus:selection_examples [2013/02/03 02:53] mithat
Line 3: Line 3:
 ===== if ===== ===== if =====
  
-<file c++ if_example.cpp>#include <iostream>+<file c++ if_example.cpp>/* Using if statements */ 
 +#include <iostream>
 using namespace std; using namespace std;
  
Line 21: Line 22:
 ===== if/else ===== ===== if/else =====
  
 +<file c++ if_else_example.cpp>/* Using if/else statements */
 +#include <iostream>
 +using namespace std;
  
-===== Nested if/else =====+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. "<< endl; 
 +    else 
 +        cout << num1 << " is not positive. "<< endl; 
 + 
 +    return 0; 
 +}</file> 
 + 
 + 
 +===== nested if/else ===== 
 + 
 +<file c++ nested_if_else_example.cpp>/* Using nested if/else statements */ 
 +#include <iostream> 
 +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;    // read an integer 
 + 
 +    cout << "The student received a letter grade of "; 
 +    if ( grade >= 90 ) 
 +        cout << "A"; 
 +    else if ( grade >= 80 ) 
 +        cout << "B"; 
 +    else if ( grade >= 70 ) 
 +        cout << "C"; 
 +    else if ( grade >= 60 ) 
 +        cout << "D"; 
 +    else 
 +        cout << "F"; 
 +    cout << "." << endl; 
 + 
 +    return 0; 
 +}</file>
  
cplusplus/selection_examples.txt · Last modified: 2013/02/11 01:00 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki