Nested IF statements vs IF-ELSE - c++

I'm learning C language usnig Turbo C++ compiler and just in time I encountered the two statements:
IF (nested with many IFs)
IF-else(not nested but continuing else,else and so on)
I was wondering if my idea is correct or not that IF (nested with many IFs) and IF-else(not nested) are the same? Suggestions are well appreciated.

That's only basic logic behind that:
Nested if conditions:
IF first condition's value is true, go into the second condition.
if(a > 0)
{
printf("A is greater than 0\n");
if(a > 2) printf("A is greater than 0 and 2\n");
}
if-else condition:
IF first condition's value is false, go to the next:
if(a > 0) printf("A is greater than zero\n");
else if(a < 0) printf("A is lesser than zero\n");
else printf("A is zero\n");
There is one more instruction that you should know, switch:
switch(a)
{
case 0: printf("A is zero\n"); break;
case 1: printf("A is one\n"); break;
case 5: printf("A is five\n"); break;
default: printf("A is not 0, 1 or 5\n"); break;
}

Nested if is not equivalent to if-else. It can be equivalent to single if with a combined condition, for instance:
if (a == 1) {
if (b == 2) {
...
}
}
is equivalent to:
if (a == 1 && b == 2) {
...
}

I guess you rather mean if this:
if(expression){
//code
}
else{
if(expression){
//code
}
}
is equivalent to this:
if(expression){
//code
}
else if(expression){
//code
}
and yes it's absolutely the same. Second one is just better looking way of doing this.

The else if blocks are in fact nested else’s since C and C++ don’t have any special support for “elseif” or “elif” concept (not speaking about the preprocessor directives now). It gets obvious with strict use of blocks and indentation:
if(something) { doSomething(); }
else {
if(anotherThing) { doAnotherThing(); }
else {
if(yetAnotherThing) { doYetAnotherThing(); }
else
{ doSomethingElse(); }
}
}
The same code written with the usual else if notation:
if(something) { doSomething(); }
else if(anotherThing) { doAnotherThing(); }
else if(yetAnotherThing) { doYetAnotherThing(); }
else { doSomethingElse(); }
And as Mateusz Kwaśniak has mentioned, you should prefer switch over else if when possible. However, it’s not available for string comparison, for example.

Related

Can somebody tell me how this code working with no curly braces between two if?

If I am enclosing the code after first if upto second return in curly braces it is not giving me desired output.
static int comparator(Player a, Player b) {
if(a.score == b.score)
if(a.name == b.name)
return 0;
else
return (a.name > b.name)? -1:1;
return (a.score < b.score)? -1:1;
}
Your code has if() and else statements. Each will execute one line of code that comes after them. This means that it will only execute a single statement and end after the first ; that it finds.
for() loops, while() loops, if-else blocks can be used without curly braces if the statement you want to execute consists of only one line of code following them.
Your code works as -
static int comparator(Player a, Player b) {
// if statement without braces- means just one statement executes
if(a.score == b.score)
// Remember if-else will be considered as a single code block so both will run
if(a.name == b.name)
return 0;
else
return (a.name > b.name)? -1:1;
// This statement will run only when the above if condition is not satisfied
return (a.score < b.score)? -1:1;
}
This can be considered to be same as -
static int comparator(Player a, Player b) {
if(a.score == b.score) {
if(a.name == b.name) {
return 0;
} else {
return (a.name > b.name) ? -1 : 1;
}
}
return (a.score < b.score) ? -1 : 1;
}
NOTE : It is generally better if you use the braces as it will be good for readability as well as maintainability of the code. There can actually be two way of parsing it - Dangling else(though most compiler will associate the else with closest if).
In this coding style, there's no way to differentiate between below two code -
if(condition1)
if(condition2)
foo1();
else
foo2();
and,
if(condition1)
if(condition2)
foo1();
else
foo2();
Since, in C/C++, it doesn't consider the indentation in code, so it might create ambiguity while reading the code. So its always better to use curly braces instead of doing it like above. Drop them only when you have a single line and it won't create any confusion reading the code later on...
Hope this helps !
Without curly braces, only the next statement is executed. With proper indentation it becomes easier to see what's going on:
static int comparator(Player a, Player b) {
if(a.score == b.score)
if(a.name == b.name)
return 0;
else
return (a.name > b.name) ? -1 : 1;
return (a.score < b.score) ? -1 : 1;
}
This is actually the same as:
static int comparator(Player a, Player b) {
if(a.score == b.score) {
if(a.name == b.name) {
return 0;
} else {
return (a.name > b.name) ? -1 : 1;
}
}
return (a.score < b.score) ? -1 : 1;
}
You have maybe used the braceless else variant without noticing it when writing something like:
if(condition) {
//
} else if(another_condition) {
//
} else {
//
}
Which is actually the same as
if(condition) {
//
} else {
if(another_condition) {
//
} else {
//
}
}
Without curly braces, the if guard only applies to the immediate next statement.
It's just how the language works. :/

