Geeks With Blogs
Stephanie Grima C#, ASP.NET and my life.

 

If/Else Statement:

The code between the curly brackets executes only if it meets the value of the boolean expression:

if(expression)
{
     statement1
}
else
{
     statement2
}

If expression is true, execute statement1, else if it is false, execute statement2. The following is a much more realistic example:

string sentence = "";
if(name == "Stephanie")
{
     sentence = "Hello " + name;
}
else
{
     sentence = "Hello there";
}

An improvement over this if/else statement is the nested if/else. This means that when the expression is executed, the next step is to find another expression to fullfill. Example:

string sentence = "";
if(name == "Stephanie")
{
     if(surname == "Grima")
          sentence = "Hello " + name +  " " + surname;
     else
          sentence = "Hello " + name;
}
else
{
     sentence = "Hello there";
}

In the example above, we first check if the user's name is Stephanie, and if it is, we check whether her surname is Grima or not. If it is, her full name is saved.

 

Posted on Wednesday, October 29, 2008 9:55 AM C# , Java | Back to top


Comments on this post: [C#] [Java] Control Structures Part 1 : If / Else Statement

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Steffi | Powered by: GeeksWithBlogs.net