You are on page 1of 25

Decision and Branching

11/14/2022 1
Recap:Logical Operators: Precedence and Associativity
▪NOT has same precedence as unary operators (thus very high
precedence)
▪AND and OR have lower precedence than relational operators
▪OR has lower precedence than AND (important)
▪Associativity for logical operators is left to right

2 == 2 && 3 == 1 || 1==1 || 5==4


1 && 0 || 1 || 0
0 || 1 || 0 1 || 0 1
The Conditional Operator
▪ The conditional operator is of the form

Expression 1 ? Expression 2 : Expression 3


▪ Meaning: Evaluate expression 1, if it is true (non-zero), evaluate expression 2,
otherwise evaluate expression 3
▪ The operator generates the value of expression 2 or expression 3
▪ Often, we assign the result to another variable (a = exp1 ? exp2 : exp3)
▪ Data type of generated value ? Whichever of exp2 or exp3 is of higher type
▪ Precedence of cond. operator is just above assignment operators
▪ Associativity of cond. operator is right to left

3
Conditional Operator: Examples
■ z = (a > b) ? a : b; /* z = max(a, b) */
■ x = 5 ? 0:1; /* what is value of x */

11/01/2022 4
Conditional Operator: Examples
■ a = (i>0) ? 100 : 10; /* a will be 100
or 10 depending on i */
▪ a = (i>0)? 10.0 : 5; /* RHS result
will be a float */

11/01/2022 5
Conditional Operator: Examples
■ A sophisticated example (expression 1
consisting of multiple operators)
▪ c += ( a>0 && a<=10 ) ? ++a : a/b;
▪ The above will first evaluate a>0 && a<=10 and
then choose ++a or a/b
▪ Result from RHS will be added to c (c = c + result)

11/01/2022 6
Now our table is..
Note: Precedence of brackets () is above every other operator
HIGH
Operators Description Associativity

(unary) + -, ! Unary plus/minus, logical NOT Right to left

*/% Multiply, divide, remainder Left to right

+- Add, subtract Left to right


Precedence

< > >= <= Relational operators Left to right

== != Equal, not equal Left to right

&& Logical And Left to right

|| Logical Or Left to right

?: Conditional Right to left

= Assignment Right to left

LOW
7
Quiz
0 <= 10 <= 4

0 <= 10 && 10 <= 4

11/01/2022 8
Statements
■ An expression such as x = 0 or j++ becomes
a statement when it is followed by a semicolon ;
■ Braces { and } are used to group declarations
and statements together into a compound
statement.
❑ { int j; j=2+3; j++; } /*this entire thing now is a statement */

■ Compound statement is also called block.

11/14/2022 9
Compound Statement
■ Variables can be declared in any block.
Discussion of this is deferred.
■ There is no semicolon after the right brace
that ends a block.

11/14/2022 10
If-Else
■ The if-else statement is used to express decisions.
■ Formally the syntax is,
❑ if (expression)
statement1
else
statement2
▪ The else part is optional
▪ The expression is evaluated; If it is true (non-zero) then
statement1 is executed, otherwise (if there is an else part)
statement2 is executed.
11/14/2022 11
If-Else
■ if (x != 0) is same as if (x)

■Check ■Check
int y = 5, k = 10; int y = 5, k = 10;
if (y == 5) if (y == 5)
{ k ++; k ++;
j = k * k; j = k * k;
}
else else
j = k; j = k;
11/14/2022 12
If-Else
■ Check ■ Syntax : if
❑ x = 0; if (2 (expr) statement
!= 1+1) ; x=
5; ■ ; /* a null statement*/
printf(“%d”,x);
■So the output is 5

11/14/2022 13
If-Else
■ Check ■What is the output?
❑ int j = 200; A
if (j = 5) ■ because j=5 has value 5
printf(“A”); which is non-zero(true)
else
printf(“B”);
■This is a common pit-fall. Beware!
11/14/2022 14
If-Else
■ if( n > 0 )
■ if( n > 0 ) if
{ if ( a > b)
( a > b)
z = a;
z = a; else z = b;
else z= }
b;

■else associates with the closest previous


else-less if.
11/14/2022 15
If-Else ■ if( n > 0 )
{ if ( a > b)
■ if( n > 0 ) z = a;
if ( a > b) z= else z = b;
a; }
else z = b; else
else {
z = c; z = c;
}

11/14/2022 16
If-Else ladder
■ if (exp1)
stmt1{
}
else { if (exp2) ■These are useful for
stmt2 multi-way decisions
else if (exp3)
stmt3
else
}
stmt4
11/14/2022 17
If-Else Ladder ■Only one statement in the
ladder is executed
■ if (exp1) ■Ifexp1 is true then stmt1
stmt1 is executed and all other
else if (exp2) in the ladder are ignored.
stmt2 ■Ifexp1 is false and exp2
else if (exp3) is true then only stmt2 is
stmt3 executed
else ■Stmt4 can be seen as a
stmt4 default
11/14/2022 18
Switch
■ The switch statement is a multi-way
decision that tests whether an expression
matches one of a number of constant integer
values, and branches accordingly.
■ switch (expression) { case
const-expr : statements case
const-expr : statements default :
statements }
11/14/2022 19
Switch
int j; $./a.out
scanf (“%d”, &j); 0
switch(j) {
zero
case 0: printf(“ zero\n”);
case 1: printf(“ one\n”); one
case 2: printf(“ two\n”); two
default: printf(“ other\n”); other
■ switch
} simply transfers control once
$ to the
matching case.
11/14/2022 20
breaking a switch
■ break; /*this is a statement which can
break a switch */
■ break exits the switch block.
■ break can be used with other control flow
structures, but discussion is deferred.

11/14/2022 21
Use break statements $./a.out
0
int j;
zero
scanf (“%d”, &j);
$./a.out
switch(j) {
1
case 0: printf(“ zero\n”);
one
break;
case 1: printf(“ one\n”); $./a.out
break; 2
case 2: printf(“ two\n”); two
default: printf(“ other\n”); other
}
11/14/2022 $ 22
Switch
■ default: statements /*optional*/
■ The control is transferred to default, if it exists and none of
the cases matches the expression value.
■ Even if there are multiple statements to be executed in
each case there is no need to use { and } (i.e., no need
for a compound statement as in if-else).
■ One can not have something like case j <= 20:
■ All that we can have after the case is a constant
expression.

11/14/2022 23
switch
■ You can also use char values.
■ char c ; c = getchar ( );
switch (c)
{
case ‘a’:
case ‘A’: printf(“apple”); break;
case ‘b’:
case ‘B’: printf(“banana”); break;
}

■Empty cases might be useful


11/14/2022 24
goto
■ goto label; /* label is similar
identifier like a variable name
*/
/* this transfers control
to */
label:

11/14/2022 25

You might also like