0% found this document useful (0 votes)
16 views5 pages

Decision Control Statements Explained

Uploaded by

lakshyagpta8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Decision Control Statements Explained

Uploaded by

lakshyagpta8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Decision Control Statement

1. If statement
2. Switch statement
3. Conditional operator statement
4. Goto statement
Decision Making with if statement
1. Simple if statement
2. if……..else statement
3. Nested if……..else statement
4. Else if ladder
[Link] if statement
The syntax for simple if statement is as follows:
if(test_condition)
{
Statement_block; //single or multiple statements
}
Statement-xyz;
Here is the test_condition is true than the statement block will be executed.
If the test_condition is false; the statement block will be skipped and execution
will jump to the statement-xyz.
Question 1: WAP to give bonus marks to a student in a subject if marks
obtained by the student is greater than or equal to 80. (Consider bonus marks
=10).
#include<stdio.h> void
main()
{ int bonus=10, marks;
printf("Enter marks of student\n");
scanf("%d",&marks);
if(marks>=80)
{
marks=marks+bonus;
} printf("The marks obtained
are= %d",marks);
}
OUTPUT:
Enter marks of student
90
The marks obtained are= 100

[Link]……..else statement
It is the extension of simple if statement.
The general syntax is:
if(test_condition)
{
True statement block(s)
}
else
{
False statement block(s)
}
Statement-xyz;
Question 2: WAP to check if the person is eligible for voting or not.
#include<stdio.h> void
main() { int age;
printf("Enter your age\n");
scanf("%d",&age);
if(age>=18)
{ printf("You are eligible for
voting");
} else { printf("You are not eligible for
voting");
}
}
OUTPUT:
Enter your age
56
You are eligible for voting.
Practice Question 1: WAP to check if the student has passed or failed
in a subject. (consider passing marks greater than equal to 40).
3. Nested if……else statement
When series of decision are involved, we may have to use more than one
if….else statement in nested form.
The syntax is as follows:

if(test_condition-1)
{
if(test condition-2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-xyz;

Question 3: WAP to print the largest of three numbers using nested if…else
statement.

#include<stdio.h> void
main()
{ int A,B,C; printf("Enter
numbers\n");
scanf("%d%d%d",&A,&B,&C);
printf("\nThe largest Number is\n");
if(A>B) { if(A>C)

{
printf("A is greatest=%d",A);
}
else
{
printf("B is greatest=%d",B);
}
} else
{ if(C>
B)
{
printf("C is greatest%d",C);
}
else
{
printf("B is Greatest%d",B);
}

}
}

Common questions

Powered by AI

The logic of a "simple if" statement can be transformed into a ternary operator for concise conditional assignment. For example, the "simple if" statement: `if (test_condition) { x = value1; }` can be rewritten as `x = (test_condition) ? value1 : x;`. The benefit of using a ternary operator is that it reduces the amount of code, making it more compact and potentially easier to read for simple conditional assignments. This is particularly useful in inline assignments where clarity is not compromised .

The main trade-off between using "if" statements and conditional operators is between readability and conciseness. "If" statements provide clarity and are better for managing complex logic as they incorporate entire blocks of code based on conditions. Conversely, conditional operators are concise and best suited for simple operations, typically for assigning variables. However, overuse of conditional operators can reduce readability, especially when nested or handling multiple conditions, which is where "if" statements maintain logical clarity .

A programmer might choose "switch" statements over "if" statements for better readability and manageability when dealing with multiple conditions that are based on the same variable. "Switch" statements provide a more structured format for examining the different cases a variable might hold, making the code easier to follow and less error-prone as opposed to numerous "if...else if" chains. Moreover, a "switch" statement can be more efficient as it can translate to a jump table in machine code, whereas "if" statements lead to a series of comparisons .

Excessive use of "if...else" chains can lead to code bloat and reduced clarity, making it difficult to understand, maintain, and debug. Large chains can also introduce performance inefficiencies as each condition requires evaluation in sequence, potentially leading to slower response times if the chain is long. Solutions include refactoring the chains into switch statements when dealing with common single-variable cases, employing polymorphism to reduce conditional logic, and utilizing lookup tables or design patterns like strategy or state for complex decision-making scenarios .

The "else if ladder" ensures efficient program execution by evaluating conditions sequentially, where execution stops as soon as a true condition is encountered, thus ignoring the remaining conditions. This not only saves computational resources but also leads to faster decision-making compared to evaluating every condition as separate "if" statements, especially when later conditions are commonly false or less frequent. This structure is ideal for scenarios where multiple conditions are mutually exclusive regarding the same variable or expression .

Optimizing an "if" statement for performance involves considering the frequency and probability of condition occurrence. Place the most likely true conditions first, as the execution time depends on how quickly a true condition can be found and the unnecessary evaluations this allows skipping. For example, in a loop with multiple "if" conditions, structuring these conditions in order of decreasing probability of being true can reduce the average number of evaluations, thus improving performance. Replacing nested "if" statements with 'else if' can also streamline execution by eliminating unnecessary evaluations when a true condition is encountered early on .

The "else if" ladder is often clearer and more readable compared to nested "if...else" statements, especially when testing the same variable against many conditions. It avoids deeply nested code blocks that can become difficult to read and manage, maintaining a linear progression in evaluation. The "else if" ladder executes each condition sequentially until a true condition is found or all are exhausted, which contrasts with nested structure where each block may require additional, separate evaluations .

Nested "if...else" statements may offer performance advantages when condition evaluations are hierarchically dependent and a decision needs to be made only after another condition holds true. This setup allows the program to short-circuit evaluation: once a higher-level condition is found to be false, the entire nested block can be skipped. They can also be optimized in such a way that certain branches are more frequently taken, thereby reducing the average-case execution time for certain inputs .

Misuse of "nested if...else" statements can lead to deeply nested structures, often referred to as "pyramid code", making it difficult to read and understand. This complexity increases the chance of errors and bugs, as the intertwined logic complicates flow control understanding. Such code is hard to refactor or extend without introducing new errors. Code maintainability suffers because changes in one part of the nested chain may necessitate modifications throughout the nested structure, leading to potential ripple effects in functionality .

Using the "goto" statement often leads to poorly structured and difficult-to-maintain code, effectively creating what is known as "spaghetti code". This undermines readability and can cause unpredictable behavior in larger systems due to its unconditional jump in the code flow, skipping the natural progression of control structures like loops and conditionals. "Goto" also makes it harder to debug and reason about code since it bypasses structured control flow mechanisms that naturally manage scope and state transition .

You might also like