ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    CC-112
    Progress0 / 39 topics
    Topics
    1. Introduction to Problem Solving, Algorithms, Programming, and C Language2. Problem Solving, a brief review of Von-Neumann Architecture3. The C Programming Language, Pseudo-code, Concept of Variable4. Data types in Pseudo-code, The C Standard Library and Open Source5. Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6. Concept of Integer division, Flowchart and its notations7. Typical C Program Development Environment, Role of Compiler and Linker8. Test Driving C Application9. Introduction to C Programming: A Simple C Program: Printing Text, Adding Two Integer10. Memory Concepts, Arithmetic in C, Operators11. Decision Making: Equality and Relational Operators12. Structured Program Development: The if, if...else, while Nested Control Statements13. Program Control: for, switch, do...while, break, continue, Logical Operators14. Functions: Modularizing Program in C, Math Library Functions15. Function Definitions and Prototypes, Function-Call Stack and Stack Frames16. Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17. Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration18. Arrays: Defining Arrays, Character Arrays, Static and Automatic Local Arrays19. Passing Arrays to Function, Sorting and Searching Arrays20. Multidimensional and Variable Length Arrays21. Pointers: Pointer Definitions and Initialization, Pointer Operators22. Passing Arguments to Function by Reference, Using the const and sizeof Operator23. Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24. Function Pointers25. Characters and Strings: Strings and Characters, Character Handling Library26. String Functions, Library Functions27. Formatted Input/Output: Streams, Formatted Output with printf, Formatted Input with scanf28. Structures: Defining Structures, Accessing Structure Member, Structures and Functions29. typedef, Unions30. Bit Manipulation and Enumeration: Bitwise Operators, Bit Fields, Enumeration Constants31. File Processing: Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File32. Preprocessor: #include, #define, Conditional Compilation, #error and #pragma33. # and ## Operators, Predefined Symbolic Constants, Assertions34. Other Topics: Variable Length Argument List, Using Command Line Arguments35. Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36. Suffixes for Integer and Floating-Point Literals, Signal Handling37. Dynamic Memory Allocation: calloc and realloc, goto38. Advance Topics: Self-Referential Structures, Linked Lists39. Efficiency of Algorithms, Selection and Insertion Sort
    CC-112›Structured Program Development: The if, if...else, while Nested Control Statements
    Programming FundamentalsTopic 12 of 39

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

    7 minread
    1,225words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,225
      Code examples0
      DifficultyIntermediate