Why do I love nested If statements?

If you’ve read the first post you’ll remember that I was struggling to get an if statement that worked the way I wanted it to (link). Well this time I’ve sat down for another half hour to just fix it and make it do what it should.

if (filmno == 1 && age >= Legend) 
 {
 Console.WriteLine("Enjoy the film");
 }
 else if (filmno == 2 && age >= Macbeth)
 {
 Console.WriteLine("Enjoy the film");
 }
 else if (filmno == 3 && age >= Everest)
 {
 Console.WriteLine("Enjoy the film");
 }
 else if (filmno == 4 && age >= Walk)
 {
 Console.WriteLine("Enjoy the film");
 }
 else if (filmno == 5 && age >= Hotel)
 {
 Console.WriteLine("Enjoy the film");
 }
 else
 {
 Console.WriteLine("You cannot see this film");
 }

What I’ve done is turn 6 IF statements from being separate into one long IF statement with many multiple conditions that will check as the program is run. Something that’s called a Nested IF statement, since other IF statements ‘nest’ inside the first. Kind of like those Russian dolls I guess.

But why do I like them? Because they’re simple and do the job. Yes there are more complex ways of solving this problem but this works. I can learn to write more complex code later once I understand it but I cant make something work if I don’t understand what it is I’m doing.

Advertisements

One thought on “Why do I love nested If statements?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s