f



Multiple commands after if control statement, should I group them or not?

Hi all,

For the following control statement:

  if (condition) statement

If I use multiple commands as the statement, i.e., something like the 
following:

  if (condition)
    statement_1
    statement_2
    ...    

In this case, should I group these statements with {} or not?  I.e., like 
the following:

  if (condition) {
    statement_1
    statement_2
    ...
   }

I mean, if I not group these statements with {}, does awk still execute 
all of these statements only when the condition meets?  If so, the {} is 
useless for this case.

Regards
-- 
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
0
Hongyi
11/18/2016 12:19:15 PM
comp.lang.awk 3450 articles. 0 followers. Post Follow

15 Replies
409 Views

Similar Articles

[PageSpeed] 24

On 11/18/2016 6:19 AM, Hongyi Zhao wrote:
> Hi all,
>
> For the following control statement:
>
>   if (condition) statement
>
> If I use multiple commands as the statement, i.e., something like the
> following:
>
>   if (condition)
>     statement_1
>     statement_2
>     ...
>
> In this case, should I group these statements with {} or not?  I.e., like
> the following:
>
>   if (condition) {
>     statement_1
>     statement_2
>     ...
>    }
>
> I mean, if I not group these statements with {}, does awk still execute
> all of these statements only when the condition meets?  If so, the {} is
> useless for this case.
>
> Regards
>

READ A BOOK!!!!
0
Ed
11/18/2016 12:49:58 PM
On 11/18/2016 6:49 AM, Ed Morton wrote:
> On 11/18/2016 6:19 AM, Hongyi Zhao wrote:
>> Hi all,
>>
>> For the following control statement:
>>
>>   if (condition) statement
>>
>> If I use multiple commands as the statement, i.e., something like the
>> following:
>>
>>   if (condition)
>>     statement_1
>>     statement_2
>>     ...
>>
>> In this case, should I group these statements with {} or not?  I.e., like
>> the following:
>>
>>   if (condition) {
>>     statement_1
>>     statement_2
>>     ...
>>    }
>>
>> I mean, if I not group these statements with {}, does awk still execute
>> all of these statements only when the condition meets?  If so, the {} is
>> useless for this case.
>>
>> Regards
>>
>
> READ A BOOK!!!!

OR TRY IT!!! You could've written a test program faster than typing your question.
0
Ed
11/18/2016 12:51:03 PM
Le vendredi 18 novembre 2016 13:50:05 UTC+1, Ed Morton a =C3=A9crit=C2=A0:
> On 11/18/2016 6:19 AM, Hongyi Zhao wrote:
> > Hi all,
> >
> > For the following control statement:
> >
> >   if (condition) statement
> >
> > If I use multiple commands as the statement, i.e., something like the
> > following:
> >
> >   if (condition)
> >     statement_1
> >     statement_2
> >     ...
> >
> > In this case, should I group these statements with {} or not?  I.e., li=
ke
> > the following:
> >
> >   if (condition) {
> >     statement_1
> >     statement_2
> >     ...
> >    }
> >
> > I mean, if I not group these statements with {}, does awk still execute
> > all of these statements only when the condition meets?  If so, the {} i=
s
> > useless for this case.
> >
> > Regards
> >
>=20
> READ A BOOK!!!!

e. g. this one by Dale Dougherty & Arnold Robbins:
http://docstore.mik.ua/orelly/unix/sedawk/ch08_01.htm
0
Marc
11/18/2016 12:51:57 PM
On 2016-11-18, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> Hi all,
>
> For the following control statement:
>
>   if (condition) statement
>
> If I use multiple commands as the statement, i.e., something like the 
> following:
>
>   if (condition)
>     statement_1
>     statement_2

April 1, 2017 is months away yet.
0
Kaz
11/18/2016 2:41:45 PM
Le vendredi 18 novembre 2016 15:41:48 UTC+1, Kaz Kylheku a =C3=A9crit=C2=A0=
:
> On 2016-11-18, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> > Hi all,
> >
> > For the following control statement:
> >
> >   if (condition) statement
> >
> > If I use multiple commands as the statement, i.e., something like the=
=20
> > following:
> >
> >   if (condition)
> >     statement_1
> >     statement_2
>=20
> April 1, 2017 is months away yet.

I you really want to write this kind of code, use Python:

if (condition):
    statement_1
    statement_2

