Chapter 5 Selection Statement Selection Statement Definition A

  • Slides: 20
Download presentation
Chapter 5: Selection Statement

Chapter 5: Selection Statement

Selection Statement: Definition § A selection statement causes the program control to be transferred

Selection Statement: Definition § A selection statement causes the program control to be transferred to a specific flow based upon whether a certain condition is true or not.

Logical Expression § if statement must test the value of an expression § Relation

Logical Expression § if statement must test the value of an expression § Relation operator is used along with if § A=10; B=20;

The if statement § If statement allows a program to choose two alternatives by

The if statement § If statement allows a program to choose two alternatives by testing the value if a expression § Syntax: if (expression) statement; Eg: a=10; b=20; if(a>b) printf(“A is greater than b”); if(b>a) printf(“ B is greater than a”); if(a==b) printf(“A and B are equal”);

The if statement contd… § For compound statement used braces § Syntax: if (expression)

The if statement contd… § For compound statement used braces § Syntax: if (expression) { statement 1; Statement 2; Statement 3; …. . ; ……; }

The else Clause § An if statement may have an else clause § Syntax:

The else Clause § An if statement may have an else clause § Syntax: if(expression) statement; else statement; § Example: if(i>j) max=i; else max=j;

Nested if § if statement inside if is called nested if § Example:

Nested if § if statement inside if is called nested if § Example:

Cascaded if § We often need series of test condition stopping them when one

Cascaded if § We often need series of test condition stopping them when one is true

Logical Operator § An operator that acts on binary value to produce a result

Logical Operator § An operator that acts on binary value to produce a result according to the laws of Boolean logic (e. g. the AND, OR, and NOT functions). § Logical operator reduces Lines of Code in selection statement § If A holds 1 and B holds 0 then,

Logical Operator Example #1

Logical Operator Example #1

Logical Operator Example #2

Logical Operator Example #2

Logical Operator Example #3

Logical Operator Example #3

Conditional Operator § Conditional operator use two symbols ? and : § Syntax: expression

Conditional Operator § Conditional operator use two symbols ? and : § Syntax: expression 1 ? Expression 2 : expression 3 § Unique C operator, needs three operands (ternary operator) § Read as if expression 1 then expression 2 else expression 3

Conditional Operator Example#1

Conditional Operator Example#1

Conditional Operator Example#2

Conditional Operator Example#2

Conditional Operator Example 3

Conditional Operator Example 3

The Switch Statement § Switch the statement according to case § Better that using

The Switch Statement § Switch the statement according to case § Better that using cascading if

Syntax

Syntax

The Switch Statement Example

The Switch Statement Example