computer methods c programming
computer methods c programming
LECTURE 5
logic_value = (5>1);
printf("(5>1) is %f\n", logic_value);
logic_value = (7>3);
printf("(7>3) is %f\n", logic_value);
logic_value = ((2+4)>=(12/3));
printf("((2+4)>=(12/3)) is %f\n", logic_value);
logic_value = (4 <5);
printf("(4 <5) is %f\n", logic_value);
logic_value = (4<=6);
printf("(4<=6) is %f\n", logic_value);
logic_value = (5==5);
printf("((5==5) is %f\n", logic_value);
return 0;
}
(5>1) is 1.000000
(7>3) is 1.000000
((2+4)>=(12/3)) is 1.000000
(4 <5) is 1.000000
(4<=6) is 1.000000
((56/7)!= 8) is 0.000000
((5==5) is 1.000000
P Q P&&Q P||Q P !P
!5 evaluates to 0
!0 evaluates to 1
!’t’ evaluates to 0
3+!x evaluates 3 if x is non zero, and
evaluates to 4 if x is zero
!!5 evaluates to 1
!!0 evaluates to 0
-5 && !10 evaluates to 0
10 || -10 evaluates to 1
The first operand is used to determine which of the other two operands should
be evaluated. If the first expression delivers logical true(non-zero) the the result
of the expression is expression2; if the first expression delivers logical
false(zero) the the result of the expression is expression3.
Exercise: define a C function to compute the absolute value of a floating point
number.
( The absolute value is mathematically defined by
x, if x ≥ 0
|x| =
−x, if x < 0
Solution:
double absolute(double x)
{
return (x<0 ? -x:x);
}
if (expression)
statement
A statement if is used to execute
conditionally a statement or block
of code.
If expression is true, statement is
executed (what is true?).
statement can be replaced by a
block of statements, enclosed in
curly braces.
int main()
{
double num;
return 0;
}
if (expression)
statement2
else
statement3
/*******************************************************
* cm11_4_5.c- The following program receives as input *
* a number and checks if it is a valid grade means *
* it is between 0 and 100 *
*******************************************************/
#include <stdio.h>
int main(void)
{
int grade;
return 0;
}
return 0;
}
switch (expression)
{
The switch statement is a multiway
case const expr1 :
conditional statement similar to
statement1
if-else if-else
case const expr2 :
It allows the selection of an arbitrary statement2
number of choices based on a given ...
value default:
statementn
}
ax 2 + bx + c = 0