Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: House Builder - November 19, 2018 :: The Game - December 14, 2018 :: Pong - December 17, 2018

Computer Programming I :: Lessons :: If Statements

If Statements

An if statement, or conditional statement, is a programming construct that determines if a condition is true or not. Code can be written to run only if a condition is true. In a way, the condition of an if statement is like a boolean in that it will either be true or false. An if statement is written in the following way:

if (first == "Michael")
     MessageBox.Show("Hi, Michael!");

The code in an if statement can contain anything, including more if statements. Below are some example if statements. Try to determine whether or not the code within the if statement will run. Press the button to check your answer.

string pitcher = "Arrieta";
if (pitcher == "Arrieta")
     MessageBox.Show("Run, if statement, run!");

Else If Statements

An else if statement will run if the if statement it is joined to is not true. Else if statements run in the order they appear so if one of them is true the rest of them are ignored. In the examples below try to determine which message box will appear.

int num1 = 5;
if (num1 < 1)
     MessageBox.Show("1");
else if (num1 > 10)
     MessageBox.Show("2");
else if (num1 == 5)
     MessageBox.Show("3");

int num1 = 4;
if (num1 < 1)
     MessageBox.Show("1");
else if (num1 > 10)
     MessageBox.Show("2");
else if (num1 == 5)
     MessageBox.Show("3");

The example above shows that you can have a situation where none of the conditions are true. In this case none of the message boxes appear. Also, you can use the following symbols in a condition:

> Greater Than
>= Greater Than OR Equal To
< Less Than
<= Less Than OR Equal To
== Equal To
!= Not Equal To

Finally, you can use an Else statement that will run if none of the conditions above it are met. The following example will run the Else code.

int num1 = 4;
if (num1 < 1)
     MessageBox.Show("1");
else if (num1 > 10)
     MessageBox.Show("2");
else if (num1 == 5)
     MessageBox.Show("3");
else
     MessageBox.Show("4");

Random Numbers

Now we want to make a random picture appear when you hit a button. We will need to generate random numbers to do this.

Random rndGen = new Random();
int rndNum = rndGen.Next(0, 3);
if (rndNum == 0)
     picLester.Visible = true;
else if (rndNum == 1)
     picBryant.Visible = true;
else if (rndNum == 2)
     picRussell.Visible = true;

The first line of code above create a new random number generator called rndGen, although the actual number has not been set yet. The Next method chooses a number between 0 and 3, not including 3. It is important to note that the first number is included while the second number is not included.

We now have random pictures appearing, but there is a problem. The problem is that the old pictures don't disappear. To fix this, you need to make sure the other pictures are made invisible at each step as in the following code.

Random rndGen = new Random();
int rndNum = rndGen.Next(0, 3);
if (rndNum == 0)
{
     picLester.Visible = true;
     picBryant.Visible = false;
     picRussell.Visible = false;
}
else if (rndNum == 1)
{
     picLester.Visible = false;
     picBryant.Visible = true;
     picRussell.Visible = false;
}
else if (rndNum == 2)
{
     picLester.Visible = false;
     picBryant.Visible = false;
     picRussell.Visible = true;
}
Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram