Single If statement

Feb 5, 2017 at 1:33am
I have to turn in my code and just realized one of the requirements is to have just one single if statement. I have a lot of nested if statements and cant figure out how to make just one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
 #include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

int main() {
 
double milesPerYear = 0;   
double pricePerGallon = 0;
const double YEARS = 5;
double initialCostOfHybrid = 0;
double efficiencyHybridMpg = 0;
double resaleValueHybrid = 0;
double initialCostOfNonHybrid = 0;
double efficiencyNonHybridMpg = 0;
double resaleValueNonHybrid = 0;
string buyingCriterion;
double totalGallonsHybrid = 0;
double totalGallonsNonHybrid = 0;
double fuelCostHybrid = 0;
double depreciationOfHybrid = 0;
double totalCostOfHybrid = 0;
double fuelCostNonHybrid = 0;
double depreciationOfNonHybrid = 0;
double totalCostOfNonHybrid = 0;

cout << "Please enter the following:";
cout << endl;
cout << endl;

cout << "The estimated miles driven per year: ";
cout << endl;
cin >> milesPerYear;
   if (milesPerYear <= 0) {
		cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
		cout << endl; //error message
		cout << "The estimated miles driven per year: ";
		cout << endl;
		cin >> milesPerYear;
	}
	
cout << "The estimated price of a gallon of gas during the 5 years of ownership: "; 
cout << endl;
cin >> pricePerGallon;
 if (pricePerGallon <= 0) {
		cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
		cout << endl;
		cout << "The estimated price of a gallon of gas during the 5 years of ownership: ";
		cout << endl;
		cin >> pricePerGallon;
	}
	
cout << "The initial cost of a hybrid car: ";
cout << endl;
cin >> initialCostOfHybrid;
 if (initialCostOfHybrid <= 0) {
		cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
		cout << endl;
		cout << "The initial cost of a hybrid car: ";
		cout << endl;
		cin >> initialCostOfHybrid;
	}
	
cout << "The efficiency of the hybrid car in miles per gallon: ";
cout << endl;
cin >> efficiencyHybridMpg;
if (efficiencyHybridMpg <= 0) {
		cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
		cout << endl;
		cout << "The efficiency of the hybrid car in miles per gallon: ";
		cout << endl;
		cin >> efficiencyHybridMpg;
	}
	
cout << "The estimated resale value (a dollar amount) for a hybrid after 5 years: ";
cout << endl;
cin >> resaleValueHybrid;
if (resaleValueHybrid <= 0) {
		cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
		cout << endl;
		cout << "The estimated resale value (a dollar amount) for a hybrid after 5 years: ";
		cout << endl;
		cin >> resaleValueHybrid;
	}
	
cout << "The initial cost of a non-hybrid car: ";
cout << endl;
cin >> initialCostOfNonHybrid;
if (initialCostOfNonHybrid <= 0) {
		cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
		cout << endl;
		cout << "The initial cost of a non-hybrid car: ";
		cout << endl;
		cin >> initialCostOfNonHybrid;
	}
	
cout << "The efficiency of the non-hybrid car in miles per gallon: ";
cout << endl;
cin >> efficiencyNonHybridMpg;
if (efficiencyNonHybridMpg <= 0) {
		cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
		cout << endl;
		cout << "The efficiency of the non-hybrid car in miles per gallon: ";
		cout << endl;
		cin >> efficiencyNonHybridMpg;
	}
	
cout << "The estimated resale value (a dollar amount) for a non-hybrid after 5 years: ";
cout << endl;
cin >> resaleValueNonHybrid;
if (resaleValueNonHybrid <= 0) {
		cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
		cout << endl;
		cout << "The estimated resale value (a dollar amount) for a non-hybrid after 5 years: ";
		cout << endl;
		cin >> resaleValueNonHybrid;
	}
	
cout << "The user's buying criterion, either minimized gas consumption";
cout << " or total cost (enter Gas or Total): ";
cout << endl;
cin >> buyingCriterion;


totalGallonsHybrid = (milesPerYear * YEARS) / efficiencyHybridMpg;
totalGallonsNonHybrid = (milesPerYear * YEARS) / efficiencyNonHybridMpg;
fuelCostHybrid = totalGallonsHybrid * pricePerGallon;
depreciationOfHybrid = initialCostOfHybrid - resaleValueHybrid;
totalCostOfHybrid = fuelCostHybrid + depreciationOfHybrid;
fuelCostNonHybrid = totalGallonsNonHybrid * pricePerGallon;
depreciationOfNonHybrid = initialCostOfNonHybrid - resaleValueNonHybrid;
totalCostOfNonHybrid = fuelCostNonHybrid + depreciationOfNonHybrid;

if (buyingCriterion == "Gas")
	{
		if (totalGallonsHybrid < totalGallonsNonHybrid) {
			cout << "For the hybrid car:" << endl << endl;
			cout << "The estimated total gallons of fuel consumed over 5 years: ";
			cout << fixed << setprecision(2) << totalGallonsHybrid << endl;
			cout << "The estimated total cost of owning the car for 5 years: ";
			cout << fixed << setprecision(2) << totalCostOfHybrid << endl;
         cout << endl;
			cout << "For the non-hybrid car:" << endl << endl;
			cout << "The estimated total gallons of fuel consumed over 5 years: ";
			cout << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
			cout << "The estimated total cost of owning the car for 5 years: ";
			cout << fixed << setprecision(2) << totalCostOfNonHybrid << endl << endl;
			cout << "** The hybrid car is better than the non-hybrid car when \"Gas\"";
			cout << " is the user's primary objective."; 
			cout << endl;
		}
		else {
		   cout << "For the hybrid car:" << endl << endl;
			cout << "The estimated total gallons of fuel consumed over 5 years: ";
			cout << fixed << setprecision(2) << totalGallonsHybrid << endl;
			cout << "The estimated total cost of owning the car for 5 years: " ;
			cout << fixed << setprecision(2) << totalCostOfHybrid << endl << endl;
			cout << "For the non-hybrid car:" << endl << endl;
			cout << "The estimated total gallons of fuel consumed over 5 years: ";
			cout << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
			cout << "The estimated total cost of owning the car for 5 years: ";
			cout << fixed << setprecision(2) << totalCostOfNonHybrid << endl << endl;
			cout << "** The non-hybrid car is better than the hybrid car when \"Gas\"";
			cout << " is the user's primary objective.";
			cout << endl;
         cout << endl;
		}

	}
else if (buyingCriterion == "Total") {
		if (totalCostOfHybrid < totalCostOfNonHybrid) {
			cout << "For the hybrid car:" << endl << endl;
			cout << "The estimated total gallons of fuel consumed over 5 years: ";
			cout << fixed << setprecision(2) << totalGallonsHybrid << endl;
			cout << "The estimated total cost of owning the car for 5 years: ";
			cout << fixed << setprecision(2) << totalCostOfHybrid << endl;
			cout << endl;
			cout << "For the non-hybrid car:" << endl << endl;
			cout << "The estimated total gallons of fuel consumed over 5 years: ";
			cout << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
			cout << "The estimated total cost of owning the car for 5 years: ";
			cout << fixed << setprecision(2) << totalCostOfNonHybrid << endl << endl;
			cout << "** The hybrid car is better than the non-hybrid car when \"Total\"";
			cout << " is the user's primary objective.";
			cout << endl;
		}
		else {
		   cout << "For the hybrid car:" << endl << endl;
			cout << "The estimated total gallons of fuel consumed over 5 years: ";
			cout << fixed << setprecision(2) << totalGallonsHybrid << endl;
			cout << "The estimated total cost of owning the car for 5 years: ";
			cout << fixed << setprecision(2) << totalCostOfHybrid << endl << endl;
			cout << "For the non-hybrid car:" << endl << endl;
			cout << "The estimated total gallons of fuel consumed over 5 years: ";
			cout << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
			cout << "The estimated total cost of owning the car for 5 years: ";
			cout << fixed << setprecision(2) << totalCostOfNonHybrid << endl << endl;
			cout << "** The non-hybrid car is better than the hybrid car when \"Total\"";
			cout << " is the user's primary objective.";
			cout << endl;
			cout << endl;
		}
	}

   return 0;
}
Feb 5, 2017 at 1:53am
one single if statement

This is a terribly stupid requirement.
If teachers are going to impose arbitrary restrictions on how you write your software, you would hope they were reasonable. This can't even be considered a useful exercise.

What exactly did the teacher write? I doubt the intent is actually to forbid all but one if-statement.

If the ban is simply of nested conditionals, then realize that
if (a) { if (b) x; else y; } else z;Is equivalent to
1
2
3
if (a && b) x;
if (a && !b) y;
if (!a) z;

The transformation is mechanical and can applied recursively if required.
Feb 5, 2017 at 2:04am
this is a key component of computer engineering, where circuits that are smaller are preferred. I only had one class on that stuff, as I went to the softer side, but in there we learned about this great thing called a Karnaugh map which does exactly that --- it can be used to convert unrelated conditions together to reduce the logic needed to perform the action. The technique has been invaluable over the decades.

That said, in C you have tons of options... switch statements, or nest your logic so that the most likely to fail condition is first, and other tweaks.

The above Boolean logic approaches are also very, very handy.

The trouble with condensation of logic is that the final statement is usually illogical to humans. If the terms are not related, you get utter nonsense like
if (carcolor = blue and engine = v8 and seats = leather) then something else... just because those 3 things are incidentally correlated and happen to give the correct result.

Anyway, give it a try and ask again if you get stuck.
Feb 5, 2017 at 2:08am
THis is what the instruction says,

"The way most want to solve this part of the lab is by using nested if-statements (that is, an if-statement inside an if-statement), however we would like you to learn how to condense your code and practice with boolean expressions. For example,

if (isCheese){
if (isHoly){
cout << "The food is swiss cheese!" << endl;
}
}
can be written as

if (isCheese && isHoly){
cout << "The food is swiss cheese!" << endl;
}"

My question is can you do that if you have an else statement!
Feb 5, 2017 at 2:33am
Trying to make sense of this, I would say:

if (isCheese && isHoly)
{
cout << "The food is swiss cheese!" << endl;
}
else
{
cout << "The food isnt swiss cheese!" << endl;
}

I personally think youll have to start over, condense the logic, and then grab the print statements from your current version after that to put it back together.
Last edited on Feb 5, 2017 at 2:41am
Feb 5, 2017 at 4:20am
My question is can you do that if you have an else statement!

Yes.
Did you see my previous post? Near the bottom, there's a brief example regarding how to do the transformation.

jonnin wrote:
This is a key component of computer engineering, where circuits that are smaller are preferred.

Good point -- I've got very little training when it comes to this stuff, but IIRC, K-maps help minimize the number of gates, i.e., the number of boolean operators / transistors, but not nesting. If you flatten the representation you'll end up with duplicate sub-circuits. The nesting isn't allowed in OP's case. I don't know in what circumstances the "flattened" representation will be an improvement.

An interesting note is that modern hardware is so sophisticated the circuit design is mostly performed by computer; the logic design problems have been far too large for humans for a long time.
Last edited on Feb 5, 2017 at 4:23am
Feb 5, 2017 at 4:58am
Darn can you just give me an example that similar to mine that i can visualize it better?
Feb 5, 2017 at 5:56am
Once you see it, it's dead simple:

The first thing I would do is correctly indent your program. It's easier to understand nesting with some sort of consistent visual clue.

Here is your entire program with everything except the control-statements removed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main() {
  if (milesPerYear <= 0) {}
  if (pricePerGallon <= 0) {}
  if (initialCostOfHybrid <= 0) {}
  if (efficiencyHybridMpg <= 0) {}
  if (resaleValueHybrid <= 0) {}
  if (initialCostOfNonHybrid <= 0)  {}
  if (efficiencyNonHybridMpg <= 0) {}
  if (resaleValueNonHybrid <= 0) {}

  if (buyingCriterion == "Gas") {
    if (totalGallonsHybrid < totalGallonsNonHybrid) {} 
    else {}
  } else if (buyingCriterion == "Total") {
    if (totalCostOfHybrid < totalCostOfNonHybrid) {}
    else {}
  }
}

Lines 2 through 10 are all flat. There's no nesting in those lines.
The only stuff you have to change is the nested conditionals on the lines in [11, 17]. Here are those lines. In between each set of empty braces is a label -- a, b, c, etc.; that label represents all the stuff that's in between the braces in your original program; those are the alternatives.
1
2
3
4
5
6
7
if (buyingCriterion == "Gas") {
  if (totalGallonsHybrid < totalGallonsNonHybrid) { a; } 
  else { b; }
} else if (buyingCriterion == "Total") {
  if (totalCostOfHybrid < totalCostOfNonHybrid) { c; }
  else { d; }
}


Which expands to
1
2
3
4
if (buyingCriterion == "Gas"   &&    totalGallonsHybrid < totalGallonsNonHybrid)  { a; }
if (buyingCriterion == "Gas"   && ! (totalGallonsHybrid < totalGallonsNonHybrid)) { b; }
if (buyingCriterion == "Total" &&    totalCostOfHybrid < totalCostOfNonHybrid)    { c; }
if (buyingCriterion == "Total" && ! (totalCostOfHybrid < totalCostOfNonHybrid))   { d; }

Take the stuff that corresponds to a, b, c, d in your original program and put it in the corresponding lines above - that's it.
Last edited on Feb 5, 2017 at 6:13am
Topic archived. No new replies allowed.