Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- /* 0. PREPREQUISITES: Comparison Operators
- Greater than: >
- Less than: <
- Greater or equal: >=
- Less or equal: <=
- Equal: ==
- Not equal: !=
- */
- /* 1. Basic structure:
- if ( condition ) {
- [lines of code to execute if true];
- }
- */
- cout << "How old are you? \n";
- int age;
- cin >> age;
- if (age >= 18) {
- cout << "Please come in!\n";
- }
- cout << "Exiting...\n";
- // 2. Else statements
- /*
- if ( condition ) {
- [lines of code to execute if true];
- }
- else {
- [lines of code to execute if false];
- }
- */
- if (age >= 18) {
- cout << "Please come in!\n";
- }
- else {
- cout << "You are not old enough!\n";
- }
- // 3. Else if statements
- /*
- if ( condition ) {
- [ lines of code to execute if true ];
- }
- else if ( condition2 ) {
- [ lines of code to execute if condition2 is true ];
- }
- else {
- [ lines of code to execute if all conditions are false];
- }
- */
- if (age < 18) {
- cout << "You are not old enough!\n";
- }
- else if (age > 30) {
- cout << "You are not young enough!\n";
- }
- else {
- cout << "Please come in!\n";
- }
- // 4. One-statement ifs
- if (age < 18)
- cout << "You are not old enough!\n";
- else if (age > 30)
- cout << "You are not young enough!\n";
- else
- cout << "Please come in!\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement