, <=, >=, ==, and !=. It also covers logical operators like &&, ||, and !. It provides examples of using these operators in expressions and evaluating boolean values. It describes using while loops with counter-controlled iteration to repeat actions while a condition remains true.">, <=, >=, ==, and !=. It also covers logical operators like &&, ||, and !. It provides examples of using these operators in expressions and evaluating boolean values. It describes using while loops with counter-controlled iteration to repeat actions while a condition remains true.">
Week4 Combine
Week4 Combine
Given int a = 3, b = 8, c = 1;
what is value of following expressions?
precedence order
high to low
precedence than binary
operators ! Not
&& And
These 2 operators have lower || Or
precedence than relational
operators which in turn have
lower precedence than
arithmetic operators
Understanding AND Logic (1/3)
10
x y x && y Value
true true true 1
true false false 0
false true false 0
false false false 0
Understanding AND Logic (3/3)
12
x y x || y Value
true true true 1
true false true 1
false true true 1
false false false 0
Understanding OR Logic (3/3)
15
x !x Value
true false 0
false true 1
Precedence Table: Arithmetic,
17
Relational, and Logical Operators
Operator(s) Description
! + - Logical negation, unary plus and minus
* / % Multiplication, division, remainder
precedence order
high to low
+ - Addition, subtraction
Less than, less than or equal to, greater
< <= > >=
than, greater than or equal to
== != Equal to, not equal to
&& Logical AND
|| Logical OR
Relation and Logical Expressions
18
(1/2)
Expression Value
!('A' > 'B')
!(6 <= 7)
!7 > !6 !0 is true !7 means false
1
!(a > b)
a < 10 < b
a < 10 && 10 < b
true false
0
!a != !b 0
0
0
HIGH-LEVEL PROGRAMMING I
while Loop by Prasanna Ghali
Iteration Structure
2
expression statement
true
false
while (expression)
statement
statement or
block of statements delimited by braces
while Loop (1/2)
3
int i = 0; 1
statement
i += 5; 3
} // update loop control variable
output
}
0 5 10 15
while Loop (2/2)
4
int i = 0;
while (i <= 20) {
print("%d ", i);
i += 5;
}
Type 1: Counter-Controlled while
5
Loops
You know how many times certain things need
to be done
// initialize N to specify how many
// times certain things need to be done
#include <stdio.h>
#include <stdio.h>
int main(void) {
int main(void) {
int ch = getchar();
int ch;
while (ch != EOF) {
while ((ch = getchar()) != EOF) {
putchar(ch);
putchar(ch);
ch = getchar();
}
}
return 0;
return 0;
}
}
Infinite Loops
13
int i = 0;
Example 2: while (i < 10);
printf("i is %d\n", ++i);
while (1)
Example 3: printf("Infinite loop ...\n");
HIGH-LEVEL PROGRAMMING I
Conditional Operator by Prasanna Ghali
Reference
2
if (x > y) {
z = x + 2;
} else {
z = y + 5;
}
z = x > y ? x + 2 : y + 5;
Conditional Operator and
10
Expression
Write function signum that returns 1, -1, or 0
depending on whether its argument is positive,
negative, or zero
int signum(int x) { int signum(int x) {
if (x > 0) return 1; if (x > 0) return 1;
else if (x < 0) return -1; if (x < 0) return -1;
else return 0; return 0;
} }
int signum(int x) {
return (x > 0) ? 1 : (x < 0) ? -1 : 0;
}
Computing Maximum of 3 Integral
11
Values (1/3)
if (x > y) {
if (x > z) {
max = x;
} else {
max = z;
}
} else {
if (y > z) {
max = y;
} else {
max = z;
}
}
max = x > y ? x>z ? x : z : y>z ? y : z;
Computing Maximum of 3 Integral
12
Values (2/3)
Statement a b c
c = a == b ? a + 2 : b + 5;
c = a = b ? a + 2 : b + 5;
c = a = b ? a + 2 : b += 5;
c = (a = b) ? (a + 2) : (b += 5);
HIGH-LEVEL PROGRAMMING I
for Looping Structure by Prasanna Ghali
while Loop Structure
2
expression statement
true
false
while (expression)
statement
for Loop Structure (1/4)
3
true
expression statement update expression
initial expression;
false
while (expression) {
statement
update expression;
}
for Loop Structure (2/4)
4
initial
expression
true
expression statement update expression
initial expression;
false while (expression) {
statement
update expression;
}
for Loop Structure (3/4)
5
1
goto step 2 5 4
2
execute statement if evaluation of
expression is true evaluate expression
otherwise, jump out of loop 3
for Loop Structure (4/4)
6
int i = 0;
while (i <= 20) {
printf("%d ", i);
i += 5;
}
for (; ;) {
printf("This is infinite loop ...\n");
}
expression expression
true true false
false
statement 1 statement 2
statement
if (expression) if (expression)
statement statement 1
else
statement or statement 2
block of statements delimited by braces
Meaning of C/C++ Statement
3
expression followed by ;
Zero or more statements enclosed in
opening brace { and closing brace }
{
{
// empty block
w = x2 - x1;
}
h = y2 - y1;
d = sqrt(w*w + h*h);
}
One Way Selection: if Statement
4
C/C++ Syntax:
if (expression)
statement expression
true false
statement
Common error:
Always true!!! Result of assignment expression is 5
if (health = 5)
printf("You are dead!\n");
More saner:
if (5 == health)
printf("You are dead!\n");
if Statement: Syntax (1/4)
7
if (a > b) {
printf("a = %d, ", a);
printf("b = %d\n", b);
}
if Statement: Syntax (3/4)
9
if (a > b) if (a > b) {
;
}
Good habit to put single statement in braces!!!
if (a > b) {
printf("a = %d, b = %d\n", a, b);
}
if Statement: Syntax (4/4)
10
if (health == 0);
printf("You are dead!\n");
C/C++ Syntax:
if (expression) expression
statement 1
else true false
statement 2 statement 1 statement 2
int average;
printf("What is your score for HLP1?: ");
scanf(" %d", &average);
...
if (d <= 30.0) {
velocity = 0.425 + 0.00175*d*d;
} else {
velocity = 0.625 + 0.12*d - 0.0025*d*d;
}
Multiple Selections: Nested if
14
(1/7)
Some problems require implementation of
more than two alternatives
Calculating interest on your bank balance
braces!!!
Everything in braces becomes one (compound)
statement, so inner if is "hidden" from else