Hi !
First of all I would to say, that I´m just a beginner in C#, I may ask some silly question , I apologize for this convenient.
well I have a program and I want to stop it from executing the rest of the code until the condition witch is var > 0 is true .
the problem after I execute this code , the program still ask me about the second score even the previous score is negative .
And here is the code :
[CODE]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string name;
string svalue;
int x, y, z, aV;
// Enter User´s name.
Console.Write("Enter please your name : \n");
name = Console.ReadLine();
// grap three scores from users.
Console.Write("Enter 1 score : \n ");
svalue = Console.ReadLine();
x = int.Parse(svalue);
/* Testing if the score is less than 0
negative nubers are not allowed */
if (x < 0)
Console.WriteLine("The score must be a positive number ");
else if (x > 0)
Console.Write("Enter 2 score : \n ");
svalue = Console.ReadLine();
y = int.Parse(svalue);
/* Testing if the score is less than 0
negative nubers are not allowed */
if (y < 0 )
Console.WriteLine("The score must be a positive number ");
else if (y > 0)
Console.Write("Enter 3 score : \n ");
svalue = Console.ReadLine();
z = int.Parse(svalue);
/* Testing if the score is less than 0
negative nubers are not allowed */
if ( z < 0 )
Console.WriteLine("The score must be a positive number ");
else if (z > 0)
// Dispaying the inputs
Console.WriteLine("Your name is {0}", name);
Console.WriteLine("Your score is {0}", x + y + z / 3);
Console.ReadKey();
}
}
}
[/CODE]
Loading
Jean PaulPosted Nov 10, 2011, 12:55 PM
Simply the If statement is executed if the condition is true.
if (condition)
statement
So if the condition is false, the branching falls to the else statement or the code after the if block.
In you case you need to stop the branching and continue the loop until the value is > 0.
There are many options to achieve this.
1) Use goto statement (not recommended as unstructured pgmg)
2) Use do..while loop - The do block is executed first without any condition check. At the end the while condition will be checked and loop is continued if the condition is true.
I have modified the code. Please check it.
One more thing the x + y + z / 3 will be executed as x + y + (z / 3)
[as / operator have higher precedence than + operator]
Happy Adventures!!
Regards,
Jean Paul
VulpesPosted Nov 10, 2011, 12:51 PM
Javeed M ShaikhPosted Nov 10, 2011, 12:48 PM
if (x < 0)
{
Console.WriteLine("The score must be a positive number ");
}
else if (x > 0)
{
Console.Write("Enter 2 score : \n ");
svalue = Console.ReadLine();
y = int.Parse(svalue);
}
/* Testing if the score is less than 0
negative nubers are not allowed */
if (y < 0)
{
Console.WriteLine("The score must be a positive number ");
}
else if (y > 0)
{
Console.Write("Enter 3 score : \n ");
svalue = Console.ReadLine();
z = int.Parse(svalue);
/* Testing if the score is less than 0
negative nubers are not allowed */
}
if (z < 0)
{
Console.WriteLine("The score must be a positive number ");
}
else if (z > 0)
{
// Dispaying the inputs
Console.WriteLine("Your name is {0}", name);
Console.WriteLine("Your score is {0}", x + y + z / 3);
Console.ReadKey();
}