Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a conditional statement in a printf statement?

Tags:

java

My code contains a switch statement, and in all cases there are if else statements. They are all pretty short so I was thinking about condensing the code by turning them into conditional statements. The format I was going for was...

System.out.printf( (conditional-Statement ) );

Here is my if else statement for one of the cases...

    if (count == 1) {
        System.out.printf("%3d", count);
    } else {
        System.out.printf("%11d", count);
    }

Something like...

System.out.print((count == 1) ? count : "  " + count);

does not produce syntax errors,

but it all messed up when I did...

System.out.printf((count == 1) ?  "%3d", count : "%11d", count);

Is what I am trying to do possible?

like image 408
Jaaavaaaaa Avatar asked Sep 29 '15 16:09

Jaaavaaaaa


People also ask

Can we write printf inside if condition?

The expression within if( expression ) is always evaluated, and in your case that's a call to printf . The value of this expression is used to determine if the body (blank in your case) of the if is run. Show activity on this post. printf() function return the number of characters that are printed.

Can we use ternary operator in printf?

Here's a simple decision-making example using if and else: int a = 10, b = 20, c; if (a < b) { c = a; } else { c = b; } printf("%d", c); This example takes more than 10 lines, but that isn't necessary. You can write the above program in just 3 lines of code using a ternary operator.

Where do we use conditional statements?

Conditional statements are used to decide the flow of execution based on different conditions. If a condition is true, you can perform one action and if the condition is false, you can perform another action.

What are the three types of conditional statements?

Conditional Statements : if, else, switch.

Can we use printf () statement inside a if () condition?

As long as a function returns a value, you can use any function in another (if, while, for, etc.) Yes, we can use printf () statement inside a if () condition to print anything on the screen. For other queries visit shortcpluspluscodes.blogspot.com Yes, since printf returns an int indicating the number of characters printed.

How do you use conditional formatting in an IF statement?

One of the methods to achieve Conditional formatting based on a custom formula is using the IF statement. The IF function works on the IF/THEN/ELSE condition syntax. For example, “if the given argument is TRUE, THEN return this value, ELSE that value.”

Can you put statements in printfat all?

2 You cannot put statementsinto printfat all, you only can put expressionsthere. The ternary operator forms an expression. An expression is basically a tree of operators and operands, however there are a few funny operators allowed, like the ',' comma operator or the '=' assignment operator.

How to print conditional output with variables in C++?

In particular, we’re looking at conditional output that includes variables. The << operator in C++ is the stream insertion operator and it is used to push data into the std::out output stream. This allows you to print to stdout like this: std::cout figures out how to output variables - you can just feed them in and it works automagically:


1 Answers

Yes, it is possible. But remind that the ternary operator only returns one value, not two. What you are trying to do must be done like this:

System.out.printf((count == 1) ?  "%3d" : "%11d", count);
like image 180
Little Santi Avatar answered Oct 16 '22 14:10

Little Santi