Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,744 members, 7,817,056 topics. Date: Saturday, 04 May 2024 at 01:36 AM

ATTENTION: Programming Logic Comparison - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / ATTENTION: Programming Logic Comparison (3210 Views)

Help Me Keep My Brain In My Skull! Solve This Logic! / Simple Logic!think U Can Crack This? / The Door-Goat-Car Logic Problem (2) (3) (4)

(1) (2) (Reply) (Go Down)

ATTENTION: Programming Logic Comparison by romme2u: 2:19pm On Nov 13, 2014
NOTICE: pls this post is for experienced programmers only. if u don't understand what it means , just sit back and enjoy the explanation from our senior friends.

........AND if u know, please offer ur explanation. even if u are a newbie, u can benefit from this.


/************************************/
what is the difference between these two statements.

=================================

if(condition && condition){

}

----------------------------

if(condition){
if(condition){}
}

==================================
Re: ATTENTION: Programming Logic Comparison by Skykid1208(m): 4:19pm On Nov 13, 2014
Whatching And ready to learn
Re: ATTENTION: Programming Logic Comparison by romme2u: 5:36pm On Nov 13, 2014
pls all the programmers in the house should contribute for the benefit of all. it is very necessary
Re: ATTENTION: Programming Logic Comparison by Nobody: 5:41pm On Nov 13, 2014
The way I see both conditions, they are different scenarios entirely... depending on the situation shaa

The first condition implies that if A and B are satisfied, do something. Else, do something else.

For instance, if my username is bruceleah and my password is xoxox, log me in into nairaland else prompt a mismatch.

The second condition is kinda different. It implies that if the first condition is satisfied, then check for the second condition and do something.

For instance, if my username is bruceleah, then check if my pasword xoxox. If true, log me into the programming section else if my password is yyy log me into the joke section.

Note that at both scenarios, the first condition is satisfied but the second is kinda handled differently

My opinion thoo.

1 Like 1 Share

Re: ATTENTION: Programming Logic Comparison by Nobody: 6:00pm On Nov 13, 2014
Another scenario... Let's say we want to know if a username actually exist in our database, and check if the password matches the specified user name

The first example wld just throw a mismatch error

But the with d example, we could actually check if the username exists first, the throw an error if username does not exist. And den we check if the password matches the specified username. If not true, it throws a mismatch error.

1 Like

Re: ATTENTION: Programming Logic Comparison by Cybergenius(m): 6:02pm On Nov 13, 2014
No difference, they are both the same
Re: ATTENTION: Programming Logic Comparison by Nobody: 6:07pm On Nov 13, 2014
Cybergenius:
No difference, they are both the same

Boss cybergenius, maybe you look at it from another point of view
Re: ATTENTION: Programming Logic Comparison by romme2u: 6:08pm On Nov 13, 2014
bruceleah:
Another scenario... Let's say we want to know if a username actually exist in our database, and check if the password matches the specified user name

The first example wld just throw a mismatch error

But the with d example, we could actually check if the username exists first, the throw an error if username does not exist. And den we check if the password matches the specified username. If not true, it throws a mismatch error.

i love your assertion by bringing two use case illustrating each scenario perfectly. in fact if it was a competition i may likely give it to you wink cheesy. but lets keep this open for further thoughts and opinions.
Re: ATTENTION: Programming Logic Comparison by blueyedgeek(m): 6:08pm On Nov 13, 2014
There really is no difference between both as they are basically different ways to do the same thing which is check to make sure that two expressions in the condition are met before any code statement is executed. The first method is a more compact form.

Just like we have the while and for loop, both are used for the same thing but the for loop allows you to keep all your code expressions in one statement making it a more compact form.

An example.

if(9 > 7 && 7 < 9){
console.log('true');
}


Will give the same result as:

if(9 > 7){
if(7 < 9){
console.log('true');
}
}

1 Like

Re: ATTENTION: Programming Logic Comparison by romme2u: 6:11pm On Nov 13, 2014
Cybergenius:
No difference, they are both the same

