Lecture Notes Part 1

advertisement

IPC144 Week 3 Part 2

Agenda:

1 2 Logical Not Operator ( ! ) / De Morgan’s Law Switch Statement 3 Homework

Logical Not Operator / De Morgan’s Law

Logical Not (!) operator is used when the logical value of the condition must be reversed.

Syntax:

if !( condition ) statement; or while !( condition ) statement; Statement will be executed when condition has a false value.

Example:

if(num1 <= 5) printf(“The number is smaller or equal than five.”); and if !( num1 > 5 ) printf(“The number is smaller or equal than five.”); statements will have the same output - printf statement will be executed when the value of num1 is less than five. while !( num2 <= 7 ){ num2--; printf(“%%%d”, num2); } same as while ( num2 > 7 ){ num2--; printf(“%%%d”, num2); }

DeMorgan’s Law

says that to get the opposite of a compound condition, all the sub conditions must be reversed, as well as all || must be changed to && and && changed to ||.

Example:

Let say we need the loop to stop when the sum is greater than 10 or when the counter reaches 5. So this is the condition to

stop

the loop. ( sum > 10 | | counter = = 5 ) Applying DeMorgan’s Law you can write something like this: while ( sum <= 10 && counter != 5 ){ sum = sum + counter; counter ++; }

Switch Statement

Switch

statement is an alternative to a nested if statement. Used when the execution of the statements depends on the comparison between an integer expression and a constant.

Syntax 1:

switch ( integer expression ) { case constant 1: statement1; case constant 2: statement2; case constant 3: statement3; } In this case the “integer expression” is evaluated first, then the value of “integer expression” is compared with the value of each “constant” starting with “constant 1”. When both values are equal then the statement is executed and all statements below will be executed as well.

Example 1:

int num = 4; switch ( num – 1 ){ case 1: case 2: printf(“num - 1 is equal to %d”, num - 1); printf(“num - 1 is equal to %d”, num - 1); case 3: printf(“number is equal to %d”, num - 1); case 4: printf(“number is equal to %d”, num - 1); }

The reserved word

break

is used to skip all the following statements inside of the switch statement and exit the switch statement. Statements from

default:

section will be executed only if there is no match found.

Syntax 2:

switch ( integer expression ) { case constant1: statement1; break; case constant2: statement2; break; case constant3: statement3; break; default: /* statement 4 is executed only if there is no match found */ statement4; }

Example 2:

int num = 4; } switch ( num - 1){ case 1: printf(“that’s so little...num is 2”); break; case 2: printf(“that’s not to much...num is 3 ”); break; case 3: printf(“that’s more than 2...num is 4”); /* next statement will be executed, since there is no break word in this case */ case 4: printf(“could be 4 or 5....”); break; default: printf(“num is not 2, not 3, not 4, and not 5...”);

Homework # 5 (email: [email protected])

Write a complete C program that will display a character supplied by the user number of times the user specified. There are maximum 10 characters should be displayed per line. Sample output: matrix:/home/akogan/ipc144 $ a.out Enter a character: * Enter the number of characters to display: 36 ********** ********** ********** ******

Download