Upload
meljun-cortes
View
95
Download
0
Embed Size (px)
Citation preview
Introducing C’s Program Introducing C’s Program Control StatementsControl Statements
C Programming C Programming LanguageLanguage
MELJUN CORTESMELJUN CORTES
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 2
ObjectivesObjectives
CC’’s most important program control s most important program control
statements statements if, for if, for Determine your programDetermine your program’’s flow of execution.s flow of execution.
Form the backbone of your programs.Form the backbone of your programs.
WeWe’’ll learnll learn Blocks of codeBlocks of code
The relational and logical operatorsThe relational and logical operators
More printf()More printf()
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 3
Ex. 1Ex. 1
Read two numbers to A and B. Read two numbers to A and B. Increase 1 number to A and decrease 1 number to Increase 1 number to A and decrease 1 number to
B.B.
STARTSTART
A = A + 1A = A + 1
B = B - 1B = B - 1
ENDEND
A, BA, B
READ A, BREAD A, B
#include <stdio.h> void main() {
#include <stdio.h> void main() {
int a, b; int a, b;
printf(“Input two numbers : ”); scanf(“%d%d”, &a, &b);
printf(“Input two numbers : ”); scanf(“%d%d”, &a, &b);
} }
a = a + 1; b = b - 1;
a = a + 1; b = b - 1;
printf(“A’s value : %d\n”, a); printf(“B’s value : %d\n”, b);
printf(“A’s value : %d\n”, a); printf(“B’s value : %d\n”, b);
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 4
Ex. 2Ex. 2
Read two numbers to A and B.Read two numbers to A and B. Swap A for B.Swap A for B.
AA BB
AA BB
temptemp
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 5
1.1. Become familiar with the Become familiar with the ifif
If statementsIf statements CC’’s selection statementss selection statements
Conditional statementsConditional statements
Logical dataLogical data truetrue
falsefalse
C doesnC doesn’’t have boolean datatype.t have boolean datatype. Use int, char datatype for logical dataUse int, char datatype for logical data
if(if(expressionexpression)) statement ;statement ;Execute when expression is true
The result is true or false
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 6
if (10>9) printf(“true”);if (10>9) printf(“true”);
1.1. Become familiar with the Become familiar with the ifif
Relational operatorRelational operator > : greater than> : greater than
expression
statement
true
false
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 7
if (9 < 5) printf(“this will not print”);if (9 < 5) printf(“this will not print”);
1.1. Become familiar with the Become familiar with the ifif
Relational operatorRelational operator < : less than< : less than
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 8
1.1. Become familiar with the Become familiar with the ifif
Example 1Example 1
#include <stdio.h>int main(void) {
int answer;
printf(“What is 10 + 14 ? ”);scanf(“%d”, &answer);
if(answer == 10+14) printf(“Right!”);
return 0;}
#include <stdio.h>int main(void) {
int answer;
printf(“What is 10 + 14 ? ”);scanf(“%d”, &answer);
if(answer == 10+14) printf(“Right!”);
return 0;}
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 9
1.1. Become familiar with the Become familiar with the ifif
Example 2 Example 2 (feet (feet –– meters conversion program) meters conversion program)
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 10
1.1. Become familiar with the Become familiar with the ifif
ExercisesExercises
Write a program that asks the user for an integer
and then tells the user if that number is even or odd.
(Hint, use C’s modulus operator %.)
Write a program that asks the user for an integer
and then tells the user if that number is even or odd.
(Hint, use C’s modulus operator %.)
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 11
Two-way Selection statementTwo-way Selection statement
2. Add the 2. Add the elseelse
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 12
ifif…… else logic flow else logic flow
2. Add the 2. Add the elseelse
;
;
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 13
Example 1Example 1
2. Add the 2. Add the elseelse
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 14
Example 2Example 2
2. Add the 2. Add the elseelse
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 15
ExercisesExercises
2. Add the 2. Add the elseelse
Write a program that requests two numbers and then
displays either their sum or product, depending on
what the user selects.
input two number : 28 5
select operator ( 1 : sum, 2 : product ) : 1
28 + 5 = 33
Write a program that requests two numbers and then
displays either their sum or product, depending on
what the user selects.
input two number : 28 5
select operator ( 1 : sum, 2 : product ) : 1
28 + 5 = 33
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 16
ExercisesExercises
2. Add the 2. Add the elseelse
Write a program that requests two numbers and then
displays bigger number.
input two number : 28 5
28 > 5
Write a program that requests two numbers and then
displays bigger number.
input two number : 28 5
28 > 5
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 17
Code block : { the statements }Code block : { the statements }
3. Create blocks of code3. Create blocks of code
One logical unit
One logical unit
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 18
If the user enters any positive number, this If the user enters any positive number, this
fragment prints the messagefragment prints the message
3. Create blocks of code3. Create blocks of code
scanf(“%d”, &num) ;
if(num > 0) {
printf(“This is ”) ;
printf(“an example of ”) ;
printf(“a code block.”) ;
}
scanf(“%d”, &num) ;
if(num > 0) {
printf(“This is ”) ;
printf(“an example of ”) ;
printf(“a code block.”) ;
}
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 19
Example 1Example 1
3. Create blocks of code3. Create blocks of code
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 20
Example 2Example 2
3. Create blocks of code3. Create blocks of code
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 21
ExercisesExercises
3. Create blocks of code3. Create blocks of code
Write a program that either adds or subtracts two
integers. First, prompt the user to choose an operation;
then prompt for the two numbers and display the result.
select operator ( 1 : add, 2 : subtract ) : 1
input two number : 28 5
28 + 5 = 33
Write a program that either adds or subtracts two
integers. First, prompt the user to choose an operation;
then prompt for the two numbers and display the result.
select operator ( 1 : add, 2 : subtract ) : 1
input two number : 28 5
28 + 5 = 33
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 22
CC’’s loop statementss loop statements for loop / while loop / do.. while loopfor loop / while loop / do.. while loop
forfor loop allows one or more statements to be loop allows one or more statements to be repeated. repeated.
General form of General form of forfor loop loop
Initialization Initialization Conditional-testConditional-test incrementincrement
4. Use the 4. Use the forfor loop loop
for (initialization; conditional test; increment) statements;
for (initialization; conditional test; increment) statements;
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 23
Initialization Initialization Use to give an initial value to loop-control variableUse to give an initial value to loop-control variable Execute only once before the loop beginsExecute only once before the loop begins
Conditional-testConditional-test Is performed at the start of each iteration.Is performed at the start of each iteration. Test the loop-control variable against a target valueTest the loop-control variable against a target value True True the loop repeat the loop repeat False False the loop stop the loop stop
IncrementIncrement Is executed at the bottom of the loopIs executed at the bottom of the loop Increase or decrease the loop-control value by a certain Increase or decrease the loop-control value by a certain
amountamount
4. Use the 4. Use the forfor loop loop
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 24
4. Use the 4. Use the forfor loop loop
for statementfor statement
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 25
4. Use the 4. Use the forfor loop loop
Simple exampleSimple example This program uses a for loop to print the This program uses a for loop to print the
numbers 1 through 10 on the screennumbers 1 through 10 on the screen
#include<stdio.h>
int main(void){
int num;
for(num=1; num<11; num=num+1)
printf(“%d ”, num) ;
printf(“terminating”);
return 0;
}
#include<stdio.h>
int main(void){
int num;
for(num=1; num<11; num=num+1)
printf(“%d ”, num) ;
printf(“terminating”);
return 0;
} 1 2 3 4 5 6 7 8 9 10 terminating1 2 3 4 5 6 7 8 9 10 terminating
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 26
4. Use the 4. Use the forfor loop loop
Example Example (loop will not execute)(loop will not execute)
#include<stdio.h>
int main(void){
int num;
//this loop will not execute
for(num=11; num<11; num=num+1)
printf(“%d ”, num) ;
printf(“terminating”);
return 0;
}
#include<stdio.h>
int main(void){
int num;
//this loop will not execute
for(num=11; num<11; num=num+1)
printf(“%d ”, num) ;
printf(“terminating”);
return 0;
}terminatingterminating
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 27
4. Use the 4. Use the forfor loop loop
Example Example (A for loop can run negatively)(A for loop can run negatively)
#include<stdio.h>
int main(void){
int num;
//this loop will not execute
for(num=20; num>0; num=num-1)
printf(“%d ”, num) ;
printf(“terminating”);
return 0;
}
#include<stdio.h>
int main(void){
int num;
//this loop will not execute
for(num=20; num>0; num=num-1)
printf(“%d ”, num) ;
printf(“terminating”);
return 0;
}20 19 18 17 …… 3 2 1 terminating20 19 18 17 …… 3 2 1 terminating
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 28
4. Use the 4. Use the forfor loop loop
Using block to repeat several statementsUsing block to repeat several statements This program computes the product and sum of the This program computes the product and sum of the
numbers from 1 to 5.numbers from 1 to 5.
#include<stdio.h>int main(void){
int num, sum, prod;sum = 0;prod = 1;for(num=1; num<6; num=num+1) {
sum = sum + num;prod = prod * num;
}printf(“product and sum : %d %d\n”, prod, sum) ;return 0;
}
#include<stdio.h>int main(void){
int num, sum, prod;sum = 0;prod = 1;for(num=1; num<6; num=num+1) {
sum = sum + num;prod = prod * num;
}printf(“product and sum : %d %d\n”, prod, sum) ;return 0;
} product and sum : 120 15product and sum : 120 15
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 29
4. Use the 4. Use the forfor loop loop
Example Example (increment or decrement by more than one)(increment or decrement by more than one) This program counts to 100 by fives.This program counts to 100 by fives.
#include<stdio.h>
int main(void){
int i;
for(i=0; i<101; i=i+5)
printf(“%d ”, i) ;
return 0;
}
#include<stdio.h>
int main(void){
int i;
for(i=0; i<101; i=i+5)
printf(“%d ”, i) ;
return 0;
}0 5 10 15 20 …… 90 95 1000 5 10 15 20 …… 90 95 100
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 30
4. Use the 4. Use the forfor loop loop
Example 1. Example 1. (addition-drill)(addition-drill)
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 31
4. Use the 4. Use the forfor loop loop
Example 2. Example 2. (determination of prime number)(determination of prime number)
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 32
4. Use the 4. Use the forfor loop loop
Exercises 1.Exercises 1.
Create a program that prints the numbers from 1 to 100.
1 2 3 …… …… 8 9 10: : : :: : : :: : : :
91 92 93 …… …… 98 99 100
Create a program that prints the numbers from 1 to 100.
1 2 3 …… …… 8 9 10: : : :: : : :: : : :
91 92 93 …… …… 98 99 100
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 33
4. Use the 4. Use the forfor loop loop
Exercises 2.Exercises 2.
Write a program that prints the numbers between 17 to
100 that can be evenly divided by 17.
34 51 68 85
Write a program that prints the numbers between 17 to
100 that can be evenly divided by 17.
34 51 68 85
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 34
4. Use the 4. Use the forfor loop loop
Exercises 3.Exercises 3.
Write a program similar to the prime-number tester,
except that it displays all the factors of a number entered
By the user.
For example, if the user entered 8, it would respond with
2 and 4.
Enter a number : 8
8’s factor => 2 4
Write a program similar to the prime-number tester,
except that it displays all the factors of a number entered
By the user.
For example, if the user entered 8, it would respond with
2 and 4.
Enter a number : 8
8’s factor => 2 4
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 35
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
Increment operator (++)Increment operator (++) num = num +1; num = num +1; num++;num++;
Using for loopUsing for loop
Decrement operator (--)Decrement operator (--) count = count -1; count = count -1; count--;count--;
for(num=1; num<some_value; num=num+1) ……for(num=1; num<some_value; num=num+1) ……
for(num=1; num<some_value; num++) ……for(num=1; num<some_value; num++) ……
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 36
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
The Position of increment/decrement operatorThe Position of increment/decrement operator (++, (++, --)--) Although the effect on the variable is the same, the Although the effect on the variable is the same, the
position of the operator does affect when the operation is position of the operator does affect when the operation is performed.performed.
#include<stdio.h>int main(void){
int i, j;i = 10;j = i++;// this will print 11 10printf(“i and j : %d %d\n”, i, j) ;return 0;
}
#include<stdio.h>int main(void){
int i, j;i = 10;j = i++;// this will print 11 10printf(“i and j : %d %d\n”, i, j) ;return 0;
}
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 37
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
After the variable After the variable ( num++ )( num++ )
If max = 1 If max = 1 ResultResult
max = 2, count = 10max = 2, count = 10
First : the current value of variable is used for expression.First : the current value of variable is used for expression. After : increment expression execute.After : increment expression execute.
ArrangementArrangement
count = 10 * max++;count = 10 * max++;
count = 10 * max++;count = 10 * max++; count = 10 * max;
max = max + 1;
count = 10 * max;
max = max + 1;
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 38
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
Before the variable Before the variable ( ++num )( ++num ) First : increment expression execute.First : increment expression execute. After : the incremented value of variable is used for After : the incremented value of variable is used for
expression.expression.
#include<stdio.h>int main(void){
int i, j;i = 10;j = ++i;// this will print 11 11printf(“i and j : %d %d\n”, i, j) ;return 0;
}
#include<stdio.h>int main(void){
int i, j;i = 10;j = ++i;// this will print 11 11printf(“i and j : %d %d\n”, i, j) ;return 0;
}
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 39
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
Before the variable Before the variable ( num++ )( num++ )
If max = 1 If max = 1 ResultResult
max = 2, count = 20max = 2, count = 20
ArrangementArrangement
count = 10 * ++max;count = 10 * ++max;
count = 10 * ++max;count = 10 * ++max; max = max + 1;
count = 10 * max;
max = max + 1;
count = 10 * max;
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 40
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
Example 1.Example 1.
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 41
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
Example 2.Example 2.
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 42
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
Exercises 1.Exercises 1.
Change all appropriate assignment statements in this program to increment or decrement statements.
#include <stdio.h>int main(void) {
int a, b;a=1;a = a + 1;b = a;b = b – 1;printf(“%d %d”, a, b);return 0;
}
Change all appropriate assignment statements in this program to increment or decrement statements.
#include <stdio.h>int main(void) {
int a, b;a=1;a = a + 1;b = a;b = b – 1;printf(“%d %d”, a, b);return 0;
}
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 43
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
Exercises 2.Exercises 2.
Enter a number. Compute summation until the number
from 1.
enter a number : 10
sum of 1 ~ 10 : 55
Enter a number. Compute summation until the number
from 1.
enter a number : 10
sum of 1 ~ 10 : 55
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 44
5. Substitute C5. Substitute C’’s increment and s increment and decrement Operatorsdecrement Operators
Exercises 3.Exercises 3.
Enter a number. Compute factorial of the number.
enter a number : 3
3! = 6
enter a number : 5
5! = 120
enter a number : 10
10! = 3628800
Enter a number. Compute factorial of the number.
enter a number : 3
3! = 6
enter a number : 5
5! = 120
enter a number : 10
10! = 3628800
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 45
6. Expand 6. Expand printf()printf()’’s capabilitiess capabilities
Backslash codes Backslash codes Represent characters that cannot be entered from the Represent characters that cannot be entered from the
keyboard, and non-printing characterskeyboard, and non-printing characters
Escape characterEscape characterEscape characterEscape character
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 46
6. Expand 6. Expand printf()printf()’’s capabilitiess capabilities
'\n' '\n' (newline character)(newline character) It translates it into a carriage return/linefeed It translates it into a carriage return/linefeed
combination.combination.
• backslash codes are character constants.
char ch; ch = '\t'; /* assign ch the tab character */
• backslash codes are character constants.
char ch; ch = '\t'; /* assign ch the tab character */
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 47
number square cube=============================== 1 1 1 2 4 8 3 9 27 : : : 10 100 1000
number square cube=============================== 1 1 1 2 4 8 3 9 27 : : : 10 100 1000
6. Expand 6. Expand printf()printf()’’s capabilitiess capabilities
ExerciseExercise Using Using forfor loop (1~10) loop (1~10)
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 48
7. Program with C7. Program with C’’s relational s relational and Logical operatorsand Logical operators
Relational operatorsRelational operators Compare two values and return a true or false result Compare two values and return a true or false result
based upon that comparison.based upon that comparison.
Logical operatorsLogical operators Connect together true/false results.Connect together true/false results.
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 49
7. Program with C7. Program with C’’s relational s relational and Logical operatorsand Logical operators
Truth table Truth table (AND, OR, NOT)(AND, OR, NOT)
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 50
7. Program with C7. Program with C’’s relational s relational and Logical operatorsand Logical operators
Precedence of operatorsPrecedence of operators The relational and logical operators are both lower in The relational and logical operators are both lower in
precedence than the arithmetic operators.precedence than the arithmetic operators.
10 + count > a + 1210 + count > a + 12 (10 + count) > (a + 12)(10 + count) > (a + 12)
var > max || ! (max==100) && 0 <= itemvar > max || ! (max==100) && 0 <= item
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 51
7. Program with C7. Program with C’’s relational s relational and Logical operatorsand Logical operators
Example 1.Example 1.
if(count != 0) …… if(count != 0) …… if(count)if(count)
The expression !count is true only if count is zero.The expression !count is true only if count is zero.
if(count = = 0) …… if(count = = 0) …… if(!count)if(!count)
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 52
7. Program with C7. Program with C’’s relational s relational and Logical operatorsand Logical operators
Example 2. Example 2. (the outcome of a relational or logical operation)(the outcome of a relational or logical operation)
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 53
7. Program with C7. Program with C’’s relational s relational and Logical operatorsand Logical operators
Example 3. Example 3. (exclusive-OR)(exclusive-OR) XOR truth TableXOR truth Table
A function of XORA function of XOR
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS School of Computer InformationSchool of Computer Information 54
7. Program with C7. Program with C’’s relational s relational and Logical operatorsand Logical operators
Example 3. Example 3. (exclusive-OR)(exclusive-OR) Demonstrate the xor()Demonstrate the xor()