Mukezh

Session 7 Conditional Operator

Dec 17th, 2020 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. CONDITIONAL OPERATOR ( ? : ) or Ternary Operator
  2. >> It Requires 3 Argument Arg1 ? Arg2 : Arg3
  3. >> If Arg1 is true, Whole statement is replacement by Arg2.
  4. Arg1(True) ? Arg2 : Arg3 >>>>>>> Arg2
  5. >> If Arg1 is False, Whole statement is replacement by Arg3.
  6. Arg1(False) ? Arg2 : Arg3 >>>>>>> Arg3.
  7.  
  8. True = Non-Zero
  9. False = Zero
  10.  
  11. a= 15?1:2 ; 1
  12. a=10.5?45:78; 45
  13. a= 0?1:2; 2
  14. a=3+0?23:43; 23
  15. a= 10 ?20:30; 20
  16. a= 100?0:200; 0
  17. a=1.5 ? 100:200; 100
  18. a=0?1.2:300; 300
  19. a=3?10:20; 10
  20. a= 3+0?10:20; 10
  21. a=5?10+5:5+15; 15
  22. a=0?5+3:2-5; -3
  23.  
  24.  
  25. Priority
  26. 1. ! Unary
  27. 2. * / % Binary
  28. 3. + - Binary
  29. 4. > < >= <= Binary
  30. 5. == != Binary
  31. 6. && Binary
  32. 7. || Binary
  33. 8. ?: Ternary
  34. 9. = Binary
  35.  
  36.  
  37. if(a>b)
  38. {
  39. max=a;
  40. }
  41. else Conditional >>> max= b>a(False)?b :a; max=a;
  42. { Bitwise Operator>>> max=a+((a-b)>>15)*(a-b);
  43. max=b;
  44. }
  45.  
  46.  
  47.  
  48.  
  49. if(a>b && a>c)
  50. {
  51. max=a; max= a>b&&a>c ?a : b>c ? b : c;
  52. } max= a>b&&a>c?a:b>c?b:c;
  53. else
  54. {
  55. if(b>c)
  56. {
  57. max=b;
  58. else
  59. {
  60. max=c;
  61. }
  62. }
  63. max= a>b&&a>c? a:b>c?b:c;
  64.  
  65. a= 8>3?4>7?10:4>7?9>10?20:30:4>7?40:50:6>8?9>1?10:70:80; 50
  66.  
  67. Step 1: Count No. of ? and : . If both are Equal then go to next step
  68. Step 2: Goto 1st ( : ) and Match to the nearest left ( ? ) and Repeat
  69. Step 3: All ? and : Matched then there is no error
  70.  
  71. a=6>8?2>1?10:3>2?20:30:6>4?4>8?40:6>2?50:60:70; 50
  72.  
  73.  
  74. if(x>y)
  75. {
  76. printf("A");
  77. printf("B"); Statement
  78. }
  79. else
  80. {
  81. printf("C");
  82. printf("D");
  83. }
  84. x>y ? printf("A");printf("B");:printf("C");printf("D");; Invalid
  85. Arg1? Arg2 : Arg3; Syntax
  86. Note : Every Conditional opr can be converted to if else but vice versa not possible.
  87.  
  88.  
  89. a=10,b=20;
  90. c = a && b = 30;
  91. printf("%d %d %d",a,b,c); lvalue
  92. *****************************************
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
Add Comment
Please, Sign In to add comment