REV 00 Chapter 4 Selection Making Decision DDC

  • Slides: 7
Download presentation
REV 00 Chapter 4 Selection Making Decision DDC 1123 PENGATURCARAAN I (C) 1

REV 00 Chapter 4 Selection Making Decision DDC 1123 PENGATURCARAAN I (C) 1

REV 00 4. 1 Logical Data and Operator � Sometimes we need to test

REV 00 4. 1 Logical Data and Operator � Sometimes we need to test multiple conditions in order to make a decision. � Logical operators are used for combining simple conditions to make complex conditions. && is AND if ( x > 5 && y < 6 ) || is OR ! is NOT if (! (bob > 42) ) if ( z == 0 || x > 10 ) DDC 1123 PENGATURCARAAN I (C) 2

REV 00 4. 1 Logical Data and Operator � && ( logical AND )

REV 00 4. 1 Logical Data and Operator � && ( logical AND ) ◦ Returns true if both conditions are true � || ( logical OR ) ◦ Returns true if either of its conditions are true �! ( logical NOT, logical negation ) ◦ Reverses the truth/falsity of its condition ◦ Unary operator, has one operand � Useful as conditions in loops Expression true && false true || false !false Result false true DDC 1123 PENGATURCARAAN I (C) 3

REV 00 4. 2 Two Way Selection C supports two selection statement: If and

REV 00 4. 2 Two Way Selection C supports two selection statement: If and Swicth. n n if-else statement if (conditional_expression) { statement 1; } else { statement 2; } DDC 1123 PENGATURCARAAN I (C) 4

REV 00 4. 3 Multiway Seletion Nested if-else statements Syntax: If (conditional_expression 1) statement

REV 00 4. 3 Multiway Seletion Nested if-else statements Syntax: If (conditional_expression 1) statement 1; else if (conditional_expression 2) statement 2; else default. Statement; DDC 1123 PENGATURCARAAN I (C) 5

REV 00 4. 4 More Standard Library Function rand( ) �A function in C

REV 00 4. 4 More Standard Library Function rand( ) �A function in C standard library �Generates an integer between 0 and RAND_MAX srand( ) �srand seeds function rand to produce a different sequence of random numbers for each execution of a program �rand generates pseudo-random numbers DDC 1123 PENGATURCARAAN I (C) 6

REV 00 4. 5 A Menu Program if ( value == 0 ) {

REV 00 4. 5 A Menu Program if ( value == 0 ) { printf (“The value you entered was zero. n”) ; } else if ( value < 0 ) { printf (“%d is negative. n”, value) ; } else { printf (“%d is positive. n”, value) ; } DDC 1123 PENGATURCARAAN I (C) 7