Conditional Statements in C

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ES085 (Computer Programming 2)

Week 1 Topic: C Conditional Control Structure


At the end of the lesson the students should be able to:
1. understand the concept of a conditional control structure,
2. identify the relational and logical operators, and
3. appreciate the application of C conditional programming using the if-else statement.

Conditional Statement in C
Conditional Statements in C programming are used to make decisions based on the conditions. Conditional statements
execute sequentially when there is no condition around the statements.

The if statement
The if statement is one of the powerful conditional statements in C. It is responsible for modifying the flow of execution
of a program.
Syntax:
if (test expression) {
//code or statements
}
The condition evaluates to either true or false. True is always a non-zero value, and false is a value that contains zero.

C Relational Operators
C has six relational operators that can be used to formulate a Boolean expression for making a decision and testing
conditions, which returns true or false:
< less than
<= less than or equal
> greater than
>= greater than or equal
== equal to
!= not equal to

Logical Operators in C
&& logical AND
|| logical OR
! logical NOT

if statement Example 1:

Output:

Practice 1: Write a C program that will determine whether the given number is positive. (Note: Use if
statement without else.)
**You may use Dev C++ IDE or you may use www.codechum.com ‘s playground for your coding.

Sample Outputs:
Test Case 1:
The given number is positive.
Test Case 2:
The given number is negative.
The if-else statement
Flowchart

Syntax:

if-else statement Example 2:

Sample Outputs:
Test Case 1:

Test Case 2:

Practice 2: Write a C program that will determine whether the given number is positive or negative.

Nested if-else statements


Nested if-else statements are used when a series of decisions is required. Nesting means using one if-else construct within
another one.
Syntax:

Example 3: Write a C program that will ask the user to enter two numbers and determine if the first number is greater than
the second number, the second number is greater than the first number, or the two numbers are equal.

Sample Outputs:
Test Case 1:

Test Case 2:

Test Case 3:

Example 4: Modified version of Example 3.

Sample Outputs:
Test Case 1:

Test Case 2:

Test Case 3:
Assignment 1:
Write a C program that will accept the height of a person in centimeters and categorize the person according to their height.
Height in cm Category
< 166 cm Short
< 175 cm Average
>= 175 cm Tall

Sample Outputs:
Test Case 1:
Enter your height in cm: 170
You are of average height.

Test Case 2:
Enter your height in cm: 155
You are short.

Test Case 3:

Enter your height in cm: 184


You are tall.

Assignment 2:

Write a C program that will ask the user to enter an integer. If the integer is even, display “Boppity Boo!” and if the integer is odd,
display “Abracadabra!”.

Sample Outputs:
Test Case 1:
Enter an integer: 16
Boppity Boo!

Test Case 2:
Enter an integer: -9
Abracadabra!

Test Case 3:

Enter an integer: 0
Boppity Boo!

Note: You may use Dev C++ IDE or you may use www.codechum.com ‘s playground for your coding.

" The struggle you're in today is developing a strength you need for tomorrow..."

You might also like