Latest topics
» GUYS. "DOOD" IS HERE!
Tue Nov 29, 2011 10:27 am by Karma

» Hi guys! Lalala
Mon Nov 28, 2011 9:26 pm by Pukito

» ARGHHHrrhhghghg
Sun Nov 27, 2011 1:25 pm by Pukito

» [Flash Game] - I don't even
Sat Nov 26, 2011 11:08 pm by Karma

» Hi, nice to meet you.
Sat Nov 26, 2011 12:48 am by Pukito

» I think I see ..
Sat Nov 26, 2011 12:47 am by Pukito

» Where Karma goes...
Fri Nov 25, 2011 2:14 am by Karma

» Guys
Tue Nov 22, 2011 9:11 pm by Pukito

» Karmini's Pose
Tue Nov 22, 2011 8:31 pm by Karma

» The Order (1)
Sat Nov 19, 2011 10:42 pm by Karma

December 2018
SunMonTueWedThuFriSat
      1
2345678
9101112131415
16171819202122
23242526272829
3031     

Calendar Calendar


[AS3 Chapter 4] Conditional Statements and Operators

Go down

[AS3 Chapter 4] Conditional Statements and Operators

Post by Pukito on Sun Oct 09, 2011 8:14 pm

If Statements
If statements are used to determine whether or not a statement is true. If that statement is true, then a particular piece of code will be executed (the code within the curly braces of the if statement). The general form of an If statement is as follows:

Code:

if (condition){
//then do something
}

Yes, the structure is very similar to that of a function. However, there must be a condition in the if statement. There are also operators used within the condition. What is an operator, you ask? Well, consider the following:

Code:

if(statement==true){
//do something
}

Operators

Take a look at the condition (dog.kicked==true) the == is the operator. It is what you are checking for. So, if you use different operators, if statements can be generalized into:

Code:

if(this operator this){
//do something
}

Here is a list of operators and their meanings:

==
equals to
>=
greater than or equal to
<=
less than or equal to
>
greater than, but not equal to
<
less than, but not equal to
!= or <>
not equal to
!
not
&&
and
||
either one is equal to

The operators must be typed exactly as the appear above or else the if statements won't work. For example, == must have two equal signs. Two equal signs are used when you are comparing the values. One equal sign is used when you are assigning a value. Example:

Code:

if(dog.kicked==true) // double equal signs used when comparing values
{
Pukito = angry; // single equal sign used when assigning a value
}

The ! not operator may be a little trickier to use than the others:

Code:
// !
if(!dog.kicked==true){
pukito = happy; //code will execute as long as dog.kicked is not true
}

Actually, the ! operator is the same as using the != or <> operators, it's just typed differently. Here's an example of the != operator:
Code:

if(dog.kicked!=true){
pukito = happy;
}

The && and operator will only be true if both statements are true.
Code:
// &&
var dogKicked:Boolean = true;
var itwasRainy:Boolean = false;
var variable:Boolean = false;

if((dogKicked==true)&&(itwasRainy==true)){
variable = true;
}

trace(variable); /*output will be false. The if statement did not execute because both statements were not true, and therefore the entire condition was not true */

itwasRainy = true;
if((dogKicked==true)&&(itwasRainy==true)){
variable = true;
}

trace(variable); //output will be true

The || or operator will evaluate to true if one or more of the statements is true
Code:

var dogKicked:Boolean = true;
var itwasRainy:Boolean = false;
var variable:Boolean = false;

if((dogKicked==true)||(itwasRainy==true)){
variable = true;
}

trace(variable); //output will be true

[EDIT] I'm going to add a tables that summarize the evaluations in order to make it easier to understand:

And (&&) Operator
Code:

if(statement1&&statement2){
}
Statement 1
Statement 2
Evaluation
True
True
True
False
False
False
True
False
False
False
True
False
Or (||) Operator

Code:


if(statement1 || statement2){

}

Statement 1
Statement 2
Evaluation
True
True
True
False
False
False
True
False
True
False
True
True

Else.. Else If...
If you had many conditions that you wanted to check with If statements, how would you do it? You could simply go:

Code:

if((condition1)||(condition2)||(condition3)||(condition4)){
//then do something
}

Now, what if you wanted different things to happen as a result of different conditions? Surely, you could have multiple If Statements..
Code:

if(condition1){
}
if(condition2){
}
if(condition3){
}

Yes, still workable. Now, what if you wanted only one of those if statements to execute, even if all of them were true? Well, it would still be possible.

Code:

var var1:Boolean = true;
var var2:Boolean = false;

if(var1==true){
var2 = true;
}

if(var2==false){
// any code written in this if statement will not be executed.
}

But... there is an easier way and using variables takes up a bit of space. For good programming practices, you want to use as little resources as you possibly can -- it really makes a difference if you're making large programs. By using the Else If statements, you can add more conditions in a much easier and less resource-consuming fashion. Consider the following:

Code:

if(condition1){
}
else if(condition2){
}

The program will go through the all of the statements. When it reaches one that is true, it will execute the code in that statement and ignore the rest of the if statements. That's where we need to be careful. Take a look at the following code snippet:

Code:

var text:String;
var score = 10;
if(score<5){
text = "You scored less than 5.";
}
else if(score>5){
text = "You scored more than 5.";
}
else if(score==10){
text = "You scored 10.";
}

trace(text);

What will the output be? That's right, it will be "You scored more than 5." While still true, it does not display the desired result. That's why the order of the if statements does matter -- only the first true statement will be executed.

