Docsity

Python Notes - Beginner to Advanced + Practical Knowledge, Study notes of Web Application Development

Toggle sidebar
bg1
Module 4
PROGRAMMING IN C
Q1. 2 MARKS QUESTIONS.
1.
What is a loop in C?
➢
Purpose of loop
is
as
follows:
• A loop in C is a control structure that allows a set of statements to be
executed repeatedly as long as a specific condition remains true.
It helps in performing repetitive tasks without writing the same code
multiple times.
Loops reduce program length, improve efficiency, and automate repeated
operations.
Common loops in C programming are for, while, and do-while.
2.
Why do we use loops in C?
➢ Need for loops is as follows:
• We use loops in C to execute a block of code multiple times without
rewriting it.
Loops make programs short, efficient, and easier to manage when
handling repeated tasks.
They are useful for operations like printing sequences, reading inputs
repeatedly, and processing data.
Loops help reduce errors and improve overall program performance.
3.
Define an infinite loop with one example.
➢ An infinite loop is a loop that never ends because its condition always remains true.
It continuously executes the loop body without stopping.
Infinite loops are sometimes used in system programs or event-driven applications.
Example: while(1) { printf("Hello"); }
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Python Notes - Beginner to Advanced + Practical Knowledge and more Study notes Web Application Development in PDF only on Docsity!

PROGRAMMING IN C

Q1. 2 MARKS QUESTIONS.

  1. What is a loop in C? ➢ Purpose of loop is as follows:
    • A loop in C is a control structure that allows a set of statements to be executed repeatedly as long as a specific condition remains true. It helps in performing repetitive tasks without writing the same code multiple times. Loops reduce program length, improve efficiency, and automate repeated operations. Common loops in C programming are for , while , and do-while.
  2. Why do we use loops in C? ➢ Need for loops is as follows:
    • We use loops in C to execute a block of code multiple times without rewriting it. Loops make programs short, efficient, and easier to manage when handling repeated tasks. They are useful for operations like printing sequences, reading inputs repeatedly, and processing data. Loops help reduce errors and improve overall program performance.
  3. Define an infinite loop with one example. ➢ An infinite loop is a loop that never ends because its condition always remains true. It continuously executes the loop body without stopping. Infinite loops are sometimes used in system programs or event-driven applications. Example: while(1) { printf("Hello"); }

PROGRAMMING IN C

  1. What is the difference between while and do-while statement in C.
  2. What does the continue statement do? ➢ The continue statement skips the remaining statements inside a loop for the current iteration. Control jumps to the next iteration of the loop without exiting it. It is mainly used when some values need to be skipped during loop processing. Continue modifies loop behavior without stopping the loop.
  3. What is the purpose of the break statement? ➢ The break statement is used to immediately terminate a loop or switch block. When executed, control jumps to the statement that appears just after the loop or switch. It is commonly used to stop early when a specific condition is met. This prevents execution of unwanted iterations or cases.
  4. What is the use of the goto statement in C? ➢ The goto statement allows an unconditional jump to a labeled statement within a program. It is used to transfer control to another part of the code instantly. Although rarely recommended, it can simplify exiting from deeply nested loops. The label and goto must be inside the same function.

PROGRAMMING IN C

  1. Explain the while loop with syntax and example. ➢ A while loop in C is a control structure used to repeat a block of code as long as a given condition remains true. It is known as an entry-controlled loop , because the condition is checked before executing the loop body. This means if the condition is false initially, the statements inside the loop will not execute even once. The loop continues executing repeatedly until the condition becomes false. while loop is most useful when the number of iterations is not known in advance and depends on logical conditions or user inputs. The working process of the while loop is as follows:
  2. A condition is evaluated first.
  3. If the condition is true , the statements inside the loop are executed.
  4. After execution, control goes back to the condition again.
  5. If still true → loop repeats; if false → loop stops. This makes the loop ideal for situations like reading input until user stops, processing data until a condition satisfies, and waiting-based programs. Unlike for loop, initialization and increment are written separately, giving more freedom for conditional programming. Syntax: - while(condition) { statements; } Example Program: - #include int main() { int i = 1 ; while(i <= 5 ) { printf("%d ", i); i++; } return 0 ; } Output: - 1 2 3 4 5

PROGRAMMING IN C

  1. Explain do-while loop. Why does it execute at least once? ➢ The do-while loop in C is another looping structure used to execute statements repeatedly. Unlike the while loop, it is an exit-controlled loop , meaning the condition is evaluated

only after the loop body has executed.

