C

Decision Control Statements In C Programming – The Most Detailed

In any programming language, different tasks must be performed based on the condition. Consider an online website; if you enter the wrong id or password, it displays an error page; if you enter the correct credentials, it displays a welcome page. So there must be logic in place that checks the condition (id and password) and performs a task (displaying welcome page) if the condition returns true, otherwise it performs a different task (displaying error page). The decision control statements in C programming are summarized below.

C Decision Control Statements

Here are the links to the tutorials:
If statement: The statements within the if body are only executed when the condition defined by the if statement is true. If the condition is false, the compiler skips the statement enclosed in the body of the if statement. In a C program, we can have an unlimited number of if statements.
If-then statement: We have two blocks of statements in this decision control statement. If the condition is true, the if block is executed; otherwise, the statements inside the else block are executed. Otherwise, there is no such thing as else. I also covered else-if statements in this tutorial.
Statement of switch-cases: This is extremely useful when evaluating multiple conditions. The switch block defines an expression (or condition), and case has a set of statements that are executed based on the result of the expression. A switch can have an unlimited number of cases, but there should only be one default handler.

Command Decision Control

1.If condition

It is one of the most basic types of decision control statements and is commonly used in decision making.

Write a program to print the highest number.

#include <stdio.h>
void main()
{
int n;
printf(“Enter a number:”);
scanf(“%d”, &n);
printf (“The entered number %d”, n);
if (n>100)
printf (“You entered a higher number”);
}

The above program accepts a number from the user as input and returns the result. (n>100) is given as an expression. If the user enters a number greater than 100, only the message will be printed; otherwise, the statement will be skipped.

2. If-then clause

There are times when we want to write that if the expression is true, action should be taken, and if the expression is false, no action should be taken. We can also include a conditional action for both conditions.
In such cases, the if-else statement is used.

#include <stdio.h>
void main()
{
int yr;
clrscr();
printf(“Enter a year:”);
scanf(“%d”, &yr);
if ((yr%4 == 0) && ((yr%100 !=0) || (yr%400==0)))
printf(“It’s a leap year…!!!!”);
else
printf (“It’s not a leap year….!!!”);
}

Output:
Enter a year: 1997
It’s not a leap year…!!!

3.Case of a switch

This case statement is a multi-way decision statement that is a simplified version of the if-else block that only evaluates one variable.
Syntax:

#include <stdio.h>
void main( )
{
char ch;
printf(“Enter any character:”);
scanf(“%c”, &ch);
switch(ch)
{
case ‘A’:
case ‘a’:
case ‘E’:
case ‘e’:
case ‘I’:
case ‘i’:
case ‘O’:
case ‘o’:
case ‘U’:
case ‘u’:
printf(“%c is a vowel”,ch);
break;
default:
printf(“%c is not a vowel”, ch);
}
}

Output:
Enter any character: c
c is not a vowel.

4. In C, the if statement is used.

The statements available within an if block will only be executed if the condition defined by this statement is true. If the available condition is false, the compiler will skip the available statement contained within the if statement’s body. In a program (written in C), we can have as many if statements as we want.
Syntax:

The Syntax available for the if statement will be:

if (condition x)

{

// A block of statements for the C program

// The execution of these statements will only happen when this condition turns to be true

}

Example:

Let us look at an example of a program that will ultimately print the highest number.

#include <stdio.h>

void main()

{

int x;

printf(“We enter any number:”);

scanf(“%d”, &x);

printf (“The number entered here is %d”, x);

if (x>100)

printf (“Hi. You have entered a comparatively higher number.”);

}

There will be an execution of the statements available inside the body of the if block whenever the given condition returns to be true. In case this condition returns to be false, then the program will skip the statements that we have inside the if block. The following syntax will be available for the if statement:
When the given condition returns to true, the statements available within the body of the if block will be executed. If this condition returns false, the program will skip the statements contained within the if block.
This statement is one of the most basic types of decision control statements. In the C language, we frequently use it in simple decision-making cases.
Let’s look at an example of a program that will eventually print the highest number.
In this program, the compiler will accept a number entered by the user as input and generate an output based on it. We’ve also included the expression n>100 here. It means that if the user enters a number greater than 100, we will only get the message as an output. Otherwise, this statement will be skipped for good.

Thus,

Case #1:

If x = 300,

The output generated here will be:

Hi. You have entered a comparatively higher number.

Case #2:

If x = 50,

The compiler will ignore the statement “Hello. “You entered a relatively higher number.”

FAQs

What is the distinction between an if statement and an if-else statement?
One of the most basic types of decision control statements is the if statement. In the C language, we frequently use it in simple decision-making cases.
Statements; if (condition x) Syntax;
The description is as follows: In the case of such a statement, when the available condition is true, a specific block of code is executed.
if…else
Several times, a user specifies that if a condition/expression is true, the compiler must take action. If that statement is false, the compiler will take another action. As a result, we can include an action for either of the conditions, or just a single statement for the true condition and no action for the others. The if-else statement is used here.
If (condition x) Statement x; Statement y; otherwise Statement a; Statement b;
The description is as follows: In the case of such a statement, when the available condition is true, the execution of a specific group of statements occurs. If this available condition is found to be false, the statement specified in the else part is executed.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button