15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

566<br />

Looping Statements<br />

The IF-ELSE statement only offers two choices. If you want to offer multiple<br />

choices, you can use the IF-ELSEIF statement, which uses two or more<br />

Boolean conditions to choose which of two or more groups of commands to<br />

run. In Perl, use the ELSIF keyword, such as<br />

if (condition1) {<br />

Command;<br />

Command;<br />

}<br />

elsif (condition2) {<br />

Command;<br />

Command;<br />

}<br />

elsif (condition3) {<br />

Command;<br />

Command;<br />

}<br />

In Python, use the ELIF keyword, such as<br />

if (condition1):<br />

Command<br />

Command<br />

elif (condition2):<br />

Command<br />

Command<br />

elif (condition3):<br />

Command<br />

Command<br />

Unlike other <strong>programming</strong> languages, neither Perl nor Python provides a<br />

SWITCH statement.<br />

Looping Statements<br />

A looping statement repeats one or more commands <strong>for</strong> a fixed number of<br />

times or until a certain Boolean condition becomes True. To create a loop that<br />

repeats <strong>for</strong> a fixed number of times, use the FOR loop, which looks like this:<br />

<strong>for</strong> (startvalue; endvalue; increment) {<br />

Command;<br />

}<br />

If you wanted the FOR loop to run five times, you could set the Start value to<br />

1 and the End value to 5, such as<br />

<strong>for</strong> (i = 1; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!