Can you tell me what is wrong with this IF statement??
I think I mix something with operators.
namespace exercise1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int age;
age = int.Parse(textBox1.Text);
if (age < 17)
{
MessageBox.Show("You're still a youngster.");
}
if (age <= 25)
{
MessageBox.Show("Fame beckons!");
}
if (age <= 40)
{
MessageBox.Show("There's still time.");
}
if (age > 40)
{
MessageBox.Show("Oh dear, you've probably missed it!");
}
}
}
}
Thanks for help!!!
Loading

Sam HobbsRank #107 · 20.5K reputationPosted Nov 11, 2011, 8:18 AM EST
Josip Pavic20 reputationPosted Nov 11, 2011, 2:34 AM EST
Satish BhatRank #2,496 · 563 reputationPosted Nov 11, 2011, 2:37 AM EST
Josip Pavic20 reputationPosted Nov 11, 2011, 2:43 AM EST
Mayur GujrathiRank #666 · 3.6K reputationPosted Nov 11, 2011, 2:44 AM EST
your code seems correct but due to your code logic
if suppose a no is less than 25 then it executes respective code
then control comes down and will check less than 40 which also becomes true and again executes code in that block
either do as suggested satish or get out of block after displaying massagebox
Josip Pavic20 reputationPosted Nov 11, 2011, 2:58 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Nov 11, 2011, 9:03 AM EST