0% found this document useful (0 votes)
56 views

CS 116 Computing Fundamentals: Boolean Operators Selection Dr. Christina Class

The document discusses Boolean operators and selection using if statements in C programming. It covers topics such as Boolean values of expressions, comparison operators, if/else conditional statements, and nested if statements. Examples are provided to demonstrate computing absolute values and selecting the maximum of three values using if/else logic. The document emphasizes proper syntax, readability, and avoiding dangling else issues.

Uploaded by

Firas Abu taleb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

CS 116 Computing Fundamentals: Boolean Operators Selection Dr. Christina Class

The document discusses Boolean operators and selection using if statements in C programming. It covers topics such as Boolean values of expressions, comparison operators, if/else conditional statements, and nested if statements. Examples are provided to demonstrate computing absolute values and selecting the maximum of three values using if/else logic. The document emphasizes proper syntax, readability, and avoiding dangling else issues.

Uploaded by

Firas Abu taleb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

CS 116 Computing

Fundamentals

Boolean Operators
Selection
Dr. Christina Class
Two important basics:
part 1

 All values have a Boolean meaning.


 0 is considered to be false
 all other values (≠ 0) are considered to
be true!

13.5  true
-27.86  true
0  false
15 - 3 * 5  false
‘x‘  true

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 2


Two important basics:
part 2
 Evaluating an expression yields a value (which
then can be interpreted as having a Boolean meaning).
 Sometimes, the evaluation can have a side effect.
 Some operators are mainly used for their side effects.
 example: assignment operator
 yields the value of the right side
 has the side effect that the value of the left operand is
changed to the yielded value

x = 3 + 4; returns value 7  true side effect: x is set to 7

y = x - 7; returns value 0  false side effect: y is set to 0

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 3


comparison operators
operator meaning operator meaning
== equal to > greater than
!= not equal to <= less or equal
< less than >= greater or
equal

Attention!

x == 5  true if and only if x has the value 5

x = 5  always true as 5 ≠ 0; the value of x is additionally set to 5

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 4


operators
 introduced so far:
 sign: +, -
 arithmetic operators: +, -, *, /, %
 assignment operators: =, +=, -=, *=, /=, %=
 comparison operators: ==, !=, <, >, <=, >=
 type cast: ( new_type )
 what are the values they yield to?
 which operators have side effects? what
kind of side effect?

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 5


type of operator yielded value side effect
sign value with none
application of the
sign
arithmetic value after none
application of
arithmetic operation
assignment value yielded by the the value of the
expression on the operand on the left
right side of the side is modified
assignment
x = 4 + 5 // 9
x += 5 // x + 5
comparison yields to 1 (true) or 0 none
(false)
typecast the casted value none

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 6


Selection with if
conditional statement
selection with if
if (expression) {
// statements are executed if expression is
// evaluated to be true (not equal to 0)
} else {
// statements are executed if expression is
// evaluated to be false (equal to 0)
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 8


example

compute and print the absolute value of an integer

int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
printf("the absolute value of x is: %d", x);
} else {
printf("the absolute value of x is: %d", x);
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 9


example

we can move the common part to after the selection  empty else

int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
} else {
}
printf("the absolute value of x is: %d", x);

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 10


example
Attention! This is not an empty else, the printf is only done in the
else case!!!

int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
} else
printf("the absolute value of x is: %d", x);

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 11


example
To have an empty else we could use an empty statement (;)

int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
} else
;
printf("the absolute value of x is: %d", x);

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 12


example

we can also not define the else part of a selection statement

int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
}
printf("the absolute value of x is: %d", x);

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 13


modified example

now we do not modify x, but only print the absolute value

int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
printf("the absolute value of x is: %d", x*-1);
} else {
printf("the absolute value of x is: %d", x);
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 14


modified example

if in the if and/or the else part of an if statement we only have one


single statement, we can remove the {} that indicate a block

int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0)
printf("the absolute value of x is: %d", x*-1);
else
printf("the absolute value of x is: %d", x);

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 15


Tip

 always write the {} in the if and


else statements
 use indentions to clearly mark the
blocks and improve readability of
your code

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 16


nested if
printing the maximum of three values
we know:

a>b

a > b and a > c

a > b and a ≤ c

a≤b

a ≤ b and b > c

a ≤ b and b ≤ c

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 17


programming style and
readability
compare the readability!

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 18


dangling else
#include <stdio.h>
what will be the output?
int main() or
{ which if does the else
int a = 5; part belong to?

if (a==5) {
printf("a is 5\n");
}
if (a==10) {
printf("a is 10\n");
}
else {
printf("else case");
}
return 0; it belongs to the second if
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 19


 dangling else
 an else always belongs to the latest if
that does not yet have an else part

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 20


else if

 C does not provide a specific


elseif / elif construct like some
other languages do
 if the else part of an if statement
only consists of an if statement, the
else and if are normally written in
the same line and the brackets {}
are omitted
 this makes the code more compact
without loosing readability 21
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 22
Exercise

 Write a program.
 read an integer value from the user
 output “the value is between 1 and
10” if it is > 1 and < 10
 output “the value is not between 1
and 10” if it is ≤ 1 or ≥ 10

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 23


solution
#include <stdio.h>

int main()
{ code duplication
int val;
scanf("%i", &val);
if(val > 1) {
if(val < 10) {
printf("the value is between 1 and 10\n");
} else {
printf("the value is not between 1 and 10\n");
}
} else {
printf("the value is not between 1 and 10\n");
}
return 0;
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 24


 some code on the previous slide
was duplicated because we tested
each part of the condition
separately
 we can use Boolean operators to
specifymeaning
Boolean BooleanC symbol
conditionsexample
operator
 and && a && b
 or || (x == 1) || (x > 5)
x == 1 || x > 5
 not ! !(x < y)

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 25


Attention
 & and | alone are also valid Boolean operators
on the Bit level! So don’t forget to write && or
|| respectively.
 you may write 1 < x < 10 but it will not have
the effect you expect:
 evaluation of 1 < x < 10
 associativity: 1 < (x < 10)
 depending on the value of x (x < 10) will either
be 1 (for true) or 0 (for false)
 in both cases it will be <= 1
 so 1 < x < 10 is false for all x

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 26


Operator Precedence

precedence associativity Operator


1 right to left ! +(sign) -(sign) &(addressop)
2 right to left (type)
3 left to right * / %
4 left to right + -
5 left to right < <= > >=
6 left to right == !=
7 left to right &&
8 left to right ||
9 right to left = += -= *= /= %=

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 27


Remember the first important
basics!

 All values have a Boolean meaning.


 0 is considered to be false
 all other values (≠ 0) are considered to
be true!

13.5  true
-27.86  true
0  false
15 - 3 * 5  false
‘x‘  true

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 28


Exercises

 on handout

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 4 29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy