Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

41 views
Skip to first unread message

Hongyi Zhao

unread,
Nov 18, 2016, 7:19:16 AM11/18/16
to
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 :.

Ed Morton

unread,
Nov 18, 2016, 7:50:05 AM11/18/16
to
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!!!!

Ed Morton

unread,
Nov 18, 2016, 7:51:09 AM11/18/16
to
OR TRY IT!!! You could've written a test program faster than typing your question.

Marc de Bourget

unread,
Nov 18, 2016, 7:51:58 AM11/18/16
to
e. g. this one by Dale Dougherty & Arnold Robbins:
http://docstore.mik.ua/orelly/unix/sedawk/ch08_01.htm

Kaz Kylheku

unread,
Nov 18, 2016, 9:41:48 AM11/18/16
to
On 2016-11-18, Hongyi Zhao <hongy...@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.

Marc de Bourget

unread,
Nov 18, 2016, 9:52:23 AM11/18/16
to
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.

Mike Causer

unread,
Nov 18, 2016, 4:08:47 PM11/18/16
to
On Fri, 18 Nov 2016 06:49:58 -0600
Ed Morton <morto...@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

Kaz Kylheku

unread,
Nov 18, 2016, 4:14:52 PM11/18/16
to
On 2016-11-18, Mike Causer <m.r.c...@goglemail.com> wrote:
> On Fri, 18 Nov 2016 06:49:58 -0600
> Ed Morton <morto...@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").

Ed Morton

unread,
Nov 18, 2016, 4:31:09 PM11/18/16
to
On 11/18/2016 3:08 PM, Mike Causer wrote:
> On Fri, 18 Nov 2016 06:49:58 -0600
> Ed Morton <morto...@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.

Mike Causer

unread,
Nov 18, 2016, 4:55:48 PM11/18/16
to
On Fri, 18 Nov 2016 15:31:01 -0600
Ed Morton <morto...@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

Hongyi Zhao

unread,
Nov 18, 2016, 9:12:22 PM11/18/16
to
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.

Hongyi Zhao

unread,
Nov 18, 2016, 9:30:03 PM11/18/16
to
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.

Kaz Kylheku

unread,
Nov 19, 2016, 10:08:31 AM11/19/16
to
On 2016-11-19, Hongyi Zhao <hongy...@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.

Kaz Kylheku

unread,
Nov 19, 2016, 10:21:19 AM11/19/16
to
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.

Hongyi Zhao

unread,
Nov 19, 2016, 8:23:11 PM11/19/16
to
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.





Kaz Kylheku

unread,
Nov 19, 2016, 9:35:40 PM11/19/16
to
On 2016-11-20, Hongyi Zhao <hongy...@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 new messages