can we pass condition of if statement as an argument in any language?????????

class Matrix
{
	static void loop(char a,char b,char c)
{
	int a[][]=new int[3][3] ;
	byte i,j ;
	for(i=0;i<3;i++)
		{
		   for(j=0;j<3;j++)
		     {	
			if(a b c)
			System.out.print(a[i][j]+"\t");
			else
			System.out.print(" "+"\t");
		   }
		}
		
	public static void main(String ar[])
	{	
			loop('i','>','j');					
	}
}

it is just a example ..datatype and syntax is not correct..here i hav expressed what i am asking, is this possible and if yes what would be the datatype of argument??

REPLY WILL BE APRECIATED AND WOULD BE VERY HELPFUL FOR MY PROGRAMBLE BRAIN ;) BECAUSE THIS QUESTION IS BLOKING MY HEAD TO MOVE FURTHER....

can we pass condition of if statement as an argument in any language?????????

like-->

function(argument)
{
if(argument)
{
}
}


is this possible and if yes what would be the datatype of argument??

REPLY WILL BE APRECIATED AND WOULD BE VERY HELPFUL FOR MY PROGRAMBLE BRAIN ;) BECAUSE THIS QUESTION IS BLOKING MY HEAD TO MOVE FURTHER....

I guess that is possible, but what language are you programming in? Tiny detail :)

I guess that is possible, but what language are you programming in? Tiny detail :)

Sir well the above program is in java but.. as i can write the same in c,c++ therefore want to know the correct procedure.

Incase of java , yes you can ....(possible in other languages also)

but remember...you can pass only variables but not the logic ..
i mean

