ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-112Structured Programming…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-112

    Programming Fundamentals

    Progress0 / 39 done
    Topics39
    1Introduction to Problem Solving, Algorithms, Programming, and C Language2Problem Solving, a brief review of Von-Neumann Architecture3The C Programming Language, Pseudo-code, Concept of Variable4Data types in Pseudo-code, The C Standard Library and Open Source5Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6Concept of Integer division, Flowchart and its notations7Typical C Program Development Environment, Role of Compiler and Linker8Test Driving C Application
    9A Simple C Program: Printing Text, Adding Two Integer10Memory Concepts, Arithmetic in C, Operators
    11Equality and Relational Operators
    12The if, if...else, while Nested Control Statements
    13for, switch, do...while, break, continue, Logical Operators
    14Modularizing Program in C, Math Library Functions15Function Definitions and Prototypes, Function-Call Stack and Stack Frames16Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration
    18Defining Arrays, Character Arrays, Static and Automatic Local Arrays19Passing Arrays to Function, Sorting and Searching Arrays20Multidimensional and Variable Length Arrays
    21Pointer Definitions and Initialization, Pointer Operators22Passing Arguments to Function by Reference, Using the const and sizeof Operator23Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24Function Pointers
    25Strings and Characters, Character Handling Library26String Functions, Library Functions
    27Streams, Formatted Output with printf, Formatted Input with scanf
    28Defining Structures, Accessing Structure Member, Structures and Functions29typedef, Unions
    30Bitwise Operators, Bit Fields, Enumeration Constants
    31Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File
    32#include, #define, Conditional Compilation, #error and #pragma33# and ## Operators, Predefined Symbolic Constants, Assertions
    34Variable Length Argument List, Using Command Line Arguments35Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36Suffixes for Integer and Floating-Point Literals, Signal Handling
    37calloc and realloc, goto
    38Self-Referential Structures, Linked Lists39Efficiency of Algorithms, Selection and Insertion Sort
    7 min read
    Topic 12 of 39
    Topic Study Note Illustration
    12

    Structured Program Development: The if, if...else, while Nested Control Statements

    Structured programming is a programming paradigm that emphasizes breaking down a program into smaller, manageable blocks of code. It encourages the use of control structures like if...

    Structured Program Development in C

    Structured programming is a programming paradigm that emphasizes breaking down a program into smaller, manageable blocks of code. It encourages the use of control structures like if, if...else, while loops, and nested control statements to create readable, efficient, and error-free code. These control statements allow a program to make decisions, repeat actions, and manage complexity.

    In C, structured program development often involves using control structures to determine the flow of execution. Let’s dive into the key control statements: if, if...else, while, and nested control structures.


    1. The if Statement

    The if statement in C allows you to execute a block of code if a condition is true. If the condition is false, the code inside the if block is skipped.

    Syntax:

    if (condition) {
        // Code to be executed if condition is true
    }
    

    Example:

    #include <stdio.h>
    
    int main() {
        int number = 5;
    
        if (number > 0) {
            printf("The number is positive.\n");
        }
    
        return 0;
    }
    

    Explanation:

    • The condition number > 0 is checked. If it is true (which it is), the message "The number is positive." is printed.

    Output:

    The number is positive.
    

    2. The if...else Statement

    The if...else statement is an extension of the if statement. It allows you to specify a block of code to be executed when the condition is false, in addition to the block executed when the condition is true.

    Syntax:

    if (condition) {
        // Code to be executed if condition is true
    } else {
        // Code to be executed if condition is false
    }
    

    Example:

    #include <stdio.h>
    
    int main() {
        int number = -5;
    
        if (number > 0) {
            printf("The number is positive.\n");
        } else {
            printf("The number is negative or zero.\n");
        }
    
        return 0;
    }
    

    Explanation:

    • The condition number > 0 is false, so the program executes the code inside the else block and prints "The number is negative or zero.".

    Output:

    The number is negative or zero.
    

    3. The while Loop

    The while loop is used for repeating a block of code as long as the condition is true. If the condition is false initially, the code inside the loop is never executed.

    Syntax:

    while (condition) {
        // Code to be executed as long as condition is true
    }
    

    Example:

    #include <stdio.h>
    
    int main() {
        int i = 1;
    
        while (i <= 5) {
            printf("i is %d\n", i);
            i++;  // Increment i by 1
        }
    
        return 0;
    }
    

    Explanation:

    • The while loop runs as long as the condition i <= 5 is true.
    • On each iteration, the value of i is printed and then incremented.
    • Once i becomes greater than 5, the condition is no longer true, and the loop terminates.

    Output:

    i is 1
    i is 2
    i is 3
    i is 4
    i is 5
    

    4. Nested Control Statements

    A nested control statement is when you place one control statement (like an if, while, or for loop) inside another control statement. This allows for more complex decision-making and logic flow in your programs.

    Example of Nested if Statement:

    #include <stdio.h>
    
    int main() {
        int x = 10, y = 20;
    
        if (x > 0) {
            if (y > 0) {
                printf("Both x and y are positive.\n");
            } else {
                printf("x is positive, but y is not.\n");
            }
        } else {
            printf("x is not positive.\n");
        }
    
        return 0;
    }
    

    Explanation:

    • The first if checks if x > 0.
    • Inside the first if, there is another if statement that checks if y > 0. This creates a nested structure.
    • If both conditions are true, the program prints "Both x and y are positive.".

    Output:

    Both x and y are positive.
    

    Example of Nested while Loop:

    #include <stdio.h>
    
    int main() {
        int i = 1, j;
    
        while (i <= 3) {
            j = 1;
            while (j <= 3) {
                printf("i = %d, j = %d\n", i, j);
                j++;  // Increment j
            }
            i++;  // Increment i
        }
    
        return 0;
    }
    

    Explanation:

    • The outer while loop iterates with i from 1 to 3.
    • For each value of i, the inner while loop iterates with j from 1 to 3.
    • This produces a pair of nested loops, printing each combination of i and j.

    Output:

    i = 1, j = 1
    i = 1, j = 2
    i = 1, j = 3
    i = 2, j = 1
    i = 2, j = 2
    i = 2, j = 3
    i = 3, j = 1
    i = 3, j = 2
    i = 3, j = 3
    

    5. Example: Combining if...else, while, and Nested Control Statements

    Let’s combine if...else, while loops, and nested control statements to solve a simple problem. We will create a program to check if a number is prime.

    Prime Number Example:

    A prime number is a number greater than 1 that has no divisors other than 1 and itself. The program will check whether a number is prime or not.

    #include <stdio.h>
    
    int main() {
        int number, i, is_prime = 1;
    
        printf("Enter a number: ");
        scanf("%d", &number);
    
        if (number <= 1) {
            printf("Number must be greater than 1 to be prime.\n");
        } else {
            i = 2;
            while (i < number) {
                if (number % i == 0) {
                    is_prime = 0;  // Set to 0 if divisible
                    break;         // Exit the loop as no need to check further
                }
                i++;
            }
    
            if (is_prime == 1) {
                printf("%d is a prime number.\n", number);
            } else {
                printf("%d is not a prime number.\n", number);
            }
        }
    
        return 0;
    }
    

    Explanation:

    • The program asks for the number to check.
    • If the number is less than or equal to 1, it is not prime, and a message is printed.
    • The while loop checks for divisibility from i = 2 to number - 1.
    • If any divisor is found, the flag is_prime is set to 0, and the loop breaks early.
    • After the loop, the program prints whether the number is prime or not.

    Output (for number = 7):

    Enter a number: 7
    7 is a prime number.
    

    Output (for number = 10):

    Enter a number: 10
    10 is not a prime number.
    

    Summary

    • if Statement: Executes a block of code if the condition is true.
    • if...else Statement: Executes one block of code if the condition is true and another block if it is false.
    • while Loop: Repeats a block of code as long as the condition is true.
    • Nested Control Statements: Placing one control structure inside another for more complex logic.

    By using these control statements effectively, you can create structured and logical programs that handle various scenarios, repeat tasks, and make decisions based on conditions.

    ← Previous topic 11
    Decision Making: Equality and Relational Operators
    Next topic 13 →
    Program Control: for, switch, do...while, break, continue, Logical Operators

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals