What is Decision in C Programming?
In C Programming, decision-making is a core skill that programmers need to master to build effective programs. Decision-making is the process of evaluating different conditions and choosing the best course of action based on the results. With an if-else statement, decision-making is implemented in C. Decisions are based on conditions and are made using the if-else statement. If the condition is true, the code runs; on the other hand, if it is false, the code contained in the else statement is run.
What is Branching in C Programming?
In C Programming, branching is the technique that allows the flow of execution to change based on the result of a condition. Branching enables the program to run specific blocks of code depending on the outcome of a specific circumstance.
There are various types of branching in C Programming, including if-else, switch, and conditional operators. In a switch statement, the program evaluates a value and chooses the relevant option from the list of available cases. Conditional operators are shorthand if-else statements that allow you to write shorter code.
Decisions and Branching in C Programming
The decision-making process in C Programming involves the use of conditional statements to control the flow of program execution. Branching allows the program to execute different sets of code based on the outcome of certain conditions.
In C Programming, decision making, and branching are achieved through:
- if Statement
- if-else Statement
- switch Statement
- Nested if
- else-if Ladder
- break Statement
- continue Statement
1: if Statement
The simplest method of making decisions offered by C programming is the if statement. The if statement tests a given condition and executes the code in the body of the statement if the condition is true. Otherwise, the program ignores the code block associated with the if statement, and it continues with the subsequent code.
The syntax for if-statement is:
{
Block of statements;
}
Look at the code below:
int main ()
{
int num = 20;
if( num > 5 )
{
printf("num is greater than 5\n" );
}
printf("value of num is : %d\n", num);
return 0;
}
The above code defines a variable “num” with a value of 20 and uses an if statement to check if it is greater than 5. If it is, the program prints “num is greater than 5“. Finally, it prints the value of “num“.
Output
2: if-else Statement
The if-else statement is a modification of the if statement that allows the execution of different code blocks based on whether the condition is true or false.
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}
As a result, the first code block will be executed if the condition is true, and the second code block will be executed if the condition is false.
Consider the following code as an example:
int main ()
{
int num = 10;
if(num > 5)
{
printf("num is greater than 5\n");
} else {
printf("num is less than 10");
}
return 0;
}
The above program creates the variable num and gives it the value 10. Then, using an if statement, it determines if the “num” is larger than 5. The “num is larger than 5” is printed if the “num” exceeds 5. It displays “num is less than 10” if num is not greater than 5. The program then returns 0, signifying that it has run successfully.
Output
3: switch Statement
Another important tool for decision-making in C Programming is the switch statement. The switch statement checks for specific conditions just like if-else statements do, but it can check multiple possibilities for that condition. This is helpful when we’re dealing with lots of different outcomes.
The syntax for using the switch statement in C programming is:
case constant1:
// code to execute if expression equals constant1
break;
case constant2:
// code to execute if expression equals constant2
break;
...
default:
// code to execute if none of the cases match
break;
}
Expression is the variable being assessed in this case, and the case statements include the values being compared to it.
Consider the following code as an example:
int main() {
int num = 2;
switch (num) {
case 1:
printf("number is 1");
break;
case 2:
printf("number is 2");
break;
case 3:
printf("number is 3");
break;
default:
printf("number other than 1, 2 and 3");
break;
}
return 0;
}
The above program shows how to use the switch statement to examine the value of the variable “num” and run the relevant code block. In this case, since “num” is initialized to 2, the output will be “number is 2“.
Output
4: Nested if
Nested if statements are branching statements that are embedded within other nested if statements. It allows for more complex branching logic by checking multiple conditions within other conditional statements. The inner if statements are only executed if the outer if statements evaluate to true.
The basic syntax for nested if statements are given below:
if(expression) {
Block of statements;
} else {
Block of statements;
}
} else {
Block of statements;
}
Consider the following code as an example:
int main() {
int num1 = 1;
int num2 = 15;
int num3 = 7;
if(num1 > num2) {
if(num1 > num3) {
printf("num1=1 is the largest number\n");
}
else {
printf("num3=7 is the largest number\n");
}
}
else {
if(num2 > num3) {
printf("num2=15 is the largest number\n");
}
else {
printf("num3=7 is the largest number\n");
}
}
return 0;
}
The above program compares three integers, “num1“, “num2“, and “num3“, and uses nested if statements to determine which one is the largest number. It first compares “num1” and “num2“, then compares the larger of those two with “num3“. The output will indicate which variable has the largest value.
Output
5: else-if Ladder
We may easily resolve a complex problem when numerous criteria are present in sequential order by employing a ladder-if or else-if expression.
Below is the syntax for the else-if ladder statement:
{
Block of statements;
}
else if (condition2)
{
Block of statements;
}
else if (condition3)
{
Block of statements;
}
else
{
default statement
}
Consider the following code as an example:
int main() {
int marks =80;
if(marks >= 90 && marks = 80 && marks = 70 && marks = 60 && marks = 50 && marks < 60) {
printf("Grade: D");
}
else {
printf("Grade: Fail");
}
return 0;
}
The program above employs if-else logic to determine a grade according to the current value of the variable “marks”. Depending on the value of “marks”, the program will output a corresponding grade ranging from A+ to Fail.
Output
6: break Statement
The break statement is an important control flow statement in C Programming that allows programmers to control the behavior of loops and switch statements. The break statement has two applications in C programming:
- When a loop reaches a break statement, it is terminated immediately, and program control is handed to the statement that follows the loop.
- It can be used to end a case by using it to the switch statement.
The syntax for the break statement:
Look at the example code:
int main() {
int num = 12;
while (num 15) {
break;
}
}
return 0;
}
This piece of C code declares a while loop that executes as long as the integer variable “num” is less than 22 and initializes it to 12. In the loop, “num” is increased by 1 and its value is reported to the console using printf. The loop is then terminated with a break statement if “num” is larger than 15 as determined by an if statement. This code effectively ends the loop after printing the values of “num” between 12 and 15 (inclusive). The program ends by returning 0, which shows that it ran correctly.
Output
7: continue Statement
In C programming, the continue statement is similar to the break statement. Instead of imposing termination, it forces the next iteration of the loop and skips any code in between. The for loop’s conditional test and increment sections are executed by the continue expression. The while and do-while loops’ conditional tests are passed by the program control as a result of the continue statement.
Syntax of continue statements is:
Look at this example.
int main() {
int num = 12;
while (num 15) {
continue;
}
}
return 0;
}
The while loop in the above program is used to print the value of the variable “num” if it is less than 22. If “num” exceeds 15 during the loop, the continue statement is executed, and the present iteration of the loop is skipped. In this case, the loop will execute five times, printing the value of “num” each time, until “num” reaches 16 and the loop skips the iteration where “num” is 16, then continues with the remaining iterations until the loop is terminated.
Output
Conclusion
Decision-making and branching are critical components of C language that allow for creating complex, interactive applications that handle various real-world conditions. The conditional statements, if-else and switch, are the primary cases used in creating decision-based algorithms. Although branching may pose a challenge in the organization of codes, with proper planning and execution, programmers can create efficient and error-free programs that meet specific requirements.