Else Statement
If you have all of your handy dandy if statements and else if statements, you might still need a statement to make something happen if all of the statements are false. This is where the else statement comes in.

Code:

if(condition1){
}
else if(condition2){
}
else{
}

Notice that the else statement does not need a condition. This is because it will automatically execute if none of the if statements above it are true. You do not need to include an else statement.

Nesting
While considered bad programming practice, nesting is often very useful. Nesting is when you put if statements inside of other if statements.

if(condition!){
if(condition2){
//do something
}
else if(condition3){
//do something
}
}
else if(condition4){
// do something
}
else{
//do something
}

Nesting increases the chances of bugs occurring and can be very confusing to read, whether its by the programmer or by someone else who is looking at the code. Inserting indentations for every new if statement helps make the code easier to read.
I will not talk about nesting anymore, because it is quite obvious what it does. Any nested if statements can do anything that a regular if statements can -- they're all if statements! There are a bunch of ways to not include nesting, but it usually requires having more statements and heavy use of the && and || statements.

Well, that's it for this chapter. Smile


Last edited by Pukito on Sun Oct 09, 2011 10:13 pm; edited 1 time in total

_________________
Put on your sunglasses... Cool and put on your 'fro... afro . Because I'm here now clown.

My friends list:

Rainy albino
Karma scratch
Serene pig
avatar
Pukito
Cobbler

Posts : 377
Monies : 2875
Reputation : 4
Join date : 2011-09-27
Location : MHMM.

View user profile http://www.areost.com

Back to top Go down

Re: [AS3 Chapter 4] Conditional Statements and Operators

Post by Karma on Sun Oct 09, 2011 9:07 pm

If ( Eyepoke . Pookie == true )
Karma = Happy.

If ( !Eyepoke . Pookie == true )
Karma = RAGE

var . EyepokePookie = true
var . PillowtoFace = false
var . EyepokeKarma = false


Then..

If (( EyepokePookie == true) | | (PillowtoFace == true) && (EyepokeKarma == true ))

That would be false right? Unless the | | Make PillowtoFace true, and && makes EyepokeKarma true.

Or do I need to use that var . variable thing aswell? xD

_________________
ppionngg


Pookie just likes to laugh at my life
avatar
Karma
Baker
Baker

Posts : 347
Monies : 2687
Reputation : 7
Join date : 2011-09-27
Age : 25
Location : Your face

View user profile

Back to top Go down

Re: [AS3 Chapter 4] Conditional Statements and Operators

Post by Pukito on Sun Oct 09, 2011 9:15 pm

Karma wrote:If ( Eyepoke . Pookie == true )
Karma = Happy.

If ( !Eyepoke . Pookie == true )
Karma = RAGE

var . EyepokePookie = true
var . PillowtoFace = false
var . EyepokeKarma = false


Then..

If (( EyepokePookie == true) | | (PillowtoFace == true) && (EyepokeKarma == true ))

That would be false right? Unless the | | Make PillowtoFace true, and && makes EyepokeKarma true.

Or do I need to use that var . variable thing aswell? xD

Dood. You have to use the curly braces with your if statements ):

if(condition){
//do something
}

Yes, that would be false. To make it true, you just need to make EyepokeKarma true because EyepokePookie is already true.

(( EyepokePookie == true) | | (PillowtoFace == true) && (EyepokeKarma == true ))

It goes from left to right, so the or statement is executed first. The green evaluates to true:


(true && (EyepokeKarma == true ))

but EyepokeKarma is false, so the whole statement becomes false. To make the whole thing true, EyepokeKarma has to be true. Wink

_________________
Put on your sunglasses... Cool and put on your 'fro... afro . Because I'm here now clown.

My friends list:

Rainy albino
Karma scratch
Serene pig
avatar
Pukito
Cobbler

Posts : 377
Monies : 2875
Reputation : 4
Join date : 2011-09-27
Location : MHMM.

View user profile http://www.areost.com

Back to top Go down

Re: [AS3 Chapter 4] Conditional Statements and Operators

Post by Karma on Sun Oct 09, 2011 9:21 pm

Ohoho. So if 1 thing is false the whole thing is untrue?

_________________
ppionngg


Pookie just likes to laugh at my life
avatar
Karma
Baker
Baker

Posts : 347
Monies : 2687
Reputation : 7
Join date : 2011-09-27
Age : 25
Location : Your face

View user profile

Back to top Go down

Re: [AS3 Chapter 4] Conditional Statements and Operators

Post by Pukito on Sun Oct 09, 2011 10:06 pm

Karma wrote:Ohoho. So if 1 thing is false the whole thing is untrue?

Yup!

Well, it depends what the operator is. If it was

Code:

var KarmasFace = true;
var KarmaisPoor = false;

if ((KarmasFace==true)||(KarmaisPoor==true)){
// this code would be executed
}

Then it would still be true because it is an or statement, and one of them is true. If is was an and statement, however...

Code:

var KarmasFace = true;
var KarmaisPoor = false;

if ((KarmasFace==true)&&(KarmaisPoor==true)){
// this code would not be executed
}

then it would be false because only one is true and both need to be true in order for it to be true.

_________________
Put on your sunglasses... Cool and put on your 'fro... afro . Because I'm here now clown.

My friends list:

Rainy albino
Karma scratch
Serene pig
avatar
Pukito
Cobbler

Posts : 377
Monies : 2875
Reputation : 4
Join date : 2011-09-27
Location : MHMM.

View user profile http://www.areost.com

Back to top Go down

Re: [AS3 Chapter 4] Conditional Statements and Operators

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum