0% found this document useful (0 votes)
36 views15 pages

5 Programs With Selection Logic (Part 1)

This document discusses selection logic in C programming using if and if...else statements. It provides examples of using Boolean expressions with relational and logical operators to write conditional statements that execute different code blocks depending on whether the condition is true or false. The if statement executes the statement if the condition is true, while if...else additionally specifies an else statement to execute if the condition is false.

Uploaded by

Áhméd Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views15 pages

5 Programs With Selection Logic (Part 1)

This document discusses selection logic in C programming using if and if...else statements. It provides examples of using Boolean expressions with relational and logical operators to write conditional statements that execute different code blocks depending on whether the condition is true or false. The if statement executes the statement if the condition is true, while if...else additionally specifies an else statement to execute if the condition is false.

Uploaded by

Áhméd Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ELC 111 : Computer Programming I

Programs with Selection Logic


(Part 1)

Lecturer: Dr. Reham Samir


References
 Paul Deitel, Harvey Deitel, “C How to Program with
an introduction to C++ ”, Pearson Education, eighth
edition, 2016.

 Noel Kalicharan, “Learn to Program with C ”, Apress,


2015.
Introduction
 Programs can use selection logic as they can test some condition and take
different courses of action based on whether the condition is true or false.
 In C, selection logic is implemented using the if and the if...else
statements.
 Example: Suppose the variable score holds the score obtained by a
student in a test, and the student passes if her score is 50 or more and
fails if it is less than 50. A program can be written to test the condition:
score >= 50
 If it is true, the student passes; if it is false, the student fails.
 In C, this can be written as:
if (score >= 50)
printf("Pass\n");
else
printf("Fail\n");
Boolean Expressions
 A Boolean expression is one that is either true or false.
 Boolean expression can use
 Relational operators
 Logical operators
 Relational operators
Boolean Expressions
 Logical Operators
 AND (&&)
 OR ( | | )
 NOT ( ! )
Boolean Expressions
 AND (&&)
 Sometimes, we need to ask if one thing is true AND another thing is
true so we use the logical operator AND.
 In C, the symbol for AND is && .
 Example: suppose we want to know if the value of h lies between 1
and 99, (if h is greater than or equal to 1 AND if h is less than or
equal to 99) . In C, we express this as:
(h >= 1) && (h <= 99)
 Notes:
 It is wrong to write:
h >= 1 && <= 99 //this is wrong
 The expression
h >= 1 && h <= 99
is the same as
(h >= 1) && (h <= 99)
Boolean Expressions
 OR ( | | )
 We may need to know if one of two things is true, so we use the
logical operator OR.
 In C, the symbol for OR is | | .
 Example: If n is an integer representing a month of the year, we can
check if n is invalid by testing if n is less than 1 OR n is greater than
12. In C, we express this as:
(n < 1) || (n > 12)
 NOT ( ! )
 We may need to reverse the value of Boolean expression, so we use the
logical operator NOT.
 In C, the symbol for NOT is ! .
 Example: the expression (n < 1) || (n > 12) that tests for invalid n is
equivalent to:
!(n >= 1 && n <= 12)
if Construct
 In general, if construct in C takes the following form:
if (<condition>) <statement>
 Where <condition> is a Boolean expression and <statement> can be either a
one-line statement or a block of statements enclosed by { and }.
 If <condition> is true, <statement> is executed; if <condition> is false,
<statement> is not executed.
 Note that: if the <condition> evaluates to zero, it’s treated as false, and if it
evaluates to nonzero, it’s treated as true.
 Some styles of writing an if statement (the compiler doesn’t care which
style is used):
if Construct
 Example: Suppose we want to exchange the values of two
variables a and b but only if a is bigger than b. assuming, as an
example, that a = 15, b = 8, and c is a temporary variable:
if (a > b)
{
c = a; //store a in c; c becomes 15
a = b; //store b in a; a becomes 8
b = c; //store old value of a, 15,in b
}
if Construct
 Example: Write a program to prompt for two lengths (a length is
given in meters and centimeters for example, 3m 75cm) and print
their sum such that the centimeter value is less than 100.
 Assume the program works as follows:
if Construct
 SOL:
if Construct
 Example: If a sample run of this program is as following:

 Then we need to modify our program.


if Construct
 SOL:

 Note:
 We do not even need the if statement (there is usually more than one
way to express the logic of a program)
if...else Construct
 The if…else selection statement allows you to specify that different
actions are to be performed when the condition is true and when it’s false.
 In general, the if...else construct in C takes the form:
if (<condition>) <statement1> else <statement2>
 Or
if (<condition>) <statement1>
else <statement2>
 Or
if (<condition>) {
...
}
else {
...
}
if...else Construct
 Example: A student is given 3 tests, each marked out of 100. The
student passes if his average mark is greater than or equal to 50 and fails
if his average mark is less than 50. Prompt for the 3 marks and print Pass
if the student passes and Fail if he fails.
 A sample run of this program is as following:

 Sol:

You might also like