COMPOUND STATEMENT
Simple statements could be used to build a compound statement.
EXAMPLES
1. “3 + 2 = 5” and “Lahore is a city in Pakistan”
Here 3 + 2 = 5 is a statement and Lahore is a city in Pakistan is another statement we combine these two statements by using and to form another statement and you can talk about its truth value. Similarly in the next example we combine two statements by using or to form another statement.
2. “The grass is green” or “ It is hot today”
3. “Discrete Mathematics is not difficult to me”
Here the original statement is Discrete Mathematics is difficult to me and we form the new statement by using not.
AND, OR, NOT are called LOGICAL CONNECTIVES.
OR
COMPOUND STATEMENT
Simple statements could be used to build a compound statement.
EXAMPLES
1. “3 + 2 = 5” and “Lahore is a city in Pakistan”
Here 3 + 2 = 5 is a statement and Lahore is a city in Pakistan is another statement we combine these two statements by using and to form another statement and you can talk about its truth value. Similarly in the next example we combine two statements by using or to form another statement.
2. “The grass is green” or “ It is hot today”
3. “Discrete Mathematics is not difficult to me”
Here the original statement is Discrete Mathematics is difficult to me and we form the new statement by using not.
AND, OR, NOT are called LOGICAL CONNECTIVES.
OR
COMPOUND STATEMENT
A compound statement (or block) is a sequence of statements, enclosed by braces, that stands in place of a single statement. Simple and compound statements are completely interchangeable as far as the syntax of the C language is concerned. Therefore, the statements that comprise a compound statement may themselves be compound; that is, blocks can be nested. Thus, it is legal to write
// 3 wide 16 bit signed median filter
short median(short n1,short n2,short n3){
if(n1>n2){
if(n2>n3)
return(n2); // n1>n2,n2>n3 n1>n2>n3
else{
if(n1>n3)
return(n3); // n1>n2,n3>n2,n1>n3 n1>n3>n2
else
return(n1); // n1>n2,n3>n2,n3>n1 n3>n1>n2
}
}
else{
if(n3>n2)
return(n2); // n2>n1,n3>n2 n3>n2>n1
else{
if(n1>n3)
return(n1); // n2>n1,n2>n3,n1>n3 n2>n1>n3
else
return(n3); // n2>n1,n2>n3,n3>n1 n2>n3>n1
}
}
}
Example of nested compound statements.
Although C is a free-field language, notice how the indenting has been added to the above example. The purpose of this indenting is to make the program easier to read. On the other hand since C is a free-field language, the following two statements are quite different
if(n1>100) n2=100; n3=0;
if(n1>100) {n2=100; n3=0;}
In both cases
n2=100;
is executed if n1>100
. In the first case the statement n3=0;
is always executed, while in the second case n3=0;
is executed only if n1>100
.
Ref: http://users.ece.utexas.edu/~valvano/embed/chap1/chap1.htm
No comments:
Post a Comment