Skip to main content

Understanding the Java Ternary Operator and Modulo Operator: Examples That Print Odd Numbers

The Java programming language provides several operators that can be used to perform arithmetic, comparison, and logical operations. In this blog post, we will discuss two of these operators: the ternary operator and the modulo operator. We will use these operators to write code examples that print odd numbers.

The Ternary Operator

The ternary operator is a shorthand way of writing an if-else statement in Java. It has the following syntax:

condition ? expression1 : expression2

The "condition" is evaluated, and if it is true, "expression1" is returned. If "condition" is false, "expression2" is returned. Here is an example of using the ternary operator to print odd numbers:

for (int i = 1; i <= 10; i++) {
  int result = i % 2 == 0 ? 0 : i;
  System.out.println(result);
}

In this example, the "condition" is "i % 2 == 0", which checks if "i" is even. If "i" is even, "expression1" is 0. If "i" is odd, "expression2" is "i". Therefore, the value of "result" will be 0 for even numbers and the value of "i" for odd numbers.

The Modulo Operator

The modulo operator, denoted by the "%" symbol, is used to find the remainder of a division operation. Here is an example of using the modulo operator to print odd numbers:

for (int i = 1; i <= 10; i++) {
  if (i % 2 != 0) {
    System.out.println(i);
  }
}

In this example, the "if" statement checks if "i" is odd by using the modulo operator to find the remainder of "i" divided by 2. If the remainder is not 0 (i.e., "i" is odd), the value of "i" is printed to the console.

Conclusion

The ternary operator and modulo operator are two useful tools in the Java programmer's toolbox. By using these operators to write code examples that print odd numbers, we have demonstrated their practical applications. We hope this blog post has been helpful in understanding the purpose and usage of the ternary and modulo operators in Java.

Happy coding!

Comments

Popular posts from this blog

a simple example for jdbc PreparedStatement

a simple example for PreparedStatement package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; Statement stmt=null; ResultSet rs=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql="select * fr...

Server-Side Pagination with React-Table and Spring Boot JPA with H2 Database

Pagination is a common technique used to split large amounts of data into smaller, more manageable chunks. With server-side pagination, data is retrieved from the server in smaller batches, reducing the amount of data transferred over the network and improving application performance. React-Table provides a wide range of built-in features such as sorting, filtering, pagination, row selection, and column resizing. These features can be easily configured and customized to fit specific requirements. For example, you can customize the sorting behavior to handle multiple sorting criteria, or you can add custom filters to the table to handle complex data filtering scenarios. Additionally, React-Table provides a flexible API that allows developers to extend its functionality with custom hooks, plugins, and components. This means that you can easily add custom functionality to the table, such as exporting data to CSV or integrating with external data sources. In terms of styl...