Q1 Graded 3 clarification

188 views
Skip to first unread message

Ashiesh Kumar J

unread,
Nov 2, 2020, 3:37:55 PM11/2/20
to Discussion forum for Computational Thinking
I have query for Q 1 Graded 3
Why 
if (X.LetterCount > Avg and X.PartOfSpeech == “Noun”) {
Count = Count + 1
Why not
if (X.PartOfSpeech == "Noun" and X.LetterCount > Avg) {
Count  = Count + 1
(Which was not in the option).
 In week 3 tutorial, same sort of question, followed by same condition (later one)
This is for my clarification 

Regards

satya takkellapati

unread,
Nov 2, 2020, 9:12:11 PM11/2/20
to Discussion forum for Computational Thinking, Ashiesh Kumar J
they are both the same condition order doesn't matter here as long as the condition is satisfied it will enter the loop 
As this is an "and" statement both of them should be satisfied If it was as an "or" either of them should be satisfied
It's like a+b and b+a no difference, right?

Computational Thinking Support 2

unread,
Nov 3, 2020, 1:15:44 AM11/3/20
to Discussion forum for Computational Thinking, saty...@gmail.com, Ashiesh Kumar J
Hi,
The above explanation is correct.

Regards,
Deepak
IITM Online Degree

Ashiesh Kumar J

unread,
Nov 3, 2020, 2:31:26 AM11/3/20
to Discussion forum for Computational Thinking, ct-su...@onlinedegree.iitm.ac.in, saty...@gmail.com, Ashiesh Kumar J
Thank you for the answer. Yes, got it and even I selected the same choice as my first preference. 

But my little concern was none of the video lectures, tutorials or CT sample book explained that we can use these two statements either way if it is" and"

And I changed the option

So in this condition what will be the procedure :

  • Means, it will pick all words which are greater than average and then check for Part of Speech = Noun
or
  • It will check Part of Speech = Noun and then will check whether that word is greater than average 
or 
  • Irrespective of any of the above two - it will follow the shortest route because of "and'

Please provide some basics on this. will be helpful.

Thanks 
Message has been deleted

Ashiesh Kumar J

unread,
Nov 3, 2020, 9:17:52 AM11/3/20
to Velan Chandrasekar, Discussion forum for Computational Thinking, ct-su...@onlinedegree.iitm.ac.in, saty...@gmail.com
Thanks for the clarification. Will keep in mind for the future. 


On Tue, 3 Nov, 2020, 7:40 pm Velan Chandrasekar, <velan.chan...@gmail.com> wrote:
The objective, as per the question, is to check whether the word is a noun AND whether it is longer than the average word length.
That means, both the conditions are a must to be satisfied.

So, irrespective of what the 1st condition is, the code will/has to check the 2nd condition anyway.
So, it doesn't matter which one is checked first, here.

Your concern will be valid, only if it is a OR condition - in which, if the 1st condition fails, it won't even check the 2nd condition (while, the actual intention is that it checks the 2nd condition also, despite the result of 1st condition - because the test condition must pass even if any one of it is TRUE).
In that case, it would matter whether we check the Count condition first, or the Partofspeech.

Velan Chandrasekar

unread,
Nov 3, 2020, 12:54:40 PM11/3/20
to Discussion forum for Computational Thinking, Ashiesh Kumar J, Discussion forum for Computational Thinking, ct-su...@onlinedegree.iitm.ac.in, saty...@gmail.com, Velan Chandrasekar
Sorry about that... While the point I was making was correct, the explanation on how the short-circuit operators work was incorrect.
Please ignore it completely.

The objective, as per the question, is to check whether the word is a noun AND whether it is longer than the average word length.
That means, both the conditions are a must, to be satisfied.

So, irrespective of what the 1st condition is, the output will be correct.
i.e.
If the 1st condition is to check for noun, it will stop immediately, if the word is not a noun - which fulfills the objective.
If the 1st condition is to check if the word length > average word length, it'll stop immediately, if word length is smaller - which again fulfills the objective.
If either one of them is TRUE, it will still check for the other condition - which also fulfills the objective.

In short, it'll pass only if both are TRUE. And, it'll fail if either of them if FALSE. In either case, the objective is met.
So, it doesn't matter which one is checked first, here.

Your concern will not be valid for any scenario - unless, the conditions are dependent on each other & you specify those conditions itself incorrectly.

For example, take the scenario where a student's academic status is checked - as below.
if(student != null && student.marks>=40) {
     Print "PASS";
}

The code checks if the student object is created first & if yes, then it check the marks. If the student object is null (i.e. not created yet), then it won't check the 2nd condition (due to short-circuit operator). This is the expected behavior and hence it is fine.

And, it will work the same way, even if you interchange the conditions (even though, it will be against common sense to interchange them).

if(student.marks>=40 && student != null) {
     Print "PASS";
}

Only, this time, it will throw an exception saying that the student object is null i.e. not created yet.
Even in this case, the error is not about the short-circuit operator or about how/where the conditions are placed. It's only about the programming logic.

So, the placement of a condition helps/affects only to improve performance, by reducing the execution time & CPU utilization.
It won't affect the program & thereby, the output - unless your logic is incorrect. 

Side Note:
Unlike the AND operator - which passes only if both the conditions are TRUE, OR operator checks whether at least one condition is TRUE.
Thus, in case of a OR operator, it'll check for the 2nd condition, even if the 1st condition is FALSE.
So, if the 1st condition is TRUE, it won't check the 2nd condition (we know, it doesn't have to).

Ashiesh Kumar J

unread,
Nov 9, 2020, 2:23:57 AM11/9/20
to Velan Chandrasekar, Discussion forum for Computational Thinking, ct-su...@onlinedegree.iitm.ac.in, saty...@gmail.com
Hello
My course portal has not been working since last night. Any update on this. Tried calling support team but no answer. Also mailed to support but no reply received. 
Reply all
Reply to author
Forward
0 new messages