This is the fourth day of my participation in the August Text Challenge.More challenges in August

directory

  • C language ternary operator introduction
  • C language ternary operator actual combat
    • 1. Use the ternary operator
    • 2. Use if/else conditions
  • Guess you like it

C/C++ learning Directory >> C language basics

C language ternary operator introduction

The ternary operator, also called conditional operator, is a computer language ([C] (https://www.codersrc.com/c%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80), C + +, Java, etc.) is an important part of. It is the only operator with three operands, so it is sometimes called a ternary operator, but the ternary operator is similar to if/else conditions.

The writing of the ternary operator:

// The trinary operator < expression 1>? < expression 2> : < expression 3>;Copy the code

Return value: evaluates expression 1 first, if true, executes expression 2 and returns the result of expression 2; If the value of expression 1 is false, expression 3 is executed and the result of expression 3 is returned.

For example: For the conditional expression B? If the value of b is true, then the calculation result of the expression x is returned. Otherwise, the value of y is evaluated and the result of the expression y is returned.

A conditional expression will never evaluate both X and y (just as in if/else conditionals, it is impossible to execute both if and else code).

Note: In C, result 1 and result 2 must be of the same type. Using the if/else condition for the above code also works:

/ / if/else conditions determine the if (< 1 > expression) {/ / < 2 > expression} else {/ / expression < 3 >}Copy the code

C language ternary operator actual combat

For the following code, we use the ternary operator and c language condition judgment respectively, as shown in the following example:

1. Use the ternary operator

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: apes said programming / / @ Blog (personal Blog address) : www.codersrc.com // @file :C language tri-order operator // @time :2021/05/28 08:00 // @motto: a thousand miles without a step, a small stream without a river or sea, the wonderful program life needs to be accumulated unrelentingly! /************************************************************************/ #include "stdafx.h" #include <stdio.h> int main() { int a = 1; int b = 2; int c = 0; // The trinary operator returns the integer c = a? (a + b) : (a - b); Printf (" integer c = %d\n",c); printf(" integer c = %d\n",c); // The trinary operator returns the string char *s = 0? "Condition held" : "Condition not held "; Printf (" string s = %s\n", s); printf(" string s = %s\n", s); Printf (" End of main! \n"); return 0; } /* Output result: integer c = 3 string s = condition not valid main function end! Press any key to continue.. */Copy the code

2. Useif / elseconditional

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: apes said programming / / @ Blog (personal Blog address) : www.codersrc.com // @file :C language tri-order operator // @time :2021/05/28 08:00 // @motto: a thousand miles without a step, a small stream without a river or sea, the wonderful program life needs to be accumulated unrelentingly! /************************************************************************/ #include "stdafx.h" #include <stdio.h> int main() { int a = 1; int b = 2; int c = 0; if (a) c = a + b; else c = a - b; Printf (" integer c = %d\n",c); char *s = NULL; // initialize if (0) // 0 equals false s = "conditional "; Else s = "condition not true "; Printf (" string s = %s\n", s); Printf (" End of main! \n"); return 0; } /* Output result: integer c = 3 string s = condition not valid main function end! Press any key to continue.. */Copy the code

Note:

  • 1. WhetherCorC++,false0These are equivalent,true1They are equivalent;
  • 2. The string placeholder is%s, the integer placeholder is%d, otherCLanguage variable type placeholders refer to:C format controller/placeholder

3.Guess you like

  1. Install Visual Studio
  2. Install the Visual Studio plug-in, Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 Uninstall
  5. Set the Visual Studio font/background/line number
  6. C Hello World
  7. C code comments
  8. C data type/variable type
  9. C language variable declaration and definition
  10. C format controller/placeholder
  11. C printf function
  12. C conditional judgment if/else
  13. C language logic operator
  14. C language ternary operator

C ternary operator

This article is published by the blog – Ape Say Programming Ape Say programming!