Very happy to see you again, welcome! back to the "technical_spc". In the my last two articles of "C" programming we have completed unit 1 i.e. Introduction to "c". I hope all of you have understood the unit 1 very nicely.
Now in this article let's start with unit 2 i.e. Decision control structure.
Today the topics that we will cover are :-
Let's move forward...
Decision making in"c".
(a). Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program.
(b). Along with a statement or statements to be executed if the condition is determined to be true.
(c). Optionally other statements to be executed if the condition is determined to be false.
(d). A decision control instruction can be implemented :
1. The if statement.
2. The if.....else statement.
3. The if.....else if..... statement.
4. Nested if statements.
5. Switch statement.
Algorithm & Flowchart.
(a). Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired outputs.
(b). Flowchart is a diagrammatic representation of sequence of logical steps of a program.
(c). Flowcharts use simple geometric shapes to depict processes and arrows to show relationships and process/data flow.
if statement.
- Program :-
- Output :-
- Explanation :-
if.....else statement.
if(condition)
{
statement will execute if the condition is true;
}
else
{
statement will execute if the condition is false;
}
(c). Flowchart :-
{
statement will execute if the condition 1 is true;
}
else if(condition 2)
{
statement will execute if the condition 2 is true;
}
else if(condition 3)
{
statement will execute if the condition 3 is true;
}
else
{
statement will execute if the condition is false;
}
(c). Flowchart :-
(d). Example :-
- Program :-
- Output :-
- Explanation :-
1. else and else..if are optional statements, a program having only “if” statement would run fine.
2. else and else..if cannot be used without the “if”.
3. There can be any number of else..if statement in a if else..if block.
4. If none of the conditions are met then the statements in else block gets executed.
5. Just like relational operators, we can also use logical operators such as AND (
&&), OR(||) and NOT(!).Nested if statements.
(a). It is always legal in "c" programming to nest if-else statements.
(b). Which means you can use one if or else if statement inside another if or else if statements.
(c). Syntax :-
if(condition 1)
{
execute this statement if condition 1 is true;
if(condition 2)
{
execute this statement if condition 2 is true;
}
else
{
execute if condition 2 is false;
}
else
{
execute if condition 1 is false;
}
}
(d). Example :-
- Program :-
- Output :-
- Explanation :-
Within this example, If the age of a person is less than 18, he is not eligible to work. If the age is greater than or equal to 18, then the first condition fails, it will check the else statement. In the Else statement, there is another if condition called Nested If in C to check further.
- In this example, the Nested IF Statement checks the person’s age greater than or equal to 18 and less than or equal to 60. When the condition is TRUE, then he can apply for the job.
- If the condition is FALSE, then the statement – he is too old to work as per the government.
If you have any questions you may ask in comment below. If you enjoyed this article, share it with your friends and colleagues!


Good yaar, beginner friendly ☺
ReplyDeleteThank you!!!
Delete