Fix some indentation and change some typo (#22017)

This commit is contained in:
fadelpamungkas
2018-11-18 10:13:05 +07:00
committed by Christopher McCormack
parent 1606de6c51
commit d93ad2c00b

View File

@ -62,11 +62,11 @@ else
//Program to check whether number entered by user is positive or negative //Program to check whether number entered by user is positive or negative
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int no; int no;
cout << "Enter a number: " << endl; cout << "Enter a number: " << endl;
cin >> no; cin >> no;
// condition to check if number is positive or negative // condition to check if number is positive or negative
@ -116,19 +116,22 @@ This step is always printed
If we have to make decisions based on more than one conditions using if else. We use else if condition as follows - If we have to make decisions based on more than one conditions using if else. We use else if condition as follows -
```cpp ```cpp
#include<iostream> #include<iostream>
using namespace std;
int main() int main()
{ {
int score; int score;
std::cout<<"Enter your score: \n"; cout << "Enter your score: \n";
std::cin>>score; cin >> score;
if(score>=90)
std::cout<<"Top performance."; if (score >= 90)
else if(score<90 && score>=70) cout << "Top performance.";
std::cout<<"Good performance"; else if (score < 90 && score >= 70)
else if(score<70 && score>=45) cout << "Good performance";
std::cout<<"Average performance"; else if (score < 70 && score >= 45)
else if(score<45 && score>=30) cout << "Average performance";
std::cout<<"You can improve it."; else if (score < 45 && score >= 30)
cout << "You can improve it.";
return 0; return 0;
} }
``` ```
@ -142,11 +145,12 @@ Good performance
### Another example of if...else if...else ladder ### Another example of if...else if...else ladder
Suppose we have the user input two numbers and we are going to display if either number is greater than the other. And if neither is greater than the other, then we print the statement "Both are equal". Suppose we have the user input two numbers and we are going to display if either number is greater than the other. And if neither is greater than the other, then we print the statement "Both are equal".
In this scinerio we will need an if...else if...else ladder statement. The program will look like this : In this scenario, we will need an if...else if...else ladder statement. The program will look like this :
``` ```
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int number1,number2; int number1,number2;
@ -155,11 +159,11 @@ int main()
cout << "Enter second number: \n"; cout << "Enter second number: \n";
cin >> number2; cin >> number2;
if(number1 > number2) // Checks if the first number is greater than the second number if (number1 > number2) // Checks if the first number is greater than the second number
{ {
cout << "Number 1 is greater."; cout << "Number 1 is greater.";
} }
else if(number2 > number1) // Checks if the second number is greater than the first number else if (number2 > number1) // Checks if the second number is greater than the first number
{ {
cout << "Number 2 is greater."; cout << "Number 2 is greater.";
} }