Branching statements in C
If statement
The if statement can cause other statements to execute only under certain conditions. The figure below is the famous figure associated with all books and it is the best way of understanding if statement.
Without a conditional statement such as the if statement, programs would run almost the exact same way every time. If statements allow the flow of the program to be changed, and so they allow algorithms and more interesting code.
Below this is the perfect example that shows the use of if statement in c with basic syntax and conditions statement.
Example of If in C
#include<stdio.h>
#include<conio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
Output on Screen:
Variable x is less than y
Furthermore, examples for a better understanding of if the statement is as below in the table
Note: you should not miss brackets of if statement and braces of if statement at the start and endpoint.
The if /else statement in C
If else statements in C is also used to control the program flow based on some condition, only the difference is: it’s used to execute some statement code block if the expression is evaluated to true, otherwise executes else statement code block.
The example of if/else statement in the foam on the table is as below:
This example shows us that if the condition in if statement is true then it will be executed and else the statement will be neglected and will not be checked and if the statement is false then the compiler goes down and check the else statement and print the else statement answer.
The if-else Syntax in C
if(test_expression)
{
//execute your code
}
else
{
//execute your code
}
if-else Sample example
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y;
printf("enter the value of x:");
scanf("%d", &x);
printf("enter the value of y:");
scanf("%d", &y);
if (x>y)
{
printf("x is greater than y\n");
}
if (x<y)
{
printf("x is less than y\n");
}
if (x==y)
{
printf("x is equal to y\n");
}
printf("End of Program");
return 0;
}
Output on Screen:
enter the value of x:20
enter the value of y:20
x is equal to y
End of Program
Nested if/else if Statements
The if-else if the statement is a chain of if statements. They perform their tests, one after the other until one of them is found to be true.
Sample program of if/else if statement-
This example shows us about the grading system in various colleges in India.
First, if statement will be checked and then if it is true then print the if statement and if it becomes false then goes on else/if statement if will be checked and so on at the end, it will go to else statement which is far more easy to code instead of using if/else chain.
Nested if statement in C
To test more than one condition, an if statement can be nested inside another if statement.
Nested if it can be used in menu-driven programs in which we have to check more than one condition at the same time. Like we saw the point of sale system in which there is a menu for the user.
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
if(num<35)
{
if(num==1)
{
printf("The value is as below :%d\n",num);
}
else
{
printf("The value is greater than 1 then below see");
}
}
else
{
printf("The value is greater than 10 so lets check");
}
return 0;
}
Output on Screen:
You passed.
Perfect!
Another example of this approach is as below:
Switch statement in C
The switch statement uses the value of a variable or expression to determine where the program will branch to.
The switch case statement is used when we have multiple conditions as we deal in point of sale system where the user comes and want to purchase things of multiple-choice, and we need to perform different action based on the condition. When we have multiple conditions and we need to execute a block of statements when a particular condition is satisfied.
In such a case either we can use lengthy if or else or if/else or maybe nested if statements or switch case. The problem with lengthy if or else or if/else or maybe nested if statements are that it becomes complex when we have several conditions.
The switch case is a short and efficient method of handling such scenarios with a smaller number of lines of code and with more readability.
Important tip: if the switch condition becomes true then it goes down and checks the case of switch cases one by one like else if statements.
In the above, there is a basic syntax of a switch statement which shows us the first expression statement in which we pass a single value or variable which considers as a primary condition and other cases below will be checked on the basis of this condition.
Example of Switch case in C
/* C program to implement simple functions of a calculator*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
float result;
char ch; //to store operator choice
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose operation to perform (+,-,*,/,%): ");
scanf(" %c",&ch);
result=0;
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
return 0;
}
Output on Screen:
First run:
Enter first number: 10
Enter second number: 20
Choose operation to perform (+,-,*,/,%): +
Result: 10 + 20 = 30.000000
Second run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): /
Result: 10 / 3 = 3.333333
Third run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): >
Invalid operation.
Result: 10 > 3 = 0.000000
Enumerated Data Types in C
An enumerated data type in C is a programmer-defined data type whose legal values are a set of named integer constants.
#include<stdio.h>
#include<conio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Output on Screen:
Day name is 4