You are on page 1of 13

Unit – 3

1. Illustrate loop control structures with suitable examples.


Ans:

Loop control structures are used in programming to execute a block of code repeatedly for a fixed
number of times or until a certain condition is met.

The for loop:

The for loop is used to execute a block of code repeatedly for a fixed number of times. The syntax of
the for loop is as follows:

for (initialization; condition; increment/decrement) {

// code to be executed

The initialization part initializes the loop variable, the condition part checks whether the loop should
continue or not, and the increment/decrement part modifies the loop variable on each iteration.

The while loop:

The while loop is used to execute a block of code repeatedly as long as a certain condition is true.
The syntax of the while loop is as follows:

while (condition) {

// code to be executed

The code in the loop will continue to execute as long as the condition is true.

The do-while loop:

The do-while loop is similar to the while loop, but the code in the loop is executed at least once
before the condition is checked. The syntax of the do-while loop is as follows:

do {

// code to be executed

} while (condition);

The code in the loop will execute once before the condition is checked, and will continue to execute
as long as the condition is true.
2. How would you explain the switch statement with an example?
Ans:

The switch statement is a multiway branch statement. Switch case statements are a substitute for
long if statements that compare a variable to several integral values. It provides an easy way to
dispatch execution to different parts of code based on the value of the expression. The switch is a
control statement that allows a value to change the control of execution.

For Example:-

#include <stdio.h>
int main() {
int choice;
printf("Select a number between 1 and 3: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose option 1\n");
break;
case 2:
printf("You chose option 2\n");
break;
case 3:
printf("You chose option 3\n");
break;
default:
printf("Invalid choice\n");
break;
}
return 0;
}

In this example, the program prompts the user to enter a number between 1 and 3. It then uses a
switch statement to execute different codes based on the value of the choice variable. If the user
enters 1, the program prints "You chose option 1". If the user enters 2, the program prints "You chose
option 2". If the user enters 3, the program prints "You chose option 3". If the user enters any other
number, the program prints "Invalid choice". The break statement is used to exit the switch statement
after each case.
3. How would you explain if-else statement with example?

Ans:

In C programming, the if-else statement is used to execute a block of code conditionally. The basic
syntax of the if-else statement in C is:

if (condition) {
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}

Here's an example to illustrate the use of the if-else statement in C:

#include <stdio.h>

int main() {
int num = 10;

if (num % 2 == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}

return 0;
}

In this example, we declare a variable num and assign it the value of 10. We then use an if-else
statement to check whether num is even or odd. If the condition num % 2 == 0 is true, the program
will execute the code inside the if block, which will print the message "10 is even". Otherwise, the
program will execute the code inside the other block, printing the message "10 is odd".

So, in this way, if-else statement helps us to execute different blocks of code based on whether a
certain condition is true or false.
4. How do “break” and “continue” works? Explain with example.

Ans:

break: The break statement is used to terminate a loop early. When the break statement is
executed inside a loop, the loop is immediately terminated and the program continues with the
next statement after the loop.

Here's an example of how the break statement works in a while loop in C:

#include <stdio.h>

int main() {
int i = 1;

while (i <= 10) {


printf("%d ", i);
i++;

if (i == 6) {
break; // terminate the loop if i equals 6
}
}

printf("\n");
printf("Loop terminated early\n");

return 0;
}

In this example, we use a while loop to print the numbers from 1 to 10. We use an if statement
inside the loop to check if the variable i equals 6. If i is 6, the break statement is executed, which
terminates the loop early. As a result, the program skips the rest of the loop and prints the
message "Loop terminated early".

The output of this program will be:

1 2 3 4 5
Loop terminated early

continue: The continue statement is used to skip the current iteration of a loop and move on to
the next iteration. When the continue statement is executed inside a loop, the loop continues
with the next iteration, skipping any code that comes after the continue statement in that
iteration.

Here's an example of how the continue statement works in a for loop in C:


#include <stdio.h>

int main() {
int i;

for (i = 1; i <= 10; i++) {


if (i % 2 == 0) {
continue; // skip even numbers
}

printf("%d ", i);


}

printf("\n");
printf("Loop complete\n");

return 0;
}

In this example, we use a for loop to print the odd numbers from 1 to 10. We use an if statement
inside the loop to check if the variable i is even. If i is even, the continue statement is executed,
which skips the rest of the loop for that iteration and moves on to the next iteration. As a result,
the program prints only the odd numbers.

The output of this program will be:

1 3 5 7 9
Loop complete

So, in summary, break and continue are two powerful control statements that allow us to modify
the behavior of loops in C.
4. List the differences between while loop and do-while loop.

Ans:
5. Write a short note on switch case structure for decision making. How does it differ from an if
structure in C programming?

Ans:

The switch case structure is a decision-making statement in C programming that provides a way
to execute different blocks of code based on the value of a single expression.

The basic syntax of the switch statement is as follows:

switch (expression) {

case value1:
// code to execute if expression equals value1
break;
case value2:
// code to execute if expression equals value2
break;
// and so on
default:
// code to execute if none of the cases match
break;
}

The switch statement compares the value of the expression with each of the case values. The code
inside that case block is executed if a match is found, and the program exits the switch statement. If
no match is found, the code inside the default block is executed (if it exists).

The switch statement differs from the if structure, allowing for multiple tests based on a single
expression. It is often used when there are many possible conditions to test and simplify the code,
especially when each condition only requires a simple comparison.

In contrast, the if structure allows for more complex conditions and tests, and it can be used to test
multiple conditions with the use of logical operators (&&, ||). It is often used when conditions need
to be checked more thoroughly.

Overall, both switch and if structures are important decision-making statements in C programming
and are used depending on the program’s specific needs.
6. What are the different types of decision control structures offered by C Programming?

Ans:

C Programming language offers three different types of decision control structures:

if statement: The if statement is the simplest decision-making statement in C. It is used to


execute a block of code if a particular condition is true. The basic syntax of the if statement is:

if (condition) {
// code to execute if the condition is true
}

if-else statement: The if-else statement is used to execute one block of code if a particular
condition is true, and another block of code if the same condition is false. The basic syntax of the
if-else statement is:

if (condition) {
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}

switch statement: The switch statement is used to execute different blocks of code based on the
value of a variable or expression. It provides a more concise and readable way to test multiple
conditions than using multiple if-else statements. The basic syntax of the switch statement is:

switch (expression) {
case value1:
// code to execute if expression equals value1
break;
case value2:
// code to execute if expression equals value2
break;
// and so on
default:
// code to execute if none of the cases match
break;
}

In summary, these three decision control structures in C - if, if-else, and switch - provide different
ways to execute different blocks of code based on certain conditions or values.
7. Explain use of logical operators in decision control statements. Give examples.

Ans:

Logical operators are used in decision control statements to combine multiple conditions and
create more complex tests. In C, there are three logical operators:

&& (logical AND): This operator returns true if both conditions are true, and false otherwise.

For example, the following if statement tests whether both x and y are greater than 0 before
executing the code inside the block:

if (x > 0 && y > 0) {


// code to execute if x and y are both greater than 0
}

|| (logical OR): This operator returns true if at least one of the conditions is true, and false
otherwise.

For example, the following if statement tests whether either x is greater than 0 or y is greater than
0 before executing the code inside the block:

if (x > 0 || y > 0) {
// code to execute if either x or y (or both) are greater than 0
}

! (logical NOT): This operator negates the value of the condition, returning true if the condition is
false and false if the condition is true.

For example, the following if statement tests whether x is not equal to 0 before executing the
code inside the block:

if (x != 0) {
// code to execute if x is not equal to 0
}

In summary, logical operators allow for more complex tests in decision control statements in C.
They can be used to combine multiple conditions and create tests based on multiple variables or
expressions.
8. State the drawbacks of ladder if-else. Explain how do you resolve with suitable example.

Ans

The ladder if-else structure in C programming is a series of if-else statements that are executed in
sequence until a condition is met. While this structure can be useful in certain situations, it has
some drawbacks that can make it difficult to read and maintain as the number of conditions
increases. Some of the drawbacks of the ladder if-else structure include:

• Code redundancy: The same code may be repeated multiple times in each if block, leading
to code redundancy and maintenance issues.
• Performance: As the number of if-else statements increases, the performance of the code
may suffer.
• Difficulty debugging: As the ladder if-else structure becomes more complex, debugging
and finding errors can be difficult.

We can use the switch-case structure in C programming to resolve these drawbacks. The switch-
case structure is used when there are many possible conditions to test and simplify the code,
especially when each condition only requires a simple comparison.

The basic syntax of the switch-case structure is as follows:

switch (expression) {
case value1:
// code to execute if expression equals value1
break;
case value2:
// code to execute if expression equals value2
break;
// and so on
default:
// code to execute if none of the cases match
break;
}
Here is an example that demonstrates how to replace the ladder if-else structure with switch-
case:

// ladder if-else
if (x == 1) {
// code to execute if x equals 1
} else if (x == 2) {
// code to execute if x equals 2
} else {
// code to execute if none of the above conditions are true
}

// switch-case
switch (x) {
case 1:
// code to execute if x equals 1
break;
case 2:
// code to execute if x equals 2
break;
default:
// code to execute if none of the above conditions are true
break;
}

In this example, we can see that the switch-case structure provides a more concise and easier-to-
read code as compared to the ladder if-else structure. It also eliminates the need for redundant
code and simplifies the debugging process.
9. Write the syntax of different branching statements and explain their working.

Ans:

There are three branching statements in C programming: break, continue, and goto. Here is the
syntax and working of each of these statements:

• break: The break statement is used to exit a loop or switch statement before its normal
completion. The syntax of the break statement is as follows:
break;

When the break statement is encountered inside a loop or switch statement, the control
flow immediately exits the loop or switch, and the program continues executing from the
next statement after the loop or switch.

Here is an example of how to use the break statement in a while loop:

int i = 1;
while (i <= 10) {
if (i == 5) {
break; // exit the loop when i equals 5
}
printf("%d\n", i);
i++;
}

In this example, the loop will print numbers 1 through 4, then exit the loop when i is equal
to 5 due to the break statement.

• continue: The continue statement is used to skip the current iteration of a loop and
move on to the next iteration. The syntax of the continue statement is as follows:
continue;
When the continue statement is encountered inside a loop, the control flow immediately
goes back to the beginning of the loop and starts the next iteration.

Here is an example of how to use the continue statement in a for loop:

for (int i = 1; i <= 10; i++) {


if (i % 2 == 0) {
continue; // skip even numbers
}
printf("%d\n", i);
}

In this example, the loop will print only odd numbers (1, 3, 5, 7, and 9), as the continue
statement skips even numbers.
• goto: The goto statement is used to transfer control to a labeled statement in the same
function. The syntax of the goto statement is as follows:

goto label;
Here, label is the name of the labeled statement that the control will be transferred to.

Here is an example of how to use the goto statement to transfer control to a labeled
statement:

int i = 1;
loop:
if (i <= 10) {
printf("%d\n", i);
i++;
goto loop; // transfer control to the "loop" label
}

In this example, the loop will print numbers 1 through 10 using a goto statement to
transfer control to the labeled statement called "loop". However, it is generally
recommended to avoid using the goto statement in C programming, as it can make the
code harder to understand and maintain.

You might also like