like bruceleah said. i think there is a very subtle difference mostly in different scenarios and application. wink
Re: ATTENTION: Programming Logic Comparison by Nobody: 6:15pm On Nov 13, 2014
blueyedgeek:
There really is no difference between both as they are basically different ways to do the same thing which is check to make sure that two expressions in the condition are met before any code statement is executed. The first method is a more compact form.

Just like we have the while and for loop, both are used for the same thing but the for loop allows you to keep all your code expressions in one statement making it a more compact form.

An example.

if(9 > 7 && 7 < 9){
console.log('true');
}


Will give the same result as:

if(9 > 7){
if(7 < 9){
console.log('true');
}
}

I quite agree with you. But what if we wanna check if 7 is less than 9 but equal to 8? The first example can't handle that but the second will

For instance:

f(9 > 7){
if(7 < 9){
console.log('true');
} else {
Console.write("8"wink;
}
}

1 Like

Re: ATTENTION: Programming Logic Comparison by Nobody: 6:17pm On Nov 13, 2014
romme2u:


i love you assertion by bring two use case illustrating each scenario perfectly. in fact if it was a competition i may likely give it to you wink cheesy. but lets keep this open for further thoughts and opinions.

Thanks boss.
Re: ATTENTION: Programming Logic Comparison by Nobody: 6:17pm On Nov 13, 2014
romme2u:


i love you assertion by bring two use case illustrating each scenario perfectly. in fact if it was a competition i may likely give it to you wink cheesy. but lets keep this open for further thoughts and opinions.

Thanks boss :-D
Re: ATTENTION: Programming Logic Comparison by Nobody: 6:17pm On Nov 13, 2014
romme2u:


i love you assertion by bring two use case illustrating each scenario perfectly. in fact if it was a competition i may likely give it to you wink cheesy. but lets keep this open for further thoughts and opinions.

Thanks boss :-)
Re: ATTENTION: Programming Logic Comparison by romme2u: 6:17pm On Nov 13, 2014
bruceleah has a higher insight which is mainly in their application. in between i am learning and enjoying it wink

more suggestions please
Re: ATTENTION: Programming Logic Comparison by Nobody: 6:20pm On Nov 13, 2014
My network is kinda bad... I never intended to reply three times. Programmers abeg no vex
Re: ATTENTION: Programming Logic Comparison by blueyedgeek(m): 6:24pm On Nov 13, 2014
bruceleah:


I quite agree with you. But what if we wanna check if 7 is less than 9 but equal to 8? The first example can't handle that but the second will

For instance:

