15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Branching Statements 565<br />

Table 3-5 (continued)<br />

Assignment Operator Purpose Example<br />

*= Multiplication assignment i *= y (equivalent to i = i * y)<br />

/= Division assignment i /= 3.5 (equivalent to i = i / 35)<br />

%= Modulo assignment i %= 2.8 (equivalent to i = i % 2.8)<br />

Branching Statements<br />

The simplest branching statement is an IF statement that only runs one or<br />

more commands if a Boolean condition is True. In Perl, the IF statement<br />

uses curly brackets to enclose one or more commands:<br />

Book VI<br />

Chapter 3<br />

Perl and Python<br />

if (condition) {<br />

Command1;<br />

Command2;<br />

}<br />

In Python, the IF statement uses indentation to enclose one or more<br />

commands:<br />

if (condition):<br />

Command1<br />

Command2<br />

To make the computer choose between two mutually exclusive sets of<br />

commands, you can use an IF-ELSE statement in Perl like this:<br />

if (condition) {<br />

Command;<br />

Command;<br />

}<br />

else {<br />

Command;<br />

Command;<br />

}<br />

In Python, the IF-ELSE statement looks like this:<br />

if (condition):<br />

Command<br />

Command<br />

else:<br />

Command<br />

Command

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

Saved successfully!

Ooh no, something went wrong!