No braces needed. In my opinion this is much nicer.
0
Marc
11/18/2016 2:52:21 PM
On Fri, 18 Nov 2016 06:49:58 -0600
Ed Morton <mortonspam@gmail.com> wrote:

> READ A BOOK!!!!

Hey, we were all newbies once.  OK a loooooong time ago.  And not all
have english as their native language, nor the norms of usenet explained
to them.  Once anyone has used as many programming languages as we
"experts" have (Fortran II anyone?) then we know how to find these
things out, but IMHO there is no such thing as a stupid question 
-- until it's been repeated three times at least.


It looks to me as if the OP needs the idea of "else" explaining, but
given the neat indenting Python may indeed be the answer.

And when did anyone last find a decent book on any programming language
in a bricks&mortar shoppe?  Even in Silicon Fen.   Only publisher's
books I'll buy without flicking over the pages is O'Reilly.  Oh, I did
buy the Haynes manual for the Vickers VC10 online.  The fantasy ones are
good, the ones you need to keep your motorcycle moving are too much
fantasy.


Mike

0
Mike
11/18/2016 9:08:28 PM
On 2016-11-18, Mike Causer <m.r.causer@goglemail.com> wrote:
> On Fri, 18 Nov 2016 06:49:58 -0600
> Ed Morton <mortonspam@gmail.com> wrote:
>
>> READ A BOOK!!!!
>
> Hey, we were all newbies once. 

Sure, but not in the permanent sense of once ("once and for always").
0
Kaz
11/18/2016 9:14:49 PM
On 11/18/2016 3:08 PM, Mike Causer wrote:
> On Fri, 18 Nov 2016 06:49:58 -0600
> Ed Morton <mortonspam@gmail.com> wrote:
>
>> READ A BOOK!!!!
>
> Hey, we were all newbies once.

Yeah but Hongyi has been a newbie for at least the past 10 years and keeps 
posting completely inane questions that the simplest google search or test code 
would answer for him/her or even just THINKING about it for a few seconds.

	Ed.
0
Ed
11/18/2016 9:31:01 PM
On Fri, 18 Nov 2016 15:31:01 -0600
Ed Morton <mortonspam@gmail.com> wrote:

> > Hey, we were all newbies once.  
> 
> Yeah but Hongyi has been a newbie for at least the past 10 years and
> keeps posting completely inane questions that the simplest google
> search or test code would answer for him/her or even just THINKING
> about it for a few seconds.

Ahh, OK.  


Mike

0
Mike
11/18/2016 9:55:46 PM
On Fri, 18 Nov 2016 06:51:03 -0600, Ed Morton wrote:

> OR TRY IT!!! You could've written a test program faster than typing your
> question.

After some tries, I got the following conclusion:

When not using the {}, only the immediate statement after the condition 
is effected by the condition.  So the following: 

  for (COND1) {
    if (COND2)
      statement 
  }

is equivalent to the following one:

  for (COND1) 
    if (COND2)
      statement

So, using the above latter form can save one pair of `{}' and let the 
code more concise. 

But for the following one:

    for (COND1) 
      if (COND2)
        statement1
        statement2

The statement2 will only be executed after the for loop and the if 
condition on the statement1. 

Regards
-- 
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
0
Hongyi
11/19/2016 2:10:47 AM
On Fri, 18 Nov 2016 06:51:03 -0600, Ed Morton wrote:

> OR TRY IT!!! You could've written a test program faster than typing your
> question.

After some tries, I got the following conclusion:

When not using the {}, only the immediate statement after the condition 
is effected by the condition.  So the following: 

  for (COND1) {
    if (COND2)
      statement 
  }

is equivalent to the following one:

  for (COND1) 
    if (COND2)
      statement

So, using the above latter form can save one pair of `{}' and let the 
code more concise. 

But for the following one:

    for (COND1) 
      if (COND2)
        statement1
        statement2

The statement2 will only be executed after the for loop and the if 
condition on the statement1. 

Regards
-- 
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
0
Hongyi
11/19/2016 2:12:21 AM
On 2016-11-19, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> On Fri, 18 Nov 2016 06:51:03 -0600, Ed Morton wrote:
>
>> OR TRY IT!!! You could've written a test program faster than typing your
>> question.
>
> After some tries, I got the following conclusion:
>
> When not using the {}, only the immediate statement after the condition 
> is effected by the condition.

