notes2

return 0 indicate that program ended successfully

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ternary conditional operator 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /* using the ternary conditional operator */ #include <stdio.h> /* function main begins program execution */ int main() { int score; /* declare variables */ printf( "\nEnter your score, and I will tell you\n" ); printf( "if you passed: " ); scanf( "%d", &score ); /* read integer */ printf( "%s\n", score >= 60 ? "You passed!!!" : "You failed." ); return 0; /* indicate that program ended successfully */ } /* end function main */ Logical Operators • && ( logical AND ) – Returns true if both conditions are true • || ( logical OR ) – Returns true if either of its conditions are true • ! ( logical NOT, also called logical negation ) – Reverses the truth/falsity of a statement Example Program 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /* logical operators, using the ternary conditional operator */ #include <stdio.h> int main() { int num1, num2, num3, num4; printf( "\nEnter two integers, and I will tell you\n" ); printf( "if they are both even: " ); scanf( "%d%d", &num1, &num2 ); /* read integer */ /* check if both integers are even */ num1 % 2 == 0 && num2 % 2 == 0 ? printf( "\nBoth integers are even.\n\n" ) : printf( "\nAt least one integer is not even.\n\n" ); Example Pro...
View Full Document

Ask a homework question - tutors are online