Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


Computing for Engineers

Lecture 4

Conditions in the C programming language


1. Selection – the if, ifelse and switch clauses

The ifstatement allows branching (decision making) depending on the value of some conditional (Boolean) statement which evaluates to TRUE or FALSE. The   following program inputs a number and prints out a message if the number is   less than 100:

#include

int main(void)

{

int value;

printf("Enter a number: ");

scanf("%d", &value);

if (value<100)

printf("The number was less than 100\n");

return 0;

}

The expression (value<100) is a Boolean expression and has a value of TRUE or FALSE. Thus if the expression evaluates to TRUE, then the message is printed. As in the case of for and while loops, multiple program statements can be included under a single if by enclosing them in {…} :

#include

int main(void)

{

int value;

printf("Enter a number: ");

scanf("%d", &value);

if (value<100)

{

printf("The number was less than 100\n");

value++;

}

return 0;

}

We can have a series of sequential if statements to apply multiple conditions. The following program determines whether a character is within the range A-Z:

#include

int main(void)

{

char letter = 'T';

if (letter >= 'A')

if (letter <= 'Z')

printf("The character %c is

within the range A to Z\n," letter);

return 0;

}

Sample program output:

The character T is within the range A to Z

1.1.    The ifelse clause

The general form of ifelse is:

if (condition 1)

statement 1;

else if (condition 2)

statement 2;

else if (condition 3)

statement 3;

.

else

statement n;

Here is a simple example:

#include

int main(void)

{

int value;

printf("Enter a number: ");

scanf("%d", &value);

if (value<100)

printf("The number is less than 100\n");

else

printf("The number is greater than or equal to 100\n");

return 0;

}

The else part of the statement runs if the if condition is false, when the value is >= 100. If we draw this as a flowchart:

Enter value

“Number is less than

100”

FALSE

“Number is greater than or equal to

100”

Here is a slightly longer example where two ifstatements are linked with an else statement. The following program uses an if else clause to check if a number input from the keyboard is in the range 1..10:

#include

int main(void)

{

int n;

printf("Enter a number: ");

scanf("%d", &n);

if (n<1)

printf("n is below 1 \n");

else if (n>10)

printf("n is above 10 \n");

else

printf("n is in the range 1..10 \n"); return 0;

}

If we draw this program as a flowchart, this is what we see:

Enter n

FALSE

FALSE

2. The switch statement

The switch clause is a way of writing a program when a series of if else clauses occurs. The general form of the switch clause is:

switch (expression)

{

case 1:

program statement;

program statement;


.


.

break;

case 2:

program statement;

program statement;

.

.

break;

.

.

default:

program statement;

program statement;

.

.

break;

}

The  “case” values  must  be  specific values – they  can’t  be  conditions.  Only integers or single characters can be used as case values.

The keyword break must be included at the end of each case statement. This causes control to jump to the end of the switch clause once one of the cases has been met.

The default clause is optional and is executed if all of the cases are not met.

The following example program performs a specific arithmetic operation on two numbers within a switch clause:

#include

int main(void)

{

int menu, number1, number2, result;

printf("Enter two numbers: ");

scanf("%d %d", &number1,&number2);

printf("Enter operator choice \n");

printf("1=addition\n");

printf("2=subtraction \n");

printf("3=multiplication\n");

scanf("%d",&menu);

switch (menu)

{

case 1: result=number1+number2;

break;

case 2: result=number1-number2;

break;

case 3: result=number1*number2;

break;

default: printf("Invalid operator \n”); result = 0;

break;

}

printf ("The result is %d\n", result);

return 0;

}

Clearly this program could be implemented with a series of ifelse clauses but using switch produces much neater code.


This arrangement is very frequently used within a do-while loop in order to   implement an interactive menu system. The while condition is used to            determine the value to exit the menu, and the default case is used to detect any incorrect menu options, by printing out an error message.

3. Logical and relational operators

These operators evaluate to Boolean (TRUE or FALSE) values and are used in if statements or for the stopping condition in while loops. We have already seen the use of relational operators in for loops. The symbols are as follows: