Hello.,
I'm very new in C++, and I have a task to solve.
I have a function:

void cio_display(const char *str, int row, int col, int len){
  int i;
  cio_move(row, col);
  if(len<=0) {
    cio_putstr(str);
  }
  else{
    for(i=0;i<len && str[i];i++)
      cio_putch(str[i]);
   
    for(;i<len;i++)
      cio_putch(' ');
  }

here iss what i have done:

void cio_display(const char *str, int row, int col, int len){
	
	int i;
	cio_move(row, col);
	for( i = 0; ((len <= 0) && cio_putstr(str)), (i < len && str[i]); i++){
	cio_putch(str[i]);
	}

	for(;i<len;i++)
	cio_putch(' ');
}

The problem id the function cio_putstr() returns void, thats why lazy evaluation is not possible. Any other thoughts would be greatly appreciated!

Thank you

Dani AI

Generated

Short diagnosis: the failed “lazy evaluation” trick happened because cio_putstr() returns void. C/C++ logical operators (&&, ||) short‑circuit only when both operands can be evaluated as boolean values; a void result can't participate in those expressions. pointed this out, and 's comma/operator idea shows it's possible to cram logic into loop headers, but that style is hard to read and brittle.

Two readable, "no if" alternatives (useful for ):

void cio_display(const char *str, int row, int col, int len) {
    cio_move(row, col);
    std::string s = str ? str : "";
    int n = (len <= 0) ? static_cast<int>(s.size()) : std::min(static_cast<int>(s.size()), len);
    std::string out = s.substr(0, static_cast<size_t>(n));
    out.append(static_cast<size_t>(std::max(0, len - n)), ' ');
    cio_putstr(out.c_str());
}

This builds the exact output first (no conditional branches calling cio_putstr), then emits it in one call.

If avoiding extra allocation is required, pick the printer at runtime with a callable:

#include <functional>

void cio_display(const char *str, int row, int col, int len) {
    cio_move(row, col);
    std::function<void(const char*)> printer =
        (len <= 0)
        ? std::function<void(const char*)>([](const char* s){ cio_putstr(s); })
        : std::function<void(const char*)>([len](const char* s){
            int i = 0;
            for (; i < len && s[i]; ++i) cio_putch(s[i]);
            for (; i < len; ++i) cio_putch(' ');
        });
    printer(str);
}

Notes and cautions: both solutions avoid an explicit if statement but use conditional expressions; std::string allocates and std::function has overhead. For constrained or performance‑critical code, a simple if plus manual loops is usually clearer and faster.

Recommended Answers

All 9 Replies

I have no idea what you are talking about. What is "lazy evaluation" ? And why does it matter whether cio_putstr() returns void or something else ?

sorry was in a rush and forgot to ask., The question is how to rewrite the function w/o using if statement.
Any help would be appreciated

Um..."while" and "for" come to mind, just need to use a little creativity. Its not that hard.

What's a problem?

for (i = 0;
     len <= 0 && str[i] || str[i] && i < len;
     i++) {
    cio_putch(str[i]);
}
while (i++ < len)
    cio_putch(' ');

> To the OP: You say that this function - cio_putstr() returns a void . If this is so, then just why is it in your for loop as a test expression? And could you explain what it - cio_putstr() - is supposed to do?

> To Ancient Dragon: I read about "lazy evaluation" in some book, but I don't remember which one :( ... Anyway, here is wiki on it.

>just why is it in your for loop as a test expression?
That's a good question ;)
However there is an absolutely senseless condition with comma operator in that snippet, so it does not matter why ;)...

To Ancient Dragon: I read about "lazy evaluation" in some book, but I don't remember which one :( ... Anyway, here is wiki on it.

OMG I've been doing that for 50 years and always thought it was called procrastination. Now I know I was just plain lazy :)

a switch statement??

a switch statement??

What's a cool tip!
Which else C++ control statements are you familiar with?
;)

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.