Get the App
SLTechnology News&Howtos  ›  Development  › 

What's the difference between conditional statements and loop statements?

Shulou Source: shulou.com Published: 2022-06-03 05:32:28 09月16日 Update

This article shares with you what is the difference between conditional statements and circular statements. Xiaobian thinks it is quite practical, so share it with you to learn. I hope you can gain something after reading this article. Let's not say much. Let's take a look at it together with Xiaobian.

conditional statement IF statement

IF conditional statement syntax format is as follows:

if(condition)

action

You can also use braces to perform a set of operations:

if(condition)

{

action-1

action-1

.

.

action-n

}

The following examples are used to determine whether a number is odd or even:

$awk'BEGIN{num=10;if(num%2==0)printf"%d is even\n",num}'

The output is:

10 is an even number.

IF-ELSE statement

IF-ELSE conditional statement syntax format is as follows:

if(condition)

action-1

else

action-2

Only action-1 is required if condition is true, otherwise action-2 is executed.

$awk'BEGIN{

num=11;

if(num%2==0)printf"%d is even\n",num;

elseprintf"%d is odd\n",num

}'

The output is:

11 is odd.

IF-ELSE-IF

We can create multiple IF-ELSE statements to determine multiple conditions:

$awk'BEGIN{

a=30;

if(a==10)

print"a=10";

elseif(a==20)

print"a=20";

elseif(a==30)

print"a=30";

}'

The output is:

a=30

cycle

For

The syntax for a For loop is as follows:

for(initialisation;condition;increment/decrement)

action

The for statement first performs an initialization and then checks the condition. If the condition is true, an action is performed, followed by an increment or decrement operation. As long as the condition is true, the loop will execute. At the end of each loop, the condition is checked, and if the condition is false, the loop ends.

The following example uses the For loop to output numbers 1 to 5:

$awk'BEGIN{for(i=1;i

Tags: Loops conditions statements output results examples even numbers syntax actions odd numbers numbers formats facets checks parameters multiple more states knowledge programs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia OPPO Reno MariaDB MySQL