Reader small image

You're reading from  Learning AWK Programming

Product typeBook
Published inMar 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788391030
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Shiwang Kalkhanda
Shiwang Kalkhanda
author image
Shiwang Kalkhanda

Shiwang Kalkhanda (RHCA, RHCSS, MCSE) is a Linux geek and consultant with expertise in the automation of infrastructure deployment and management. He has more than 10 years' experience in security, system, and network administration, and training on open source tech. For most of his automation work, he uses Shell Scripting, Python, and Go. He holds a master's and a bachelor's degree in computer applications. He enjoys traveling and spending time with his children. He is also the author of a book on text processing utilities in Unix-like environments, Learning Awk Programming.
Read more about Shiwang Kalkhanda

Right arrow

AWK Control Flow Statements

This chapter covers the control structures of the AWK programming language. This includes the different types of conditional and looping statements, such as if...else, do...while, switch...case, and so on available in AWK . The syntax for conditional and looping constructs is very similar to that of C programming language. If you are already familiar with C, then you will find it quite easy, and those who are new to programming will also find it simple and easy to understand.

In this chapter, we will cover the following topics:

  • Different conditional constructs
  • if statement usage
  • switch statement usage
  • The while loop construct
  • The for loop construct
  • Different statements affecting flow controls
  • Break usage
  • Next usage
  • The exit statement

Conditional statements

Conditional statements such as if and switch are used to test expressions and control the flow of execution in AWK programs. All control statements start with special keywords such as if and switch to differentiate them from simple expressions. Within one conditional statement block, we can have other multiple statements separated by braces, newlines, or semicolons. Such conditional statements are known as compound statements.

The if statement

if is a conditional statement used to control the flow of a program. AWK supports three types of if statements:

  • if
  • if...else
  • if...elseif...elseif...

if

...

Looping statement

A loop is a conditional construct that allows us to perform one or more actions again and again till the condition is true as specified in expression. In AWK we can specify a loop using a while, do or for statement.

The while loop

The while is the simplest looping statement in AWK. A while statement has a condition and a body. The body contains the action statements that are executed till the condition is true. The condition could be a logical condition or conditional-expression that evaluates to true. First while statement tests the condition; if the condition evaluates to true, then it executes the statements specified in the body. Once all the statements specified in the body have been executed, the condition...

Statements affecting flow control

Till now we have seen different conditional constructs and loop construct such as if...else, while, for, switch and do statements. Now, we will study break, continue and exit statements which are used to alter the normal flow of program. A loop performs a set of repetitive tasks until the conditional-expression becomes false, but sometimes it is desirable to skip some action statements inside the loop or terminate the loop immediately without checking the conditional-expression. In such cases, break and continue statements are used.

Break usage

The break statement is used to terminate the innermost while, do...while, or for loop that encloses it. The break statement is also used to break out...

Summary

In this chapter, we learned about the different types of control structures that are used in the AWK programming language. We learned types of conditional statements such as if, if...else, if...else...if, and switch...case. Then we learned about different looping constructs like while, do...while, and for loops. Finally, we saw the usage of different flow control statements such as break, continue, exit, and next.

In next chapter, we will learn about the different types of built-in and user-defined functions available in AWK.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning AWK Programming
Published in: Mar 2018Publisher: PacktISBN-13: 9781788391030
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Shiwang Kalkhanda

Shiwang Kalkhanda (RHCA, RHCSS, MCSE) is a Linux geek and consultant with expertise in the automation of infrastructure deployment and management. He has more than 10 years' experience in security, system, and network administration, and training on open source tech. For most of his automation work, he uses Shell Scripting, Python, and Go. He holds a master's and a bachelor's degree in computer applications. He enjoys traveling and spending time with his children. He is also the author of a book on text processing utilities in Unix-like environments, Learning Awk Programming.
Read more about Shiwang Kalkhanda