You are on page 1of 6

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:

You might also like