Conditional Statements - If
Definition
An if conditional statement is a programming control structure that executes a block of code only when a specified condition evaluates to true.
In simple words:
- If the condition is true, the program runs the code inside the
ifblock. - If the condition is false, the program skips that block.
This decision-making structure is used in almost every programming language and is essential for controlling program flow.
Main Content
1. First Concept: Basic if Statement
- The basic
ifstatement checks a single condition. - It contains a Boolean expression, which means the condition must result in either true or false.
The general form is:
if (condition)
{
statements;
}
Example:
if (marks >= 40)
{
printf("You have passed.");
}
In this example, the message is displayed only if marks is greater than or equal to 40.
Explanation:
marks >= 40is the condition.- If the condition is true, the statement inside the block executes.
- If the condition is false, nothing happens.
Real-world analogy:
If the door is open, enter the room.
Important notes:
- A condition must be something that can be evaluated as true or false.
- The statements inside the
ifblock are usually enclosed in braces{ }. - If there is only one statement, some languages allow braces to be omitted, but using braces is safer and clearer.
2. Second Concept: Relational and Logical Conditions
- Conditions in an
ifstatement are created using relational operators and logical operators. - These operators help compare values and combine multiple conditions.
Relational operators:
These are used to compare two values.
==equal to!=not equal to>greater than<less than>=greater than or equal to<=less than or equal to
Example:
if age >= 18:
print("Eligible to vote")
Logical operators:
These are used when a decision depends on more than one condition.
&&orand→ both conditions must be true||oror→ at least one condition must be true!ornot→ reverses the condition
Example:
if (age >= 18 && citizen == true)
{
console.log("Eligible to vote");
}
Explanation:
The message appears only if both conditions are satisfied:
- age is 18 or above
- the person is a citizen
Key idea:
An if statement is only as useful as the condition inside it. Strong decision-making depends on correctly writing conditions.
3. Third Concept: Nested if and if in Decision Making
- An
ifstatement can be placed inside anotherifstatement. This is called a nestedif. - Nested
ifstatements are used when one decision depends on another decision.
Example:
if (marks >= 40)
{
if (marks >= 75)
{
System.out.println("Passed with distinction");
}
else
{
System.out.println("Passed");
}
}
Explanation:
- First, the program checks whether the student passed.
- If yes, it then checks whether the student got distinction.
- This allows multi-level decision-making.
When it is useful:
- checking eligibility rules
- validating user input
- determining grades
- making menu-based selections
Simple flow representation:
Start
|
v
Check condition
|
+---- True ----> Execute block ----> End
|
+---- False ---> Skip block -------> End
This flow shows the basic behavior of an if statement.
Working / Process
1. The program begins execution and reaches the if statement.
At this point, the condition inside the if is evaluated. The condition can involve variables, comparisons, or logical expressions. The program does not yet execute the block; it first determines whether the condition is true or false.
2. The condition is tested using operators and values.
The program compares data such as numbers, characters, or text values depending on the language and context. If the condition becomes true, the code inside the if block is selected for execution. If it is false, the block is ignored.
3. The result of the condition controls the flow of the program.
When true, the statements inside the if block run in sequence. When false, the program jumps past the block and continues with the next statement after the if. This is what makes if a control flow statement.
Advantages / Applications
Decision-making in programs
if statements allow programs to make intelligent choices, such as checking whether a user entered the correct password or whether a number is valid.
Input validation
They are widely used to verify data before processing it, helping prevent errors and incorrect results.
Real-world problem solving
if statements are used in grading systems, shopping discounts, traffic control systems, login authentication, and many other practical applications.
Summary
- The
ifstatement runs code only when a condition is true. - It is the simplest form of conditional decision-making in programming.
- It uses comparisons and logical checks to control program flow.
- Important terms to remember:
if, condition, true, false, relational operators, logical operators, nestedif