Quantcast
Channel: Malicious Code
Viewing all articles
Browse latest Browse all 9

The If Statement

$
0
0

In my previous post titled Summary and Analysis of “An Undetectable Computer Virus” by Chess and White I quoted and used something called pseudocode in order to help present the argument. I, like Chess and White, made the assumption that the reader will be familiar with pseudocode. However, I can’t be certain of this so in this post I will briefly explain some of the pseudocode that occurred in the previous post.

The if statement is a way for a programmer to control the flow of steps of a program (which without would normally just run one line after another. The statement usually looks something like this (though it varies by programming language):

<Code Before if Statement>

if <some condition>:

<Block of Code 1>

else:

<Block of Code 2>

<Code After if Statement>

So when this program runs it will do the <Code Before if Statement> and when it gets to the if part of the above code it checks to see if <some condition> is true or false. If it is true the program then executes <Block of Code 1> and then skips straight to <Code After if Statement>. If <some condition> is false the program will jump straight to the else statement and will execute <Block of Code 2> and then go ahead and execute <Code After if Statement>.

An Example:

<Code Before if Statement>

if x < 5:

print “x is less than 5″;

else:

print “x is greater than or equal to 5″;

<Code After if Statement>

So, for example, if x = 2 this program would execute the <Code Before if Statement> the check to see if the condition x <5 is true. Since the condition is true the program prints “x is less than 5” (prints to the users screen). Then the program executes the <Code After if Statement>

However, if x = 7 this program would still execute <Code Before if Statement> but when it gets to the condition it evaluates to false this time (since 7 is not less than 5). The program then jumps to the else part of the code (since the condition is false) and prints “x is greater than or equal to 5” to the screen for the user. Then it executes <Code After if Statement>

So in terms of the if statement from the previous post we have:

if X(p) then exit, else spread;

This can of course be written as:

if X(p):

 exit;

else:

spread;

so it looks like the ones above. So here the condition is X(p) and when the program gets to evaluating the condition is check to see whether or not X(p) is true or false. If X(p) is true the program exits, if not then the program (in this cause the virus) spreads (in real code spread would be replaced with multiple lines of code that would tell the program how to spread).

For more on the if statement try Learn Python The Hard Way and look at lessons 29, 30, and 31.



Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles



Latest Images