void dothis(String s)
{

   if(s)
{
 -----

if you call the dothis as dothis("10>5"); ..It is not correct

you can pass the values i.e 10 , 5 but not the logic(i mean 10>5)

because.. if condition will accept only boolean result..

if(condition) ---- the condition should always evaluate to boolean value in java

I guess that is possible, but what language are you programming in? Tiny detail :)

Hmm, as far as programming in java practice is concerned, I think you will get a compile error if you try doing something like this

int i=0;
public static void makeCake(if i > 0,  )
{

}

Therefore, the answer is No, you can't. I'd be surprised if you could do that.

can we pass condition of if statement as an argument in any language?????????

class Matrix
{
	static void loop(char a,char b,char c)
{
	int a[][]=new int[3][3] ;
	byte i,j ;
	for(i=0;i<3;i++)
		{
		   for(j=0;j<3;j++)
		     {	
			if(a b c)
			System.out.print(a[i][j]+"\t");
			else
			System.out.print(" "+"\t");
		   }
		}
		
	public static void main(String ar[])
	{	
			loop('i','>','j');					
	}
}

it is just a example ..datatype and syntax is not correct..here i hav expressed what i am asking, is this possible and if yes what would be the datatype of argument??

REPLY WILL BE APRECIATED AND WOULD BE VERY HELPFUL FOR MY PROGRAMBLE BRAIN ;) BECAUSE THIS QUESTION IS BLOKING MY HEAD TO MOVE FURTHER....

As already said above, you can pass the chars to the function pretty easy. If you want to pass the logic operator as well, you will have to use for example an if-statement, each including a loop. If you just want to pass the chars, the code looks like this:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testdani;

/**
 *
 * @author pietervanlaere
 */
public class TestDani {

    
    static void loop(char a,char b){
        System.out.println("Initial char: " + a);
        System.out.println("Last char: " + b);
        
        for (char temp= a; temp < b; temp++){
            System.out.println(temp);
        }
    }
    
    public static void main(String[] args) {
        loop('a', 'z');
    }
}

If you want to pass the operator to the function, this is a (basic, simple) solution to do so. There may be other alternatives to do so, but I don't use Java that often, so I would do it this way:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testdani;

/**
 *
 * @author pietervanlaere
 */
public class TestDani {

    
    static void loop(char a,char b, char c){
        System.out.println("Initial char: " + a);
        System.out.println("Last char: " + b);
        if (c == '<'){
            for (char temp= a; temp < b; temp++)
                System.out.println(temp);
        }        
        if (c == '>'){
            for (char temp = a; temp > b; temp++)
                System.out.println(temp);
        }
        
    }
    
    public static void main(String[] args) {
        loop('a', 'z', '<');
    }
}

Kind regards,

thanks for the reply...... bt sir if i have to pass in such a way than...my prpose is not getting solve actualy...i.e passing if statement any condition as funtion's argument. why cant i>j can be pass as string??

Incase of java , yes you can ....(possible in other languages also)

but remember...you can pass only variables but not the logic ..
i mean

void dothis(String s)
{

   if(s)
{
 -----

if you call the dothis as dothis("10>5"); ..It is not correct

you can pass the values i.e 10 , 5 but not the logic(i mean 10>5)

because.. if condition will accept only boolean result..

if(condition) ---- the condition should always evaluate to boolean value in java

Since you're passing a String Object to your method doThis(String s), the argument "10>5" is perfectly fine. However, it is not treated as a logical operator. It's just gonna be "10>5" and nothing more or less.

can we pass condition of if statement as an argument in any language?????????

The original question is about whether you're ALLOWED to pass an if-statement in the args which is not acceptable in Java. I agree that you can pass your condition in a for-loop after you initialise your variable and that is fine.

thanks for the reply...... bt sir if i have to pass in such a way than...my prpose is not getting solve actualy...i.e passing if statement any condition as funtion's argument. why cant i>j can be pass as string??

Do you mean that you want to pass this i>j as a string argument in a method? If yes so, then yes you can and the result you'll get out of it is that "i>j" which doesn't compare i to j. Here is an example that might hopefully be useful in your case,

public static void compareMe(){

                int x =0;
		int y =3;
		System.out.println( ((x >= y) ? x:y) );    
 // This line says if x >= y, then println x else println y.

 
}

Since you're passing a String Object to your method doThis(String s), the argument "10>5" is perfectly fine. However, it is not treated as a logical operator. It's just gonna be "10>5" and nothing more or less.

The original question is about whether you're ALLOWED to pass an if-statement in the args which is not acceptable in Java. I agree that you can pass your condition in a for-loop after you initialise your variable and that is fine.

ok if i change thelanguage and ask it for c++ ...do i ill be allowed doing so.

an if statement, maybe not, an expression.. why not?
remember, the expression of an if statement always will turn out to be a boolean.

public static void printResult(boolean b){
if ( b ){
System.out.println("the expression = true");
return;
}
System.out.println("the expression = false");
}

public static void main(String[] args){
printResult(1 <= 2);
printResult(2 <= 1);
}

As already said above, you can pass the chars to the function pretty easy. If you want to pass the logic operator as well, you will have to use for example an if-statement, each including a loop. If you just want to pass the chars, the code looks like this:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testdani;

/**
 *
 * @author pietervanlaere
 */
public class TestDani {

    
    static void loop(char a,char b){
        System.out.println("Initial char: " + a);
        System.out.println("Last char: " + b);
        
        for (char temp= a; temp < b; temp++){
            System.out.println(temp);
        }
    }
    
    public static void main(String[] args) {
        loop('a', 'z');
    }
}

If you want to pass the operator to the function, this is a (basic, simple) solution to do so. There may be other alternatives to do so, but I don't use Java that often, so I would do it this way:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testdani;

/**
 *
 * @author pietervanlaere
 */
public class TestDani {

    
    static void loop(char a,char b, char c){
        System.out.println("Initial char: " + a);
        System.out.println("Last char: " + b);
        if (c == '<'){
            for (char temp= a; temp < b; temp++)
                System.out.println(temp);
        }        
        if (c == '>'){
            for (char temp = a; temp > b; temp++)
                System.out.println(temp);
        }
        
    }
    
    public static void main(String[] args) {
        loop('a', 'z', '<');
    }
}

Kind regards,

Thanks for such a well explained reply... sir i will surely sort of my problem with the content you provided. sir if programed with the syntax of c++ then can i do the same...or say my question is valid or not??

ok if i change thelanguage and ask it for c++ ...do i ill be allowed doing so.

I am not sure about c++. Obviously, you've asked the wrong person and we are not in the c++ forum. You could ask the c++ people so they will answer your question with a definite answer. Are you trying to program in java or C or C++?
By the way, each of the languages has its own section on the website.

Good luck,

you can actually pass a statement viewed as a boolean expression to a method the same way you would to an if statement, but the method needs to be programmed to receive a boolean value as argument, because the statement will be evaluated before its final value is given to the method as an argument.

it might not help what your trying to do, but heres the closest thing you can do to give a condition to a method :

public class Test{
    int x;
    int y;

    public int enterSomeCondition(bool b){
        if(b){
             //do stuff
             return x;
        }
        else{
             //do other stuff
             return y;
        }
    }

    public static void main(...){
         x=5;
         y=6;
         int result = enterSomeCondition(x>y);
         System.out.println("result = " + result);
    }
}

this sample doesnt do anything logical but i hope you get the point

You can pass a String containing any valid JavaScript expression or program and get it evaluated, returning the result to your Java program. This works because JavaScript is a dynamic language, compiled and evaluated at runtime. Requires Java 1.6 or later.
Eg:

ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
      try {
        String condition = "a==b";  // expression to evaluate
        js.put("a", 2);             // value for a
        js.put("b", 3);             // value for b
        Object result = js.eval("var a,b;" +  condition + ";"); // evaluate expression with values
        System.out.println(result);
      } catch (ScriptException ex) {
          ex.printStackTrace();
      }

Sorry about the late reply.

commented: very interesting!! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.