then the print statement is ignored and nothing is printed If suppose that in

Then the print statement is ignored and nothing is

This preview shows page 3 - 6 out of 11 pages.

then the print statement is ignored and nothing is printed. If suppose that in addition to printing PASS we would like to add   5 bonus marks then we would write if (marks > 75) { marks = marks + 5 ; printf (“PASS\n”) ; } As seen above if there is more than one statement (compound statement) to be done after checking the expression result, then the statements should be enclosed between parenthesis . If there is only one statement (simple statement) to be executed after if then the parenthesis becomes optional (you can remove it). Note that there is no semi-colon at the end of the if or else statement . Two-Way Selection Using the if…else statement: Syntax : if (expression) statement 1; else statement 2; Page 3 of 11
Image of page 3
If the expression  is true , then  the   statement  1  is executed;  otherwise  the   statement  2  is executed  if   expression  is false . For example,  to find the  larger  of two variables   x and y and print the answer and assign the larger value to max variable and smaller value to min variable, we would write : if ( x > y ) { max = x ; min = y ; printf (“ x is BIG\n”) ; } else { max = y ; min = x ; printf (“ y is BIG\n”) ; } Check  for the  parenthesis  used  for more  than  one  statement  after   if  and   else . Multiple Alternative Decision Using the if ladder: Syntax: if ( expression 1 ) statement 1; else if ( expression 2 ) statement 2; . . else if ( expression n ) statement n; else statement e; The expressions in a multiple-alternative decision are evaluated in sequence until a true condition is reached. If an expression is true , the statement following it is executed, and the rest of the multiple-alternative decision is skipped. If an Page 4 of 11
Image of page 4
expression is false , the statement following it is skipped, and the next expression is checked. If all the expressions are false , the statement e following the final else is executed.
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 11 pages?

  • Spring '10
  • zaman
  • Equals sign, relational operator, printf

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes