-2

My problem is, that I need to now, what statements passed through a If statement. The code is as follows.

int[] Array = {value1,value2,value3}
foreach {int Value in Array)
{
if (Value < 4)
{
    // Here i need to know what values passed through that were less that 4, like    
    // which one, value 1, value 2, and/or value 3
}

So is there a solution for a problem? I'm kind of new to programming. My problem is that i do not need an else statement, i Need to know if value 1 or 2 or 3 passed through. Exactly which ones are less than 4. EDIT: fixed some mistakes, was in a rush, forgot to put the sign the other way. When they are less than 4, i need to now which values passed through. Ill prob repost tho. As i messed up. I really don't care for now which ones are greater, or the else statement, i skipped that part. Edit2: I also came up with a solution, but i don't if its good. Should i run a loop when i store values in the if statement, making another if statement, to compare if the ones inside the if statement are the same on the outside, and then knowing which values passed through?

5
  • use else if (Value < 4){ //what you want to do }
    – Matt
    Jun 29, 2013 at 1:26
  • What about the ones that are exactly equal to 4, do you want to know about them as well?
    – Barmar
    Jun 29, 2013 at 1:27
  • Most of the answers have suggested using else. Didn't your programming teacher or the tutorial you're learning from show the full structure of an if statement? This is really basic stuff to be asking here.
    – Barmar
    Jun 29, 2013 at 1:29
  • @Barmar I messed up the question. I skipped the else on purpose as it was not needed. I messed up the sign, giving everyone here confusion. Jun 29, 2013 at 2:50
  • What confused everyone is that you said "what values", but you meant "what indexes".
    – Barmar
    Jun 29, 2013 at 3:10

5 Answers 5

2

I'm not 100% positive if I understand the question but it seems you can use the else statement

if (Value > 4)
{
 // Do your stuff for elements greater than 4
}
else
{
 // Do your stuff for elements greater lower or equal than 4
}
1
  • No, I want to know what values pass through, which ones are less than 4. Jun 29, 2013 at 2:00
1

How about use for instead of foreach, since you got index of array member, you will know which one passed through

int[] array = {value1, value2, value3}
for (int index = 0; index < array.Count(); index++)
{
    if (array[index] < 4)
    {
        // do sth with index
    }
}
2
  • Thanks! Never thought of that! Was in A rush, and kinda late were I live. Jun 29, 2013 at 2:36
  • Funny, once upon a time this was the only way to loop through an array. :)
    – Barmar
    Jun 29, 2013 at 3:09
0
int Array[] = {value1,value2,value3}
foreach {int Value in Array)
{
if (Value > 4)
{
    // Here i need to know what elements passed through that were less that 4
}else if(Value < 4){
  //values < 4 will execute this code
}
0
0

I'm going to make a few general suggestions that should hopefully be helpful. First of all, your conditional says if (Value > 4) so you will not go into that code block where you suggest figuring out which elements are less than 4. Instead you'd need an else. So here's one way;

int Array[] = {value1,value2,value3}
List<int> lessThanFour = new List<int>();
foreach {int Value in Array)
{
    if (Value < 4)
    {
        lessThanFour.Add(Value);
        Console.WriteLine(Value);
    }
}

The above code puts each value which is less than four into a list so you can access them later. It also prints them to the console.

Another option would be to use LINQ;

var lessThanFour = Array.Where(x => x < 4);
foreach (int c in lessThanFor)
    Console.WriteLine(c);

The above code uses the Where operator to create a new array with all ints in the original that have a value less than for. The statement x => x < 4 is best to think of in an iterative since where x is the current element. It works the same as the foreach loop. When you execute that code it basically says, for each int x in Array, if x is less than four add it to the result. Then I use a foreach below that to print out the results.

1
  • Yeah but using LINQ Won't help my problem, First off, I put the > sign the wrong, way, its fixed now, But i Need to now if Value1, Value2, or Value3 passed through. Jun 29, 2013 at 2:14
0

Your question is poorly framed I think but it sounds like you are looking for a switch case.

if (x < 4) {
   switch (x) {

       case 1: 
           Console.WriteLine("Case 1");
           break;

       case 2:
           Console.WriteLine("Case 2");
           break;

       case 3:
           Console.WriteLine("Case 3");
           break;

       default:
           Console.WriteLine("Default case");
           break;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.