Lecture04 - Copy
Lecture04 - Copy
University of Ottawa
Winter 2025
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
In-Class Exercise: Go To Brightspace
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Outline
2 Logical Expression
3 Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Variable Assignment
Outline
2 Logical Expression
3 Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
More on Variable Assignment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Logical Expression
Outline
2 Logical Expression
3 Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Logical Expression
Logical Expressions
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Logical Expression
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Logical Expression
Precedence of Operators
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Logical Expression
Note
It is very risky (i.e., very bad) using “==” to compare two floating-point
numbers to see if they are equal. Check how this code prints:
float a=1.777777777;
printf("%d\n", a==1.777777777);
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
Outline
2 Logical Expression
3 Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
if statement
if (S)
DoX;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
#include <stdio.h>
#include <math.h>
int main()
{
double x;
printf("Enter a positive value (or a negative number to quit)\n");
scanf("%lf", &x);
if (x>=0)
printf("The square root of %f is %f\n", x, sqrt(x));
return 0;
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
if-else statement
if (S)
DoX;
else
DoY;
S is a logical expression
“DoX;” and “DoY;” can be a single statement, or a block of
statements.
If “DoX” or “DoY;” is a block of statements, the block needs to be
enclosed by a pair of curly brackets “{ }”.
Indent DoX and DoY!
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
#include <stdio.h>
#include <math.h>
int main()
{
double x;
printf("Enter a positive value\n");
scanf("%lf", &x);
if (x>=0)
printf("The square root of %f is %f\n", x, sqrt(x));
else
printf("Invalid input!\n");
return 0;
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Decision Structures
Coding Demonstration
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .