Question about programming syntax

what this part of the code means? why question mark and what “:Null” after print action do?

(seconds < 10) ?display.print("0"):NULL;

I am trying this code that I found on GitHub and I will use this timer as a subpart of my project, the simulation runs perfectly. here is the full code:

/*********************************************************************
  This is an example sketch for a countdown timer using Nokia 5110 LCD
  and adafruit library for the display

  RWSDev.net
*********************************************************************/

#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

// pin 7 - SCE
// pin 6 - RST
// pin 5 - D/C
// pin 4 - DN
// pin 3 - SCLK

// set timer position on lcd
int xPos = 16;
int yPos = 16;

int hours = 0;
int minutes = 1;
int seconds = 10;

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup()   {
  display.begin();
  display.clearDisplay();   // clears the screen and buffer
  display.display();
}

void loop() {
  while (hours > 0 || minutes > 0 || seconds >= 0) {
    display.clearDisplay();   // clears the screen and buffer
    display.setContrast(30);
    display.setCursor(xPos, yPos);
    (hours < 10) ? display.print("0") : NULL;
    display.print(hours);
    display.print(":");
    (minutes < 10) ? display.print("0") : NULL;
    display.print(minutes);
    display.print(":");
    (seconds < 10) ? display.print("0") : NULL;
    display.print(seconds);
    display.display();
    stepDown();
    delay(1000);
  }
}

void stepDown() {
  if (seconds > 0) {
    seconds -= 1;
  } else {
    if (minutes > 0) {
      seconds = 59;
      minutes -= 1;
    } else {
      if (hours > 0) {
        seconds = 59;
        minutes = 59;
        hours -= 1;
      } else {
        trigger();
      }
    }
  }
}

void trigger() {
  display.clearDisplay();   // clears the screen and buffer
  display.setTextSize(1);
  display.setCursor(xPos, yPos);
  display.println("Time over");
  display.display();
}
(seconds < 10) ?display.print("0"):NULL;

same

if(seconds < 10)
{
display.print("0");
}
else
{
// do nothing (NULL)
}

That (?:) is called the conditional or ternary operator.

amnahbukair:
what this part of the code means? why question mark and what “:Null” after print action do?

(seconds < 10) ?display.print("0"):NULL;

That is the ternary operator, and is equivalent to:

    if (seconds < 10) 
      display.print("0");
    else
      NULL;

Note that this is an incorrect use of NULL, and will generate warning messages from the compiler if you have the IDE set to display all warnings.

/home/pi/Arduino/forumTest/forumTest.ino: In function 'void loop()':
/home/pi/Arduino/forumTest/forumTest.ino:39:41: warning: converting to non-pointer type 'unsigned int' from NULL [-Wconversion-null]
     (hours < 10) ? display.print("0") : NULL;
                                         ^~~~
/home/pi/Arduino/forumTest/forumTest.ino:42:43: warning: converting to non-pointer type 'unsigned int' from NULL [-Wconversion-null]
     (minutes < 10) ? display.print("0") : NULL;
                                           ^~~~
/home/pi/Arduino/forumTest/forumTest.ino:45:43: warning: converting to non-pointer type 'unsigned int' from NULL [-Wconversion-null]
     (seconds < 10) ? display.print("0") : NULL;
                                           ^~~~

That particular line of code prints a leading “0” if the number of seconds is from 0 through 9.

Somebody wanted to see if he understood “ternary”. Completely wrong.
Just "

if (hours <10) display.print ('0');

And that for all 3 cases.

display.print ((hours <10) ? "0" : ""); :smiley:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.