Because the condition is checked later, the statements written inside the do-while block run at least one time , even if the condition is false initially. This unique nature makes it different from for and while loops. It is widely used in menu-driven programs, repeated user input, and retry-based operations , where execution must occur before validation. Why it executes minimum one time? In a do-while loop, the control enters the loop first, executes the block, and then checks the condition. If the condition is true, the loop repeats, otherwise it exits. This ensures one guaranteed execution regardless of the condition value — even if false from the beginning. Syntax: - do { statements; } while(condition); Example Program: - #include int main() { int i = 1 ; do { printf("%d ", i); i++; } while(i <= 5 ); return 0 ; } Output: - 1 2 3 4 5 Explanation Here the loop prints numbers starting from 1. Even if the condition is wrong at the beginning, the block executes once before checking. After printing, the increment updates i, then condition (i <= 5) is checked. The loop continues until i becomes 6, after which condition becomes false and loop stops. Therefore, a do-while loop is best when a program must execute once before checking conditions — like menus, password attempts, games, and user-driven tasks.

PROGRAMMING IN C

Output Example: Using for loop: 1 2 3 4 5 6 7 8 9 10 Using while loop: 1 2 3 4 5 6 7 8 9 10 Using do-while loop: 1 2 3 4 5 6 7 8 9 10 Explanation: All three loops successfully print numbers from 1 to 10 but differ in control mechanism. for loop handles initialization, condition, and increment in a single statement. while loop checks condition before execution — may run zero times. do-while loop prints first, then checks condition — guarantees one execution. Thus, although the task and output remain same, the working style of each loop is different, making this example perfect for understanding loop behavior in C.

PROGRAMMING IN C

  1. Explain Infinite Loops with examples. ➢ An infinite loop in C is a loop that continues execution endlessly without stopping. This happens when the loop condition always remains true, or when the control variable is never updated to make the condition false. Because the program never exits the loop naturally, it continues to run until forcefully terminated by the user or system. Infinite loops are sometimes useful and not always a mistake — they are widely used in operating systems, servers, menu-driven programs, event monitoring, embedded systems, and background processes where execution must continue without stopping. An infinite loop is created either knowingly (for continuous execution) or unknowingly (programming error). If the loop variable is not incremented or the condition never becomes false, the loop enters a non-terminating state. All three loops (for, while, and do-while) have the ability to generate infinite loops if conditions are not properly controlled. A programmer must handle them carefully to avoid program freeze or CPU overload. Example using while Loop: - while( 1 ) { printf("Running "); } Here, condition 1 is always true → infinite execution. Example using for Loop: - for(;;) { printf("Hello "); } In a for loop, missing all three components means it runs forever automatically. Example using do-while Loop: - do { printf("Loop "); } while( 1 ); The condition 1 is permanently true, so output prints continuously. Explanation: - Each of the above loops keeps executing continuously without termination. They do not stop because no condition becomes false or exits the loop. Infinite loops can be intentionally used when continuous checking or response is needed, such as clock displays, sensors, live counters, and waiting screens. However, they should be used carefully and normally combined with break statement to exit safely when required.

PROGRAMMING IN C

  1. Write a program using goto to print numbers 1 to 10. Explain how it works. ➢ The goto statement in C is a jump control statement that transfers execution unconditionally from one part of the program to another. It is used along with a label , which acts as a destination point. When the goto keyword executes, control immediately jumps to the label name mentioned, skipping all statements in between. Although goto is generally avoided in modern programming because it may reduce readability, it is still useful in some cases like exiting from multiple nested loops, handling error jumps, retrying invalid input, and breaking lengthy structures quickly. It provides powerful directional control, but must be used carefully to avoid confusing and tangled code. In this question, we use goto to repeatedly print numbers from 1 to 10. Instead of using loops like for, while, or do-while, we allow the program flow to circle back to a label using goto, creating a manual loop. The program continues jumping to the label until a stopping condition is reached. Program: - #include #include int main() { int i = 1 ; start: printf("%d ", i); i++; if(i <= 10 ) goto start; getch(); return 0 ; } Output: - 1 2 3 4 5 6 7 8 9 10 Explanation
  • The variable i starts from 1.
  • The labeled line start: prints the value of i.
  • After printing, i is increased by 1.
  • If i is less than or equal to 10, the goto start; statement sends control back to the label.
  • This cycle continues until i becomes 11, after which the condition fails and print stops.

PROGRAMMING IN C

  1. Compare for, while, and do-while loops in C. ➢

PROGRAMMING IN C

  1. Explain for, while and do-while loops in C with syntax, flow and proper programs. ➢ Loops in C are used to execute a set of statements repeatedly until a specific condition becomes false. They make programs shorter, faster, and more efficient by avoiding repeated code. C provides three main looping structures — for loop , while loop , and do-while loop. Although all three achieve repetition, they differ in condition checking, structure, and use cases. Understanding them is important for algorithm development and logical coding. 1. for Loop: - A for loop is used when the number of iterations is known beforehand. It is compact because initialization, condition and increment/decrement are written together. It is an entry-controlled loop , meaning condition is checked first before executing. Syntax for(initialization; condition; increment/decrement) { statements; } Proper Example Program: - #include #include int main() { int i; printf("Printing numbers using for loop:\n"); for(i = 1 ; i <= 5 ; i++) { printf("%d ", i); } getch(); return 0 ; } Output: - Printing numbers using for loop: 1 2 3 4 5

PROGRAMMING IN C

2. while Loop: - A while loop executes only if the condition is true at the beginning. It is used when the number of repetitions is not fixed and depends on logic or input. Initialization and increment must be written separately. Syntax: - while(condition) { statements; } Proper Example Program: - #include #include int main() { int i = 1 ; printf("Printing numbers using while loop:\n"); while(i <= 5 ) { printf("%d ", i); i++; } getch(); return 0 ; } Output: - Printing numbers using while loop: 1 2 3 4 5

PROGRAMMING IN C

  1. Explain jump statements in C (break, continue, goto, return) with syntax, flow, use-cases and proper examples. ➢ Jump statements in C are used to alter the normal flow of program execution. They allow the control to break, skip, jump, or exit instantly instead of flowing sequentially. C supports four main jump statements: **1. break statement
  2. continue statement
  3. goto statement
  4. return statement** Each plays a different role in program control and is used in loops, switch cases, or function execution. 1) break Statement: - The break statement is used to exit immediately from a loop or switch block. As soon as break executes, control moves outside the loop/switch, ignoring remaining statements. It is useful when early termination is needed based on condition. Syntax: break; Proper Example Program: #include #include int main() { char grade; printf(“Enter Grade”); scanf(“%c”, &grade); switch(grade){ case “A”: printf(“Excellent”); break; default: printf(“Invalid”); break; } return0; getch(); } Output: Enter Grade: A Excellent Explanation: Loop stops completely when a = excellent is executed.

PROGRAMMING IN C

2) continue Statement: - The continue statement skips the current iteration of the loop. It does not exit the loop but jumps to the next cycle directly. Syntax: continue; Proper Example Program: #include #include int main() { int i; printf("Demonstration of continue statement:\n"); for(i = 1 ; i <= 6 ; i++) { if(i == 3 || i == 5 ) continue; printf("%d ", i); } getch(); return 0 ; } Output: 1 2 4 6 Explanation: Only 3 and 5 are skipped. Loop continues normally for other values. 3) goto Statement: - The goto statement is used to jump control to a labeled statement within same function. It can perform loop-like behaviour and jump anywhere instantly. However, excessive use may make programs unstructured. Syntax: goto label; label: statements; Proper Example Program: #include #include int main() { int i = 1 ; start: printf("%d ", i); i++; if(i <= 10 ) goto start; getch(); return 0 ; }

PROGRAMMING IN C

  1. Explain Infinite Loops in C with proper programs for for, while & do-while. ➢ An infinite loop is a loop that never terminates on its own. It continues executing repeatedly because the loop condition never becomes false or no condition exists to stop execution. Infinite loops are not always errors — sometimes they are intentionally created for continuous event monitoring, server programs, waiting screens, embedded systems, sensor monitoring, operating systems, background services, login screens, and more. Programmers sometimes introduce infinite loops knowingly and break them using break, exit() or external input. The main reason behind infinite loops is: ✔ A condition that is always TRUE ✔ Increment/decrement missing ✔ Logical mistake in condition ✔ Using for(;;) or while(1) structures In C, all three loops — for, while & do-while — can create infinite loops with very small syntax changes.

PROGRAMMING IN C

**C Program:

  1. Infinite while Loop: -** #include #include int main() { while( 1 ) { printf("Infinite WHILE loop\n"); } getch(); return 0 ; } 2) Infinite for Loop: - #include #include int main() { for(;;) { printf("Infinite FOR loop\n"); } getch(); return 0 ; } 3) Infinite do-while Loop: - #include #include int main() { int i = 1 ; do { printf("Infinite DO-WHILE loop\n"); // i is never changed → condition always true } while(i == 1 ); getch(); return 0 ; }