The for statement

Logical Data in C
Data is called logical if it conveys a
value of true or false.
If a data value is zero, it is considered
false. If a value is non-zero, it is
considered true.
1
Logical operators
The relational operators are the Logical AND, Logical
OR and and the NOT operator.
Value 1
T
T
F
F
Value2
T
F
T
F
&&
T
F
F
F
||
T
T
T
F
! (Value 1)
F
F
T
T
2
Logical Operator Examples
#include <stdio.h>
int main(void)
{
int a = 7, b=0;
printf( "\n The value of a && b is %d\n", a && b );
// prints 0
printf( "\n The value of a || b is %d\n", a || b );
// prints 1
printf( "\n The value of !a && b is %d\n", !a && b ); // prints 0
printf( "\n The value of !a || !b is %d\n", !a || b );
// prints 0
printf( "\n The value of a && !b is %d\n", a && !b ); // prints 1
printf( "\n The value of !(a || b) is %d\n", !(a || b) );
// prints 0
printf( "\n The value of !(a && b) is %d\n", !(a && b) ); // prints 1
return 0;
} // main
3
Relational operators
Six relational operators support logical relationships.
They are all binary operators that accept two
operands and compare them. The value of an
expression involving a relational operator is either
true or false.
The operators are:
<
<=
>
>=
==
!=
less than
less than or equal
greater than
greater than or equal
equal
not equal
higher priority than
the other two
4
Examples
x = 1;
y = 4;
z = 14;
x <= 1 && y == 3
x <= 1 || y == 3
!( x > 1 )
!x > 1
!( x <= 1 || y == 3 )
x >= 1 && y == 3 || z < 14
0
1
1
0
0
0
5
Two-way Selection
The basic decision statement in the computer is the
two-way selection. The decision is described to the
computer as a conditional statement that can be
answered either true or false. If the answer is true,
one or more action statements is executed. If the
answer is false, then a different action or set of
actions is executed. Regardless of which set of
actions is executed, the program continues with the
next statement, after the selection.
6
if statement
if is a mechanism for executing a statement/s
conditionally.
if ( expression )
action 1
else
action 2
In this case only action 1 or action 2 is executed,
depending on whether the expression is true or false.7
if statement cont.
Usually the expression in an if
statement is a relational, equality or
logical expression.
if ( j < k )
min = j;
if ( j < k )
printf( “ j is smaller than k\n”);
8
if statement cont.
if ( j < k )
{
min = j;
printf( “ j is smaller than k\n”);
}
else
printf(“ j is larger than k\n”);
9
Nested if statements
if ( expression 1 )
{
action 1
if ( expression 2 )
action 2
else
action 3
}
else
action 4
10
Dangling else problem
Once you start nesting if…else statements, you can
encounter the ‘dangling else’ problem. This problem
is created when there is no matching else for every if.
C has a simple rule:
an else is always paired with the most recent unpaired if
This might result in your program not matching your
actual intent.
11
Dangling else problem
Write the code for the following pseudocode:
if x is greater to 5
if x is less than 10
print that the value is between 5 and 10
else
print that the value is less than 5
12
The conditional operator
The conditional operator ?: takes in3 expressions and
is in the form
conditional_expression ::= expr1 ? expr2 : expr3
eg:
x = y<z ? y : z;
// if y is < z then x=y otherwise x=z
x = flag ? Y : Y * Y; // if flag is true then x=y otherwise x = y*y
13
The switch statement
The switch is a multi-conditional statement
generalizing the if-else statement.
switch ( expression )
{
case case-label-1 :
statement-list
case case-label-2 :
statement-list
...
default :
statement-list
}
14
The switch statement cont.



Switch works by evaluating expression, passing
control to the case labeled with its value (or the
default) and executing the case’s statement-list.
The case-labels have to have expressions that the
compiler can evaluate to an integer constant, and all
the case-labels have to be unique.
A break statement is placed at the end of each
statement list.
15
The switch statement cont.
Switch ( operator )
{
case '+' : // when the value of operator is +
result = op1 + op 2;
break;
case '-' : // when the value of operator is result = op1 - op 2;
break;
case '*' : // when the value of operator is *
result = op1 * op 2;
break;
case '/' : // when the value of operator is /
if ( op2 != 0.0 )
result = op1 / op 2;
break;
default : // when the value of operator is not any of the above
printf("Unknown operator\n");
result = 0.0;
break;
}
16
The else-if statement
The switch statement only works when the case
values are integral. If it is necessary to make a
multiway decision on the basis of a value that is not
integral, the else if statement could be used.
The else-if is pretty much the same as the if..else
statement with a style change. The last statement in
the series concludes with an else. This is the default
condition; that is, the condition that is to be executed
if all other statements are false.
17
Example
#include <stdio.h>
main()
{
int j =4;
int k, min;
k = j * 4 % 5;
if ( j < k )
{
// this is for the case of j is less than k
min = j;
printf( “ j is smaller than k\n”);
}
else if ( j == k )
{
min = j = k;
printf(“j and k are equal\n”);
}
else
which if
printf(“ j is larger than k\n”);
printf(“min = %d\n”, min );
}
does this else correspond to?
18
More Standard Library
Functions
Another standard library that comes with C is the
<ctype.h> library. This contains different functionality
dealing with characters.
int iscntrl ( int testChar );
int isalnum ( int testChar );
int isdigit ( int testChar );
int toupper ( int convChar );
int tolower ( int convChar );
19