Your conclusion contains superfluous text: "Whennot using the {}, ".

When using { }, it is still the case that one statement after the
condition is included in the if statement.

A brace-enclosed block is a single compound statement.

Your experiment and conclusion also didn't pick up on the optional else:


  if (cond) S1 else S2

now two statements are affected, in the opposite way.
0
Kaz
11/19/2016 3:08:27 PM
On 2016-11-18, Ed Morton <mortonspam@gmail.com> wrote:
> On 11/18/2016 6:49 AM, Ed Morton wrote:
>> On 11/18/2016 6:19 AM, Hongyi Zhao wrote:
>>> Hi all,
>>>
>>> For the following control statement:
>>>
>>>   if (condition) statement
>>>
>>> If I use multiple commands as the statement, i.e., something like the
>>> following:
>>>
>>>   if (condition)
>>>     statement_1
>>>     statement_2
>>>     ...
>>>
>>> In this case, should I group these statements with {} or not?  I.e., like
>>> the following:
>>>
>>>   if (condition) {
>>>     statement_1
>>>     statement_2
>>>     ...
>>>    }
>>>
>>> I mean, if I not group these statements with {}, does awk still execute
>>> all of these statements only when the condition meets?  If so, the {} is
>>> useless for this case.
>>>
>>> Regards
>>>
>>
>> READ A BOOK!!!!
>
> OR TRY IT!!! You could've written a test program faster than typing your question.

THINK FIRST

Why would if affect more than one statement?

Suppose we have this body of code:

   statement1
   statement2
   statement3
   statement4
   ...
   statement27
   ...
   statement99

Suppose we insert:

   statement1
   statement2
   if (condition)
   statement3
   statement4
   ...
   statement27
   ...
   statement99

What sense would there be in subjecting *all* statements from 3 to 99
to the condition? It would be very inconvenient to use if. Everyone
would talk about this and break up their programs to guard against it.

What sense would there be in subjecting exactly two --- statement3 and
statement4 --- to the condition? That would violate the "none, one or
else all" design rule.

Read the spec and think; then read books and exeperiment.

If you experiment, check the spec that you aren't actually hitting
something nonportable or outright undefined.

Nobody should be discouraged from experimenting, but experiments not
guided by specifications can reach bad conclusions.
0
Kaz
11/19/2016 3:21:16 PM
On Sat, 19 Nov 2016 15:08:27 +0000, Kaz Kylheku wrote:

> Your experiment and conclusion also didn't pick up on the optional else:
> 
> 
>   if (cond) S1 else S2

Thanks for your reply, try on this type of cond-statement control flow, 
give me the following understanding:

[1] This will be ok:

 if (cond) 
   S1 
 else
   S2
 
[2] This will be syntactically error without using {}: 

if (cond) 
   S1
   S2 
else
   S3

and must be used as follows:

if (cond) {
   S1
   S2 
}
else
   S3

[3] Without the else case, your notes and my previous conclusion holds.

Regards

> 
> now two statements are affected, in the opposite way.





-- 
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
0
Hongyi
11/20/2016 1:23:10 AM
On 2016-11-20, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> On Sat, 19 Nov 2016 15:08:27 +0000, Kaz Kylheku wrote:
>
>> Your experiment and conclusion also didn't pick up on the optional else:
>> 
>> 
>>   if (cond) S1 else S2
>
> Thanks for your reply, try on this type of cond-statement control flow, 
> give me the following understanding:
>
> [1] This will be ok:
>
>  if (cond) 
>    S1 
>  else
>    S2
>  
> [2] This will be syntactically error without using {}: 
>
> if (cond) 
>    S1
>    S2 
> else
>    S3

Yes; this is wrong because you have "if (cond) S1" which is a complete
if statement. This is then followed by "S2 else S3". S2 is okay,
but then the "else" is a dangling else; it doesn't refer to
any previous if.

> and must be used as follows:
>
> if (cond) {
>    S1
>    S2 
> }
> else
>    S3
>
> [3] Without the else case, your notes and my previous conclusion holds.

Your conclusion doesn't hold literally because if doesn't require { }.

Rather, S1 and S2 require a surrounding {  }, if they are to
act as a single statement for purposes such as being the consequent*
clause of an if statement.

--
* Latin-derived terminology: "if (antecedent) consequent else alternative"
0
Kaz
11/20/2016 2:35:36 AM
Reply: