Example - egecprogramlama

Introduction to Computing
Lecture 04:
Booleans & Selection
Assist.Prof.Dr. Nükhet ÖZBEK
Ege University
Department of Electrical & Electronics Engineering
[email protected]
Topics
• Booleans
• if and else statements
Boolean
• A special “type” with only two values:
false and true
• Used to implement the conditions for
selection and looping in an algorithm
Boolean Expressions
• ...are expressions that can be evaluated to
either strictly true or strictly false
• A Boolean expression which evaluates to
true has integer value 1; otherwise, 0
Boolean Operators
• ... are used in forming Boolean
expressions
• ... are also known as “logical” or
“relational” operators
– Not (!)
– Equality (==)
– Inequality (!=)
– Comparison (<, >, <=, >=)
– And (&&)
– Or (||)
Not
• True only if the single Boolean argument
is false
Examples:
! 1
! 0
! happy
Equality
• True only if both arguments have
identical values
Examples:
1 == 2
1 == 0
42 == 42
truth == beauty
Equality
• True only if both arguments have
identical values
Examples:
1 == 2
not to be
confused with
assignment
(=)
1 == 0
42 == 42
truth == beauty
Inequality
• True only if arguments have different
values
Examples:
1 != 2
1 != 0
42 != 42
num != 54
Comparison
• True only if the specified relationship
holds
Examples:
1 < 2
0 > 1
42 <= 42
age >= 18
Comparison
• True only if the specified relationship
holds
Examples:
1 < 2
Careful!
0 > 1
42 <= 42
age >= 18
And
• True only if both Boolean arguments are
true
Examples:
1 && 2
0 <= n && n <= 100
1 && 0 && -1
And
• True only if both Boolean arguments are
true.
Examples:
1 && 2
not to be
confused with
“bitwise
AND” (&)
0 <= n && n <= 100
1 && 0 && -1
Or
• True only if either Boolean argument is
true (or both are true)
Examples:
1 || 2
11 || 0 || 1
good || bad || ugly
Or
• True only if either Boolean argument is
true (or both are true).
Examples:
not to be confused
with “bitwise OR” (|)
1 || 2
11 || 0 || 1
good || bad || ugly
Truth Table
P
Q
!P
P && Q
P || Q
false
false
true
false
false
false
true
true
false
true
true
false
false
false
true
true
true
false
true
true
Precedence
• Highest to lowest:
– Brackets
– Not (!)
– Comparison (<, >, <=, >=)
– Equality (==)
Note: The assignment
– Inequality (!=)
operator (=) is lower in
– And (&&)
order of precedence
than Boolean / Logical
– Or (||)
operators
Example:
#include <stdio.h>
int main()
{
int
age
int
haveMoney
int
haveCard
float thirst
int
afterHours
int
=
=
=
=
=
18;
0;
1;
0.31;
1;
result;
result = age >= 18 && (haveMoney || haveCard)
&& thirst > 0.3 && ! afterHours;
printf("%d\n", result);
return 0;
}
bool.c
Example:
#include <stdio.h>
int main()
{
int
age
int
haveMoney
int
haveCard
float thirst
int
afterHours
int
=
=
=
=
=
18;
0;
1;
0.31;
1;
result;
result = age >= 18 && (haveMoney || haveCard)
&& thirst > 0.3 && ! afterHours;
printf("%d\n", result);
return 0;
}
0
bool.c
The if statement
• Determines whether a block is executed
• Implements the selection instructions
within an algorithm
• Decides what to do by evaluating a
Boolean expression
• If the expression is true (non-zero), the
block is executed
Example: oddnum.c
Read in a number, and print
it if it is odd.
output “Enter an integer”
input number
if (number is odd)
then
{
output the number
}
Example: oddnum.c
Read in a number, and print
it if it is odd.
#include <stdio.h>
/* Read in a number, and echo it
if it is odd. */
int main()
{
output “Enter an integer”
input number
if (number is odd)
then
{
output the number
}
return 0;
}
Example: oddnum.c
Read in a number, and print
it if it is odd.
#include <stdio.h>
/* Read in a number, and echo it
if it is odd. */
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
output “Enter an integer”
input number
if (number is odd)
then
{
output the number
}
return 0;
}
Example: oddnum.c
Read in a number, and print
it if it is odd.
#include <stdio.h>
/* Read in a number, and echo it
if it is odd. */
int main()
{
int number;
output “Enter an integer”
input number
printf("Enter an integer: ");
scanf("%d", &number);
if (number is odd)
then
{
output the number
}
if (number % 2 != 0)
{
printf("%d\n", number);
}
return 0;
}
Example: oddnum.c
Read in a number, and print
it if it is odd.
#include <stdio.h>
/* Read in a number, and echo it
if it is odd. */
int main()
{
int number;
output “Enter an
Dointeger”
not put
input number
printf("Enter an integer: ");
scanf("%d", &number);
if (number is odd)
then
{
output the number
}
if (number % 2 != 0)
{
printf("%d\n", number);
}
“then” here!
return 0;
}
Example: oddnum.c
#include <stdio.h>
Read in a number, and print
it if it is odd.
/* Read in a number, and echo it
if it is odd. */
int main()
{
int number;
output “Enter an integer”
input number
printf("Enter an integer: ");
scanf("%d", &number);
if (number is odd)
then
{
output the number
}
if (number % 2 != 0)
{
printf("%d\n", number);
}
Do not put
semicolonreturn
here!
}
0;
Example: oddnum.c
Read in a number, and print
it if it is odd.
#include <stdio.h>
/* Read in a number, and echo it
if it is odd. */
int main()
{
int number;
output “Enter an integer”
input number
printf("Enter an integer: ");
scanf("%d", &number);
if (number is odd)
then
{
output the number
}
if (number % 2 != 0)
{
printf("%d\n", number);
}
return 0;
}
Syntax for if statement
if (condition)
statement;
OR
if (condition)
{
statement1;
…
statementn;
}
Notes on if
• Which of the following code fragments are equivalent?
A
if (number % 2 != 0)
{
printf("%d", number);
}
printf(” is odd\n");
B
if (number % 2 != 0)
printf("%d", number);
printf(” is odd\n");
C
if (number % 2 != 0)
{
printf("%d", number);
printf(” is odd\n");
}
Notes on if
• Common mistakes
if (number % 2 != 0);
{
printf("%d is an odd ", number);
}
printf("number\n");
Notes on if
• Common mistakes
if (number % 2 != 0);
{
printf("%d is an odd ", number);
}
printf("number\n");
No semicolon here!
Notes on if
• Common mistakes
if (number = 0)
{
printf("%d\n", number);
}
printf("%d\n", number);
Notes on if
• Common mistakes
if (number = 0)
{
printf("%d\n", number);
}
printf("%d\n", number);
Should be
==
The else statement
• Can only occur after an if statement
• Is only executed when the if’s block does
not execute
Example: oddeven.c
Read in a number, and
determine if it’s odd or
even.
#include <stdio.h>
/* Determine whether an input number
is odd or even. */
main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
output “Enter an integer”
input number
if (number is odd)
then
{
output: number “ is an odd
number”
}
else
{
output: number “ is an even
number”
}
if (number % 2 != 0)
{
printf("%d is an odd number\n",
number);
}
}
Example: oddeven.c
Read in a number, and
determine if it’s odd or
even.
#include <stdio.h>
/* Determine whether an input number
is odd or even. */
main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
output “Enter an integer”
input number
if (number is odd)
then
{
output: number “ is an odd
number”
}
else
{
output: number “ is an even
number”
}
if (number % 2 != 0)
{
printf("%d is an odd number\n",
number);
}
else
{
printf("%d is an even number\n",
number);
}
}
Example: oddeven.c
Read in a number, and
determine if it’s odd or
even.
#include <stdio.h>
/* Determine whether an input number
is odd or even. */
main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
output No
“Enter
an integer”
semicolons
input number No semicolons
here! here!
if (number is odd)
then
{
output: number “ is an odd
number”
}
else
{
output: number “ is an even
number”
}
if (number % 2 != 0)
{
printf("%d is an odd number\n",
number);
}
else
{
printf("%d is an even number\n",
number);
}
}
Example: oddeven.c
Read in a number, and
determine if it’s odd or
even.
#include <stdio.h>
/* Determine whether an input number
is odd or even. */
main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
output “Enter an integer”
input number
if (number is odd)
then
{
output: number “ is an odd
number”
}
else
{
output: number “ is an even
number”
}
if (number % 2 != 0)
{
printf("%d is an odd number\n",
number);
}
else
{
printf("%d is an even number\n",
number);
}
}
Syntax for if & else statement
if (condition)
statementT;
else
statementF;
OR
if (condition)
{
statementT1;
…
statementTn;
}
else
{
statementF1;
…
statementFn;
}
Cascaded if statement
• Multiple alternative blocks each with a
Boolean expression
• First expression which evaluates to true
causes execution of the associated block
• Only at most one block will be executed
Example: months.c
Determine the number of days in
a given month:
30 days hath September,
April, June and November.
All the rest hath 31,
Excepting February alone,
Which hath 28 days clear,
And 29 in each leap year.
output “Enter an integer”
input month
if (month is September,
or April,
or June,
or November)
then
{
output “30 days”
}
else if (month is February)
{
output “28 or 29 days”
}
else
{
output “31 days”
}
Example: months.c
int main()
{
#include <stdio.h>
/*************************\
Determine the number of days
in a given month:
30 days hath September,
April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/
const
const
const
const
const
int
int
int
int
int
September = 9;
April = 4;
June = 6;
November = 11;
February = 2;
return 0;
}
Example: months.c
int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
/*************************\
Determine the number of days
in a given month:
30 days hath September,
April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/
const
const
const
const
const
int
int
int
int
int
September = 9;
April = 4;
June = 6;
November = 11;
February = 2;
return 0;
}
Example: months.c
int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
month==April ||
month==June ||
month==November )
{
printf("30 days\n");
}
/*************************\
Determine the number of days
in a given month:
30 days hath September,
April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/
const
const
const
const
const
int
int
int
int
int
September = 9;
April = 4;
June = 6;
November = 11;
February = 2;
return 0;
}
Example: months.c
int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
month==April ||
month==June ||
month==November )
{
printf("30 days\n");
}
/*************************\
Determine the number of days
in a given month:
30 days hath September,
April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/
const
const
const
const
if
const
int September = 9;
int April = 4;
int June = 6;
int November = 11;
(month==September
int
February = 2;
Common mistake:
|| April || June || November )
return 0;
}
Example: months.c
int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
month==April ||
month==June ||
month==November )
{
printf("30 days\n");
}
else if (month==February)
{
printf("28 or 29 days\n");
}
/*************************\
Determine the number of days
in a given month:
30 days hath September,
April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/
const
const
const
const
const
int
int
int
int
int
September = 9;
April = 4;
June = 6;
November = 11;
February = 2;
return 0;
}
Example: months.c
int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
month==April ||
month==June ||
month==November )
{
printf("30 days\n");
}
else if (month==February)
{
printf("28 or 29 days\n");
}
else
{
printf("31 days\n");
}
return 0;
/*************************\
Determine the number of days
in a given month:
30 days hath September,
April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/
const
const
const
const
const
int
int
int
int
int
September = 9;
April = 4;
June = 6;
November = 11;
February = 2;
}
Example: months.c
int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
month==April ||
month==June ||
month==November )
{
printf("30 days\n");
}
else if (month==February)
{
printf("28 or 29 days\n");
}
else
{
printf("31 days\n");
}
return 0;
/*************************\
Determine the number of days
in a given month:
30 days hath September,
April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/
“Default” block.
const
const
const
const
const
int
int
int
int
int
September = 9;
April = 4;
June = 6;
November = 11;
February = 2;
}
Example: months.c
int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
month==April ||
month==June ||
month==November )
{
printf("30 days\n");
}
else if (month==February)
{
printf("28 or 29 days\n");
}
else
{
printf("31 days\n");
}
return 0;
/*************************\
Determine the number of days
in a given month:
30 days hath September,
April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/
const
const
const
const
const
int
int
int
int
int
September = 9;
April = 4;
June = 6;
November = 11;
February = 2;
}
Syntax for cascaded if statements
if (condition1)
statement1;
else if (condition2)
statement2;
.
.
.
else if (conditionn)
statementn;
else
statemente;
Notes on Cascaded if
Q:
What is the output if:
• number is equal to 5
• number is equal to 20
• number is equal to 30
•number is equal to 35
if (number >= 30)
{
printf(“S1\n”);
}
else if (number <= 40)
{
printf(“S2\n”);
}
else if (number >= 10)
{
printf(“S3\n”);
}
else if (number <= 20)
{
printf(“S4\n”);
}
Common Mistakes
• Using = instead of ==
Example:
#include <stdio.h>
/* Probably the most common C error. */
int main()
{
int score;
scanf("%d", &score);
if (score = 48 || score = 49)
{
printf("Almost!\n");
}
return 0;
}
Example:
#include <stdio.h>
/* Probably the most common C error. */
int main()
{
int score;
scanf("%d", &score);
if (score = 48 || score = 49)
{
printf("Almost!\n");
Usual error message
}
return 0;
}
(if any):
“LValue required...”
Example:
#include <stdio.h>
/* Probably the most common C error. */
int main()
{
int score;
scanf("%d", &score);
if (score == 48 || score == 49)
{
printf("Almost!\n");
}
return 0;
}
Common Mistakes
• Using = instead of ==
• Multiple comparisons
Example:
#include <stdio.h>
/* Another common C error. */
int main()
{
int score;
scanf("%d", &score);
if ( 0 < score < 48 )
{
printf("Fail\n");
}
return 0;
}
Example:
#include <stdio.h>
/* Another common C error. */
int main()
{
int score;
scanf("%d", &score);
if ( 0 < score < 48 )
{
printf("Fail\n");
0 or 1
}
return 0;
}
Example:
#include <stdio.h>
/* Another common C error. */
int main()
{
int score;
always 1
scanf("%d", &score);
if ( 0 < score < 48 )
{
printf("Fail\n");
0 or 1
}
return 0;
}
Example:
#include <stdio.h>
/* Another common C error. */
int main()
{
int score;
scanf("%d", &score);
if ( 0 < score && score < 60 )
{
printf("Fail\n");
}
return 0;
}
Summary
• Booleans
• Boolean expressions
• Boolean Operators
• Precedence
• if and else statements
• Common mistakes