Autogenerate cases for a switch. Or another suggestion

Is there any way to autogenerate cases if necessary (with certain logic described by example) for a switch? Or maybe you have another suggestion. some code is always the same.
int num = 0; // Global variable
.
.
.
switch (num)
{
case 0:
{
//some code
num++;
break;
}
case 1:
{
if (CHECK(1)) // CHECK is macros for comparing
{
//some code
num++;
}
break;
}
case 2:
{
if (CHECK(1) && CHECK(2))
{
//some code
num++;
}
break;
}
case 3:
{
if (CHECK(1) && CHECK(2) && CHECK(3))
{
//some code
num++;
}
break;
}
case 4 ...
... and so on
Unless you're doing something fishy inside CHECK, it should be as easy as a for loop:
for(int i = 1; i <= num; ++i)
if(!CHECK(i))
return;
// some code
++num;
Sure. You can generate whatever code you need, stick it in a file, and then #include the generated file wherever needed in your source file.
Doing that can sometimes be a good idea and sometimes a horrible idea. It all depends on your code/problem/circumstances.
I have proposed using templates in this case.
template <int level>
bool check()
{
return CHECK(level) && check<level - 1>();
}
template <>
bool check<0>() { return true;}
template <int level>
void caseCheck(int& num)
{
if (num == level)
{
if (check<level>())
// some code
num++;
}
else
caseCheck<level - 1>(num);
}
template <>
void caseCheck<0>(int& num)
{
// some code
num++;
}
caseCheck<NUM_CASES>(num);

Refactor several nested if with same else

This is not about an existing piece of code but I'm looking for some pattern that may exist in the case that some nested if perform the same thing in their else statement.
if(condition1(a)) {
doSomethingWith1(a);
if(condition2(a)) {
doSomethingWith2(a);
} else {
elseFn();
}
} else {
elseFn();
}
The doSomethingWith... functions are changing the value of a, making it complex to have all the condition in one if.
So I'm just wondering if there is a clearer way to write it (in C, if possible).
Thanks guys
in your case, looks like the first if, if(condition1(a)), is absolutely necessary to test for the value of a before calling doSomethingWith1(a); to avoid an exception. so, no, there is no other way to do it.
if(condition1(a)) {
doSomethingWith1(a);
if(condition2(a)) {
doSomethingWith2(a);
} else {
elseFn();
}
} else {
elseFn();
}
You could just keep a count of the "doSomethings" and invoke the elseFn unless all were executed.
int count = 0;
if (condition1(a)) {
doSomethingWith1(a);
count++;
if (condition2(a)) {
doSomethingWith2(a);
count++;
if (condition2(a)) {
doSomethingWith2(a);
count++;
}
}
}
if (count < 3) {
elseFn();
}
I find it more readable, though less efficient, to double-check the first condition. This refactoring eliminates nesting, without multiple functions. It also more clearly shows three distinct paths of execution by grouping each logical path into a single code block.
if (condition1(a) && condition2(a)) {
doSomethingWith1(a);
doSomethingWith2(a);
}
else if (condition1(a)) {
doSomethingWith1(a);
elseFn();
}
else {
elseFn();
}
I don't know in C but in Java you could write this as the following:
void function(int a) {
boolean b1 = condition1(a);
if (b1) {
doSomethingWith1(a);
boolean b2 = condition2(a);
if (b2) {
doSomethingWith2(a);
}
}
if (b1 || b2) {
return;
}
elseFn();
}

What is a readable way to code a nested binary branching logic

I have a few logic that looks like
if(a){
if(b){
if(c){
//do something
}else{
//do something
}
}else{
if(c){
//do something
}else{
//do something
}
}else{
if(b){
if(c){
//do something
}else{
//do something
}
}else{
if(c){
//do something
}else{
//do something
}
}
What is the best way tot implement this into readable logic? I dont want to do some big OOP surgery to make it readable because the do something is just one liner. Solution in C/C++ is appreciated
Since the conditions are boolean, and apparently independent, treat them as bits in a word and switch on them:
#include <cstdio>
#define COMPOSE(a,b,c) ( ((!!(a)) << 2) | ((!!(b))<<1) | (!!(c)) )
int f(int i, int j, int k) {
switch(COMPOSE( i==j, i+j<k, k!=42)) {
case COMPOSE(true, true, true):
printf("yo\n");
break;
case COMPOSE(true, true, false):
printf("ye\n");
break;
case COMPOSE(true, false, true):
printf("ya\n");
break;
}
}
int main () {
f(1,1,1);
}
If all //do something are fundamentally different, you don't have much choice (afaik).
For code style I would prefer
if ( a && b && c )
{
}
else if ( a && b && !c )
{
}
else if ( a && !b && c )
...
This removes the necessity of multiple levels of indentation and makes it clear which condition is actually satisfied.
Side note: obviously a && b && !c can be stated as a && b because !(a && b && c) and the use of else if. I would leave it there anyway because the compiler might optimize it and the code looks cleaner.

Fixing a weird write to file bug in c++

i got this really wierd problem, i'm writing my results to an output file, i use functions A B and C i activate them in that order, the results in the file is printed in a different order, first from A than from C and after that from B. i just can't understand why the results printed in a different order than the activation order. thanx.
the code :
void Manager::AddCommand(Command* com, ofstream &ResultFile)
{
if (com != NULL)
{
if (com->ValidCommand(ResultFile) == true)
commands.push_back(com);
else
delete com;
}
}
bool Command::ValidCommand(ofstream &Result) const
{
if(func<PrintCityCouncilList || func >HireEmployee){
Result << "Command:Failed activating function - invalid function number\n";
return false;}
if ((func == Command::PrintDepartmentEmployees) || (func == Command::PrintDepartmentExpenses) || (func == Command::PrintDepartmentStatistics)){
if(dept<Employee::Engineering ||dept>Employee::Sanitation )
{
Result << "Command:Failed activating function - invalid department number\n";
return false;
}
}
return true;
}
void Manager::ActivateCommand(Command* com, ofstream &Result)
{
if(com != NULL)
{
switch(com->GetFunction())
{
case (Command::PrintCityCouncilList): pcc->PrintCityCouncilDetails(Result);
break;
case (Command::PrintDepartmentEmployees):ActivatePrintDeprtEmployees(com->GetDepartment(), Result);
break;
case (Command::PrintEmployeeSalary):ActivateEmployeeSalary(com->GetId(), Result);
break;
case (Command::PrintDepartmentExpenses):ActivateDeprtExpenses(com->GetDepartment(), Result);
break;
case (Command::PrintCityCouncilExpenses): pcc->AllExpenses (Result);
break;
case (Command::PrintDepartmentStatistics):ActivateDeprtStatistics(com->GetDepartment(), Result);
break;
case (Command::FireEmployee): pcc->RemoveEmployeeFromCC(NULL,com->GetId(),Result);
break;
case (Command::HireEmployee): pcc->AddEmployeeToCC(com->GetId(),com->GetPrivateName(),com->GetSurName(),com->GetDate(),com->GetAddress(),com->GetDepartment(), com->GetStatus(),com->GetSalary(),com->GetPositionPercent(),com->GetPhoneNum(), Result);
break;
default:Result<<"Manager:Failed Activating command - invalid function"<<endl;
break;
}
}
}
void Manager::ActivateCommandsList(ofstream &Result)
{
Command* tmp = NULL;
if (commands.empty() == false)
{
list<Command*>::iterator iter = commands.begin();
while (iter != commands.end())
{
tmp = (Command*)(*iter);
ActivateCommand(tmp,Result);
iter++;
}
}
}
Ok here's the deal, your code maybe having some difficulties if you are running it in visual stdio. You see, it has some bugs when it tries to optimize your code. Turn optimization off. Also flush your stream, that's done when putting endl in cout and \n in printf. There is another possibility and it is Stack-Corruption or Heap-Corruption. Check to see if your dynamic objects are referenced within their boundaries.
As long as you don't send the code, like my friend just said we should consult the crystall ball.
My guess is that if this is homework, you are not having a threading issue. Have you tried to flush the output stream from A, B, and C? That might solve your problem.