MCS-201 · Unit 3

Decision and Loop Control Statements

Section 1 of 7

Block 1 · Unit 3Decision and Loop Control Statements10 min · Section 1 of 7

3.1Control Statements and the if Statement

Covers IGNOU MCS-201 §3.2, 3.2.1

By default, C executes statements one after another in the order they are written. Control statements change that order. This section introduces the three kinds of control and the most basic decision statement, if, with its partner else.

Three kinds of control

Every program is built from three control structures (the same ones met in the flowcharts of Unit 1):

StructureMeaningC statements
SequenceStatements run one after anotherDefault behaviour
Selection (decision)Choose which statements run, based on a conditionif, if–else, switch
Iteration (loop)Repeat statementswhile, do–while, for

A control statement is a statement that changes the order in which other statements are executed.

The simple if statement

Syntax
if (condition)
    statement;

if (condition) {
    statement1;
    statement2;
}

The condition is evaluated. If it is non-zero (true), the statement or block runs; if it is zero (false), it is skipped. Either way, execution continues with the statement after the if. Braces { } group several statements into one block; without braces, only the first statement belongs to the if.

Flowchart of a simple if statement

The if–else statement

Syntax
if (condition) {
    statements executed when the condition is true
} else {
    statements executed when the condition is false
}

Exactly one of the two blocks runs, never both and never neither.

For a choice between two values, the conditional operator from Unit 2 is a compact alternative: y = (x >= 6) ? 6 : x;. Use if–else when each branch has to do something.

Key takeaways

  • Control statements change the default top-to-bottom order: selection and iteration.
  • if (condition) runs its statement only when the condition is non-zero.
  • if–else runs exactly one of two blocks.
  • Use braces for blocks of more than one statement.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does this program print?

C · if_semicolon.c
#include <stdio.h>

int main(void)
{
    int x = 3;
    if (x > 5);
        printf("big\n");
    printf("done\n");
    return 0;
}
Show answer

Answer: B. big, then done (on two lines)

The semicolon after if (x > 5) is an empty statement, so the if controls nothing and printf("big\n") always runs. The indentation misleads you into expecting only “done”.

Question 2 · multiple choice

Which statement is true about if–else?

Show answer

Answer: C. Exactly one block runs

The condition is either true or false, so exactly one of the two blocks runs.

Question 3 · multiple choice

In C, which value of a condition counts as false?

Show answer

Answer: A. 0

Only zero is false; every non-zero value, including negative numbers, is true.

Question 4 · multiple choice

With int x = 0;, what does if (x = 5) printf("yes"); else printf("no"); print?

Show answer

Answer: D. yes

x = 5 assigns 5, and the value of the assignment (5) is non-zero, so the condition is true. The intended comparison is x == 5.

Question 5 · multiple choice

Which structure does C use by default when there is no control statement?

Show answer

Answer: C. Sequence

Statements run one after another in the order written; selection and iteration require control statements.

Question 6 · short answer

Write an if–else statement that prints “Pass” when marks is at least 40 and “Fail” otherwise, and trace it for marks = 40.

Show worked answer

if (marks >= 40) printf("Pass\n"); else printf("Fail\n");. For marks = 40, the condition 40 >= 40 is true (1), so the first branch runs and “Pass” is printed; the else branch is skipped.

↑ ↓ move↵ openTab switch type