else part of the if else statement is not working
Your if
s/else
s are not exhaustive. For instance, if a > b
and c > a
is not covered: inside the if (a>b)
, you only consider if (a > c)
, and if this condition is false, you do nothing (meaning, there is no else).
You seem to be under the impression that the final else
(which prints "c is greatest"
) should be executed after each if
that has no else
. However, this is not how C works: this else
is only executed if the condition of the if
that precedes it is false. The fixed version of your first attempt is thus:
if(a>b){
if(a>c){
printf("a is greatest");
} else{
printf("c is greatest");
}
}
else if(b>a){
if(b>c){
printf("b is greatest");
} else{
printf("c is greatest");
}
}
else{
printf("c is greatest");
}
However, the second version (with the &&
) is clearer in my opinion.
Finally, when you remove the braces after if(b>a)
(your 3rd version), the final else
is associated with the if
that precedes it, which is if(b>c)
rather than if(b>a)
(unlike when the braces are present). Your indentation is misleading, and should have been:
else if(b>a)
if(b>c){
printf("b is greatest");
}
else {
printf("c is greatest");
}
The same thing holds for your 4th version, which should be read as:
if(a>b)
if(a>c)
printf("a is greatest");
else if(b>a)
if(b>c)
printf("b is greatest");
else
printf("c is greatest");
Notice how if(b>a)
can never be true since it is inside the body of if(a>b)
.
Additionally, you never consider that some or all numbers could be equal. Without the broader context, we can't know if this can happen, but if it can happen, then you should probably take that case into account.
C++ if/else if statement not working correctly
cin >> input;
does not read whitespaces. You need to use std::getline
.
getline(cin, input);
C++ if else if not working properly
Instead of this if(ID =="A"||"a"){
, you should write if(ID =="A"|| ID == "a"){
because your statements: if(ID =="A"||"a"){
means if (ID is "A" or the ascii of "a"). The ascii of "a" is bigger than 0, that means it will be read as true
value in the if statement. That's why your statement will be equal with if(ID =="A"|| true){
which is why only this statement will be executed.
`else if` condition not properly working in my program in c
You are assigning a value to gen
else if ((gen == 'M' && y_o_s >= 10 && qual == 0) ||
(gen = 'M' && y_o_s < 10 && qual == 1))
^
So when you get to your next line gen
is no longer what you expect.
else if (gen == 'M' && y_o_s < 10 && qual == 0)
^^
And then improve the code with SparKots suggestions.
Else condition not working even tho there is no error
The expression in the if statement
else if(1<Number_Of_People<=6){
may be equivalently rewritten like
else if( ( 1 < Number_Of_People ) <=6 ){
The relational operator <
yields either 0 or 1 depending on whether the expression with the relational operator is logically false or true.
Thus the result of the subexpression ( 1 < Number_Of_People )
either equal to 0 or 1 in any case is less than 6
.
So this if statement will be always evaluated if the preceding if statement will be skipped.
You need to rewrite the above if operator like
else if( ( 1 < Number_Of_People ) && ( Number_Of_People <= 6 ) ){
c: last loop in if elseif else is not working
I can't figure out why it won't go again in first "if", after in last
"else" ("Introduceti d sau n! "), I write 'n'. The 'd' is working fine
and going back to "else if".
Because of your while
loop condition(while(decizie != 'n')
). Loop will execute as long as you give anything but n
as input. It won't go in the first if
as you're giving n
as the input.
C If statements not working
Prior to the commenting, each of the last three aaa/bbb/ccc if statements was nested within the previous such if statement. Thus, when if (aaa == bbb && aaa != ccc)
tested as false, all the rest of the code up to the line of close braces was skipped.
In other words, what it really did was this:
if (aaa == bbb && aaa != ccc) {
int r = rand() % 2 + 1;
printf("aRANDOM2 = %d\n",r);
if (r == 1) {aaa = (aaa + 1);}
if (r == 2) {bbb = (bbb + 1);}
if (aaa == ccc && aaa != bbb) {
int r = rand() % 2 + 1;
printf("bRANDOM2 = %d\n",r);
if (r == 1) {aaa = (aaa + 1);}
if (r == 2) {ccc = (ccc + 1);}
if (ccc == bbb && aaa != ccc) {
int r = rand() % 2 + 1;
printf("cRANDOM2 = %d\n",r);
if (r == 1) {ccc = (ccc + 1);}
if (r == 2) {bbb = (bbb + 1);}
if (aaa == bbb && bbb == ccc) {
int r = rand() % 3 + 1;
printf("RANDOM3 = %d\n",r);
if (r == 1) {aaa = (aaa + 1);}
if (r == 2) {bbb = (bbb + 1);}
if (r == 3) {ccc = (ccc + 1);}
}
}
}
}
}
Probably what you want to do is to move three of the close braces to the end of their respective blocks, like this:
if (aaa == bbb && aaa != ccc) {
int r = rand() % 2 + 1;
printf("aRANDOM2 = %d\n",r);
if (r == 1) {aaa = (aaa + 1);}
if (r == 2) {bbb = (bbb + 1);}
}
if (aaa == ccc && aaa != bbb) {
int r = rand() % 2 + 1;
printf("bRANDOM2 = %d\n",r);
if (r == 1) {aaa = (aaa + 1);}
if (r == 2) {ccc = (ccc + 1);}
}
if (ccc == bbb && aaa != ccc) {
int r = rand() % 2 + 1;
printf("cRANDOM2 = %d\n",r);
if (r == 1) {ccc = (ccc + 1);}
if (r == 2) {bbb = (bbb + 1);}
}
if (aaa == bbb && bbb == ccc) {
int r = rand() % 3 + 1;
printf("RANDOM3 = %d\n",r);
if (r == 1) {aaa = (aaa + 1);}
if (r == 2) {bbb = (bbb + 1);}
if (r == 3) {ccc = (ccc + 1);}
}
}
Related Topics
Visual Studio 2013 Fatal Error C1041 /Fs
Print Out All Combinations of Index
What Changes Introduced in C++14 Can Potentially Break a Program Written in C++11
What Are These Seemingly-Useless Callq Instructions in My X86 Object Files For
How to Detect Code Duplication During Development
Explicitly Exporting Shared Library Functions in Linux
How to Throw Std::Exceptions with Variable Messages
C++: Difference Between Member and Non Member Functions
Restrict Passed Parameter to a String Literal
Will My iPhone App Take a Performance Hit If I Use Objective-C for Low Level Code
Unary Plus (+) Against Literal String
Why C++11 Compiler Support Still Requires a Flag
Is It Possible for the Executable to Ask for Administrator Rights? (Windows 7)