Conditional Operator (?:) PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Conditional Operator (?:)


1
(No Transcript)
2
(No Transcript)
3
(No Transcript)
4
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
(No Transcript)
13
Conditional Operator (?)
  • Conditional operator (?) takes three arguments
    (ternary)
  • Syntax for using the conditional operator
  • expression1 ? expression2 expression3
  • If expression1 is true, the result of the
    conditional expression is expression2. Otherwise,
    the result is expression3

14
Conditional Operator (?)
  • if(agtb)
  • Maxa
  • else
  • Maxb
  • Max(agtb)?ab

15
  • switch ( test )
  • case 1 // Process for test 1 ...
  • break
  • case 5 // Process for test 5 ...
  • break
  • default // Process for all other cases.

16
  • switch works as follows-
  • The expression, just test in this case, is
    evaluated.
  • The case labels are checked in turn for the one
    that matches the value.
  • If none matches, and the optional default label
    exists, it is selected,
  • the break statement is normally added before the
    next case label to transfer control out of the
    switch statement.

17
include ltiostreamgt using namespace std int
main() char grade cout ltlt "Enter your grade
" cin gtgt grade cout ltlt endl switch
(grade) case 'A' cout ltlt "Your grade is
A." ltlt endl break case 'B' cout ltlt "Your
grade is B." ltlt endl break case 'C'
cout ltlt "Your grade is C." ltlt
endl break case 'F' cout ltlt "Your grade
is F." ltlt endl break default coutltlt" The
grade is invalid."ltltendl return 0
18
switch with break
  • include ltiostreamgt
  • using namespace std
  • int main()
  • Int place
  • cout ltlt "Enter your place "
  • cin gtgt place
  • cout ltlt endl
  • switch (place)
  • case 1 cout ltlt "we're first" ltlt endl
  • break
  • case 2 cout ltlt "we're second" ltlt endl break
  • default
  • cout ltlt "we're not first or second" ltlt endl
  • return 0

19
switch with break
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int place
  • cout ltlt "Enter your place "
  • cin gtgt place
  • cout ltlt endl
  • switch (place)
  • case 1 cout ltlt "we're first" ltlt endl
  • break
  • case 2 cout ltlt "we're second" ltlt endl
  • break
  • default
  • cout ltlt "we're not first or second" ltlt endl

20
switch with break
21
switch without break
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int place
  • cout ltlt "Enter your place "
  • cin gtgt place
  • cout ltlt endl
  • switch (place)
  • case 1 cout ltlt "we're first" ltlt endl
  • case 2 cout ltlt "we're second" ltlt endl
  • default
  • cout ltlt "we're not first or second" ltlt endl

22
(No Transcript)
23
What is the output
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int place
  • cout ltlt "Enter your place "
  • cin gtgt place
  • cout ltlt endl
  • switch (place)
  • case 1 cout ltlt "we're first" ltlt endl
  • case 1 cout ltlt "we're second" ltlt endl
  • default
  • cout ltlt "we're not first or second" ltlt endl

24
(No Transcript)
25
assert
  • qn/d
  • If d is 0,the program would terminate with error
    message stating that an illegal operation has
    occurred
  • Solution
  • assert(d)
  • qn/d
  • You need preprocessor directive includeltcassertgt
  • Assert statement identifies the expression where
    assertion failed ,the name of file containing the
    source code ,and the line number where the
    assertion failed
Write a Comment
User Comments (0)
About PowerShow.com