In: Computer Science
if(condition A is true){
//run this}
else if(condition B is true){
//run this}
else if(condition C is true){
//run this}
First it checks if Condition A is true. If true it runs the code
inside the curly braces and the program ends.
Only if 'A' is false it checks for Condition B to be true. If 'B'
is true the code inside the curly braces of that condition is
executed and the program ends.
If 'B' is also false it checks for Condition C to be true. If
true it executes the code inside the curly braces and the program
ends.
Note: In the code given below some of the output strings contain single quotes. You might have to type the single quotes again through your keyboard if they're not being displayed properly in the console window.
Please find attached the commented code below:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
/* declare the variables required in the program and
initialize them to default values*/
double previous_price = 0;
double current_price = 0;
double percentage = 0;
string symbol;
string company;
/* Take the input from the user for 'symbol',
'company', 'previous_price', 'current_price'*/
cout<<"--Welcome to CSUB
Brokerage--"<<endl;
cout<<"What is the stock symbol? ";
cin>>symbol;
cout<<"What is the company name? ";
cin>>company;
cout<<"What was the previous stock price?
";
cin>>previous_price;
cout<<"What is the current price? ";
cin>>current_price;
/*if the current price is greater then the previous
price run this block of code*/
if(current_price > previous_price){
percentage = (current_price -
previous_price)/previous_price*100;
cout<<"It's a bull market!
";
cout<<company << " ("
<< symbol << ") went up ";
cout<<fixed<<setprecision(2)<<percentage<<"%";
}
/*if the current price is less than the previous price
run this block of code*/
else if(current_price < previous_price){
percentage = (previous_price -
current_price)/previous_price*100;
cout<<"It's a bear market!
";
cout<<company << " ("
<< symbol << ") went down ";
cout<<fixed<<setprecision(2)<<percentage<<"%";
}
/* if current price and previous price are same, run
this*/
/* we can also use just the else condition for this
part */
else if(current_price == previous_price){
cout<<"It's a flat market!
";
cout<<company << " ("
<< symbol << ") stock price didn't change. ";
}
return 0;
}