f(9 > 7){
if(7 < 9){
console.log('true');
} else {
Console.write("8"wink;
}
}
I don't quite get you bro, what do you mean by checking if 7 is less than 9 but equal to 8?

By the way, If we add the else block to the first method, you'll get the same result anyway.
Re: ATTENTION: Programming Logic Comparison by Nobody: 6:26pm On Nov 13, 2014
blueyedgeek:
I don't quite get you bro, what do you mean by checking if 7 is less than 9 but equal to 8?

By the way, If we add the else block to the first method, you'll get the same result anyway.

Sure boss, it would, I agree. But this might not be applicable to other scenarios like the first one I painted.
Re: ATTENTION: Programming Logic Comparison by Nobody: 6:51pm On Nov 13, 2014
blueyedgeek:

There really is no difference between both as they are basically different ways to do the same thing which is check to make sure that two expressions in the condition are met before any code statement is executed. The first method is a more compact form.

Just like we have the while and for loop, both are used for the same thing but the for loop allows you to keep all your code expressions in one statement making it a more compact form.

An example.

if(9 > 7 && 7 < 9){
console.log('true');
}


Will give the same result as:

if(9 > 7){
if(7 < 9){
console.log('true');
}
}


Yes sir, and both snippets are practically useless as they have no logical implication. In other word, the presence or absence of the control structures will not in any way alter the flow of logic since they will both evaluate to TRUE.

Question, though both structures give the same result, is anyone better than the other? If so, which one?

...... or is it subject to end goal?

Regards.

1 Like

Re: ATTENTION: Programming Logic Comparison by blueyedgeek(m): 7:00pm On Nov 13, 2014
bruceleah:


Sure boss, it would, I agree. But this might not be applicable to other scenarios like the first one I painted.
If you had passed a statement to be executed when the condition in the first if block is met and another statement to be executed if the second condition is met then it becomes something else.

Example:

if(9 > 7){
console.log('do something');
if(7 > 22){
console.log('do something else');
}
}


If this code is run, the statement in the first if block will be printed to the console and the second will be ignored. In this context, it does not do the same thing as using the
if(condition && condition){
//do something
}

but when you begin to nest multiple if statements and only one expression will be executed then they become the same thing. It doesn't matter how many nesting levels you go as far as all the if statements are used for one expression then it is the same thing as using the
if(condition && condition){
//do something
}

Example:


if(5 > 4){
if(6 > 5){
if(7 > 6){
if(8 > 7){
if(9 > 8 ) {
if(10 > 9){
console.log("This will only print if all conditions in the if statement are true" );
}
}
}
}
}
}


Is the same thing as:

Example:


if(5 > 4 && 6 > 5 && 7 > 6 && 8 > 7 && 9 > 8 && 10 > 9){
console.log("This will only print if all conditions in the if statement are true" );
}


Run both code snippets and you will get the exact same result. The second method is just a more compact form and more readable. There really is no difference.
Re: ATTENTION: Programming Logic Comparison by blueyedgeek(m): 7:07pm On Nov 13, 2014
GozieDiora:


Yes sir, and both snippets are practically useless as they have no logical implication. In other word, the presence or absence of the control structures will not in any way alter the flow of logic since they will both evaluate to TRUE.

Question, though both structures give the same result, is anyone better than the other? If so, which one?

...... or is it subject to end goal?

Regards.

In my opinion, none is better than the other. They are both identical in functionality and performance but I would favour the '&&' syntax over nesting multiple if statements since it is more readable.

welcome back sir.
Re: ATTENTION: Programming Logic Comparison by Nobody: 7:12pm On Nov 13, 2014
blueyedgeek:
If you had passed a statement to be executed when the condition in the first if block is met and another statement to be executed if the second condition is met then it becomes something else.

Example:

if(9 > 7){
console.log('do something');
if(7 > 22){
console.log('do something else');
}
}


If this code is run, the statement in the first if block will be printed to the console and the second will be ignored. In this context, it does not do the same thing as using the
if(condition && condition){
//do something
}

but when you begin to nest multiple if statements and only one expression will be executed then they become the same thing. It doesn't matter how many nesting levels you go as far as all the if statements are used for one expression then it is the same thing as using the
if(condition && condition){
//do something
}

Example:


if(5 > 4){
if(6 > 5){
if(7 > 6){
if(8 > 7){
if(9 > 8 ) {
if(10 > 9){
console.log("This will only print if all conditions in the if statement are true" );
}
}
}
}
}
}


Is the same thing as:

Example:


if(5 > 4 && 6 > 5 && 7 > 6 && 8 > 7 && 9 > 8 && 10 > 9){
console.log("This will only print if all conditions in the if statement are true" );
}


Run both code snippets and you will get the exact same result. The second method is just a more compact form and more readable. There really is no difference.

I quite agree with you boss. Judging with this example, there's no difference. How about u explain this.

I want to check if a username actually exist in our database, and check if the password matches the specified user name.

Note: I want to let the user know if d specified user name exists in my db, I just don't want to throw a mismatch error.

I know it's not advisable as a programmer, let's just assume I wanna implement this. How wld u go about it?
Re: ATTENTION: Programming Logic Comparison by blueyedgeek(m): 7:51pm On Nov 13, 2014
bruceleah:


I quite agree with you boss. Judging with this example, there's no difference. How about u explain this.

I want to check if a username actually exist in our database, and check if the password matches the specified user name.

Note: I want to let the user know if d specified user name exists in my db, I just don't want to throw a mismatch error.

I know it's not advisable as a programmer, let's just assume I wanna implement this. How wld u go about it?

pseudo code

If username matches database && password matches as well --
'Successful log in''
Else
Throw error
'Username or password doesn't match database'
Re: ATTENTION: Programming Logic Comparison by Nobody: 8:01pm On Nov 13, 2014
blueyedgeek:
pseudo code

If username matches database && password matches as well --
'Successful log in''
Else
Throw error
'Username or password doesn't match database'

Boss, I actually want to let the user know if the specified username exists in our db, not just match username and password (throw a username does not match database)

I also want to throw an error like "user does not exist in our database"

First check if user exist in db before matching username and password.

How would you go about it?

1 Like

Re: ATTENTION: Programming Logic Comparison by Nobody: 8:15pm On Nov 13, 2014
You guys have done justice to the topic kudos.

@bruceleah was correct all the way.

Both conditions look very similar If u r considering 2 conditions with no additional condition. But if u have multiple and complex conditions, they don't.

Let me add, the problem @ hand will determine which scenario to use.

The first 1 works best if I want to enter the block IF and only IF all the conditions are met.

The 2nd scenario is more of a step, entering into the the innermost block is only guaranteed if the outer block is true.

@bruceleah nice job. @op, nice argument.

1 Like

Re: ATTENTION: Programming Logic Comparison by Nobody: 8:44pm On Nov 13, 2014
blueyedgeek:

In my opinion, none is better than the other. They are both identical in functionality and performance but I would favour the '&&' syntax over nesting multiple if statements since it is more readable.

welcome back sir.

Well, from the stand point of computer programming purity and code optimisation, one is actually better.

Here is the gist:

In the first scenario (I.e. using the logical operator), compilers equiped with code optimisation functionality (like Object Pascal etc) take advantage of this structure to reduce total execution time by handling only one side of the operator in the best case scenario. In other words, the second side is executed if and only if the first part evaluates to be false (for logical OR). This goes a long way to increase speed and reduce run time memory consumption. However, this benefit can only be remarkable in data crunching programs like large file processing, batch processing especially in ERP's etc. IDE's like Netbeans for Java language, Morph X for X++ etc. which show approximate execution time would show this for very large amount of data.

In the second, still on logical OR, provided the first condition is true, the second must run.

So if OR is the intended logic, it would pay to use the first structure for code optimisation.

2 Likes

Re: ATTENTION: Programming Logic Comparison by blueyedgeek(m): 9:38pm On Nov 13, 2014
GozieDiora:


Well, from the stand point of computer programming purity and code optimisation, one is actually better.

Here is the gist:

In the first scenario (I.e. using the logical operator), compilers equiped with code optimisation functionality (like Object Pascal etc) take advantage of this structure to reduce total execution time by handling only one side of the operator in the best case scenario. In other words, the second side is executed if and only if the first part evaluates to be false (for logical OR). This goes a long way to increase speed and reduce run time memory consumption. However, this benefit can only be remarkable in data crunching programs like large file processing, batch processing especially in ERP's etc. IDE's like Netbeans for Java language, Morph X for X++ etc. which show approximate execution time would show this for very large amount of data.

In the second, still on logical OR, provided the first condition is true, the second must run.

So if OR is the intended logic, it would pay to use the first structure for code optimisation.

learnt from this post.

1 Like

Re: ATTENTION: Programming Logic Comparison by blueyedgeek(m): 9:51pm On Nov 13, 2014
bruceleah:


Boss, I actually want to let the user know if the specified username exists in our db, not just match username and password (throw a username does not match database)

I also want to throw an error like "user does not exist in our database"

First check if user exist in db before matching username and password.

How would you go about it?
! == does not

if user && password match database
'Success'
Else if user !match && password match
'User does not match database'
Else if user match && password !match
'Incorrect password'
Else
'Incorrect user and password'
Re: ATTENTION: Programming Logic Comparison by Nobody: 10:04pm On Nov 13, 2014
blueyedgeek:

! == does not

if user && password match database
'Success'
Else if user !match && password match
'User does not match database'
Else if user match && password !match
'Incorrect password'
Else
'Incorrect user and password'

I don't seem to understand line 3 of ur code.

What if this logic was simplified:

If (validUser(user)) {
If (validPword(user,pword)) {
// Do something
} else {
"Mismatch";
}
} else {
"No user";
}

Instead of

If (validUser(user) && validPword(user,pword)) {
//Do something
} else {
"Mismatch"
}

Note: the goal is to prompt user if user name does not exist and if there's a mismatch.
Re: ATTENTION: Programming Logic Comparison by Nobody: 10:19pm On Nov 13, 2014
GozieDiora:


Well, from the stand point of computer programming purity and code optimisation, one is actually better.

Here is the gist:

In the first scenario (I.e. using the logical operator), compilers equiped with code optimisation functionality (like Object Pascal etc) take advantage of this structure to reduce total execution time by handling only one side of the operator in the best case scenario. In other words, the second side is executed if and only if the first part evaluates to be false (for logical OR). This goes a long way to increase speed and reduce run time memory consumption. However, this benefit can only be remarkable in data crunching programs like large file processing, batch processing especially in ERP's etc. IDE's like Netbeans for Java language, Morph X for X++ etc. which show approximate execution time would show this for very large amount of data.

In the second, still on logical OR, provided the first condition is true, the second must run.

So if OR is the intended logic, it would pay to use the first structure for code optimisation.


Great one boss, nice one.

I agree boss. Talking about code optimisation, remember that code would first be parsed, and den the semantics would be understood before it could be optimised.

My point: depending on the goal or the logic being used, result of parsing could be thesame and different.

Maybe be we try out different examples. We act as the compiler and try to parse our statements.

We'll see that in some cases we could generate the same parse tree for both conditions and in some, we won't.

So, both scenarios are different. in some cases they could look the same but they are ACTUALLY NOT (we could only figure this out acting as a compiler).

Just like boss GozieDiora has rightfully said, when writing codes, we need to consider the compilation & run time before implementing our logic.
Re: ATTENTION: Programming Logic Comparison by Nobody: 10:31pm On Nov 13, 2014
bruceleah:


I don't seem to understand line 3 of ur code.

What if this logic was simplified:

...............
Note: the goal is to prompt user if user name does not exist and if there's a mismatch.

Though I prefer your approach, you seem to be forgetting we are dealing with pseudo codes here not programming languages. Functions are not to be imagined.

That aside, I think the goal is to prompt the user with his user name in case of password mismatch. In other words, if the user does not exist at all, the system simply tells the user but if the user name exists, the system should call the user by his name and ask if he forgot his password.

Just a slight modification of your code can achieve this.
Re: ATTENTION: Programming Logic Comparison by Nobody: 10:41pm On Nov 13, 2014
bruceleah:


Great one boss, nice one.

I agree boss. Talking about code optimisation, remember that code would first be parsed, and den the semantics would be understood before it could be optimised.

My point: depending on the goal or the logic being used, result of parsing could be thesame and different.

Maybe be we try out different examples. We act as the compiler and try to parse our statements.

We'll see that in some cases we could generate the same parse tree for both conditions and in some, we won't.

So, both scenarios are different. in some cases they could look the same but they are ACTUALLY NOT (we could only figure this out acting as a compiler).

Just like boss GozieDiora has rightfully said, when writing codes, we need to consider the compilation & run time before implementing our logic.

Yeah, bro.

Let's not bother going into parsing/tokenisation (Backus Naur/ type 2 grammar) or semantic trees (Finit State Automata/ type 3 grammar). I'm sure a good number of peeps here won't flow along with us.

This is what differentiates a computer scientist from other computer programmers.

Lol. ... just kidding.

(1) (2) (Reply)

I Just Wrote What Might Be The Worst System Ever Programmed In This Country / How To Program A PHP SMS Gateway / How To Choose A Programming Language

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 78
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.