Created
June 6, 2023 03:33
-
-
Save Asyraf-wa/39c20ea631390664bc58b98465d7ac69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //IF Else | |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int number; | |
| cout << "Enter an integer: "; | |
| cin >> number; | |
| // checks if the number is positive | |
| if (number > 0) { | |
| cout << "You entered a positive integer: " << number << endl; | |
| } | |
| cout << "This statement is always executed."; | |
| return 0; | |
| } | |
| //========================================================== | |
| //IF ELSE | |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int number; | |
| cout << "Enter an integer: "; | |
| cin >> number; | |
| if (number >= 0) { | |
| cout << "You entered a positive integer: " << number << endl; | |
| } | |
| else { | |
| cout << "You entered a negative integer: " << number << endl; | |
| } | |
| cout << "This line is always printed."; | |
| return 0; | |
| } | |
| //========================================================== | |
| //IF ELSE…ELSE IF | |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int number; | |
| cout << "Enter an integer: "; | |
| cin >> number; | |
| if (number > 0) { | |
| cout << "You entered a positive integer: " << number << endl; | |
| } | |
| else if (number < 0) { | |
| cout << "You entered a negative integer: " << number << endl; | |
| } | |
| else { | |
| cout << "You entered 0." << endl; | |
| } | |
| cout << "This line is always printed."; | |
| return 0; | |
| } | |
| //========================================================== | |
| //NESTED IF | |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int num; | |
| cout << "Enter an integer: "; | |
| cin >> num; | |
| // outer if condition | |
| if (num != 0) { | |
| // inner if condition | |
| if (num > 0) { | |
| cout << "The number is positive." << endl; | |
| } | |
| // inner else condition | |
| else { | |
| cout << "The number is negative." << endl; | |
| } | |
| } | |
| // outer else condition | |
| else { | |
| cout << "The number is 0 and it is neither positive nor negative." << endl; | |
| } | |
| cout << "This line is always printed." << endl; | |
| return 0; | |
| } | |
| //========================================================== | |
| //SWITCH CASE | |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| char oper; | |
| float num1, num2; | |
| cout << "Enter an operator (+, -, *, /): "; | |
| cin >> oper; | |
| cout << "Enter two numbers: " << endl; | |
| cin >> num1 >> num2; | |
| switch (oper) { | |
| case '+': | |
| cout << num1 << " + " << num2 << " = " << num1 + num2; | |
| break; | |
| case '-': | |
| cout << num1 << " - " << num2 << " = " << num1 - num2; | |
| break; | |
| case '*': | |
| cout << num1 << " * " << num2 << " = " << num1 * num2; | |
| break; | |
| case '/': | |
| cout << num1 << " / " << num2 << " = " << num1 / num2; | |
| break; | |
| default: | |
| // operator is doesn't match any case constant (+, -, *, /) | |
| cout << "Error! The operator is not correct"; | |
| break; | |
| } | |
| return 0; | |
| } | |
| //========================================================== | |
| //REPEAT | |
| int main() | |
| { | |
| bool repeat = true; | |
| while (repeat) | |
| { | |
| // stuff to be repeated here | |
| cout << "Repeat? [y/n]" << endl; | |
| char answer; | |
| cin >> answer; | |
| repeat = answer == 'y'; | |
| } | |
| cout << "Thank you, goodbye" << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment