Nested If-else. Iterative Statement - For

Comprehensive study notes, diagrams, and exam preparation for Nested If-else. Iterative Statement - For.

Nested If-else. Iterative Statement - For

Definition

Nested if-else is a decision-making structure in which an if or if-else statement is placed inside another if or if-else statement to handle multiple levels of conditions.

for iterative statement is a looping statement that repeats a block of code a known number of times, usually by using a loop variable that changes automatically with each iteration.


Main Content

1. Nested If-else

Meaning and structure

Nested if-else means placing one conditional statement inside another. It is used when a decision must be made step by step, and the next condition depends on the result of the previous condition. This allows a program to handle complex logic in an organized manner.

Example:

  if (marks >= 40) {
      if (marks >= 75) {
          printf("Distinction");
      } else {
          printf("Pass");
      }
  } else {
      printf("Fail");
  }

How it works and where it is used

The outer if checks the first condition. If that condition is true, the inner if-else runs. If the outer condition is false, the program skips the inner block completely. Nested if-else is commonly used in grading systems, eligibility checks, login validation, age classification, and other situations where multiple conditions must be checked one after another.

Example scenario:

  • If age is at least 18, then check whether the person has an ID card.
  • If both conditions are true, allow entry.
  • Otherwise, deny entry.

2. Iterative Statement - For

Meaning and structure

The for loop is used to repeat a set of statements a fixed number of times. It usually contains three parts: initialization, condition, and update. This makes it compact and suitable when the number of repetitions is already known.

Basic form:

  for (initialization; condition; update) {
      // statements
  }

Example:

  for (int i = 1; i <= 5; i++) {
      printf("%d\n", i);
  }

How it works and where it is used

First, the loop variable is initialized. Then the condition is checked before each repetition. If the condition is true, the loop body executes. After the body finishes, the update expression changes the loop variable, and the condition is checked again. This continues until the condition becomes false.

Common uses include:

  • Printing numbers from 1 to 10
  • Finding the sum of a series
  • Traversing arrays
  • Displaying tables and patterns
  • Repeating actions in menu-driven programs

3. Relationship Between Nested If-else and For Loop

Using both together

In many programs, nested if-else and for loops are combined. A loop repeats the process, and inside that repeated process, conditions decide what action to perform. This combination is powerful for solving real problems.

Example:

  for (int i = 1; i <= 10; i++) {
      if (i % 2 == 0) {
          printf("%d is even\n", i);
      } else {
          printf("%d is odd\n", i);
      }
  }

Why this combination is important

The for loop controls repetition, while nested if-else controls decision-making within each repetition. This is useful in processing multiple records, validating data, filtering values, and generating formatted output. It helps programmers write efficient and readable code.

Example in student marks:

  • Loop through all students
  • Check each student’s marks
  • Decide whether the student passes, fails, or gets distinction

Working / Process

1. Analyze the problem requirements

First, identify whether the task needs decision-making, repetition, or both. If multiple conditions are involved, nested if-else may be needed. If a task must be repeated several times, a for loop is suitable. If both are required, combine them carefully.

2. Set up the loop or conditions correctly

For a for loop, choose the starting value, ending condition, and step size. For nested if-else, arrange conditions from general to specific or from highest priority to lowest priority, depending on the problem. Proper arrangement avoids logical errors.

3. Execute and test each step

The program starts with initialization, checks conditions, and performs the required action. In nested if-else, only one matching path is executed. In a for loop, the process repeats until the condition becomes false. Test with different values to ensure all branches and iterations work correctly.


Advantages / Applications

Efficient handling of complex decisions and repeated tasks

Nested if-else allows multiple related conditions to be checked clearly, while for loops reduce repetitive code. Together, they make programs shorter, more structured, and easier to manage.

Widely used in real-world programs

These statements are used in grading systems, billing applications, validation checks, searching, sorting, processing arrays, and generating patterns. They are essential in almost every programming language and are used frequently in academic and practical programming problems.

Improves problem-solving and program clarity

Nested if-else breaks complex logic into smaller decision levels, and the for loop organizes repeated tasks in a controlled way. This improves readability, debugging, and maintenance of code.


Summary

  • Nested if-else is used for decision-making inside another decision.
  • The for loop is used to repeat statements a known number of times.
  • Both are often combined to solve real programming problems efficiently.
  • Important terms to remember: if, else, nested if-else, for loop, initialization, condition, update, iteration.