15.01.2024 Views

CompTIA A+ Certification All-in-One Exam Guide

  • No tags were found...

Create successful ePaper yourself

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

2. The if statement checks to see if the animal variable contains a value

of “cat”.

3. If that’s the case, the computer will print the word “meow” to the

command prompt. If not, then nothing will happen.

In this case, the script would never print “meow” because the condition

was not met.

Conditionals are a critical building block for most scripts and programs.

One of the main reasons to write a script in the first place is because you need

to do something many times. In programming, a good way to accomplish this

is with a loop. Basic loops are another kind of conditional, but tell the

computer to run the code over and over until the condition is (or is not) met.

Depending on the language, you’ll usually see loops indicated with keywords

like for and while. Here’s an example:

This loop will run until the number of cows is 4, so it will print “moo”

four times—one for each cow. Because the cows variable has an integer

value, we can do math with it. Each time the loop runs, it prints “moo” once,

and adds one to the number of cows.

EXAM TIP Different languages have different control structures. Most of

them have at least a few. This section looked at two—if statements and loops

—but the CompTIA A+ objectives only expect you to identify basic loops.

Comments

Scripting and programming languages usually have a way to insert special

text—called a comment—that helps anyone reading the script later (including

the writer, a few months on!) understand what’s going on. The computer

ignores this text, as it’s only for the humans. Leaving comments is a really

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

Saved successfully!

Ooh no, something went wrong!