Hey learner, Welcome to my blog, in this article I will discuss Statement Flow Control in C Programming. So lets start!
Follow this link to read fundamentals of statements and about C programming:
Topics Covered
Statement Flow Control in C Programming
A statement is a combination of letters and symbols in C programming which ends with a semicolon. The sequence or order of statements is known as statement flow control in C programming. It is very essential for every programming language to use proper order of sequence of statements to be executed.
In the next section of Statement Flow Control in C Programming, I will talk about the constructs provided by C programing.
Constructs in C Programming
As every programming provides three main constructs so as C programming too. They are:
- Sequence
- Selection
- Iteration
Now understands these constructs for Statement Flow Control in C Programming.
Sequence
The sequence is a normal and simple construct of C programming. It is just a sequence of statements written in the program. Consider this example:
void main()
{
printf("Hello, this is sequence construct\n")
printf("The sequence of normal statement is considered as a sequence construct.")
}
Selection
Select refers to the statements where a programmer has to select a condition. It is based on different conditions. The example are:
- Simple if
- If-else
- If-else ladder
- Switch
Now lets undersand these constructs in detail with example for Statement Flow Control in C Programming.
Simple if
It checks only one condition and executes the statements if it evaluates to true, otherwise does nothing. The syntax of C programming is as following:
if (test-condition)
{
Statements;
}
Example:
if (n==0)
{
printf(“You have entered 0”)
}
Watch this video for more understanding:
If-else statement in C programming
The if-else statement is another control flow statement in C programming. If the statement evaluated to true it will execute the first part, if the condition evaluates to false it will execute the else part. Look at the syntax:
if (condition)
{
statements;
}
else
{
statements;
}
As you have observed many real-life situations where there are two possibilities arise. Either this or that! For example, when you are trying to do login with your e-mail account or any social media, you are entering your username and password. The system checks the username and password entered by use on-screen with the stored username and password. If both matches it will render your profile/inbox page.
Just have look at the example:
#include<stdio.h>
#include<conio.h>
void main()
{ un=8912;
pwd=123;
clrscr();
printf("Enter your username:");
scanf("%d",&un);
printf("Enter your password:");
scanf("%d",&pwd);
if(un==8912 && pwd==123)
{
printf("Welcome %d",un);
}
else
{
print("Incorrect username and/or password")
}
getch();
}
Similarly, another condition is there when you are going to check whether the entered number is odd or even! Observe this code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter your number:");
scanf("%d",&n);
if(n%2!=0)
{
printf("%d is odd number");
}
else
{
printf("%d is even number");
}
getch();
}
Watch this video for more understanding:
Iteration
Iteration or looping is also a very important part of C programming. The repetition of statements according to the condition is known as iteration. It can be considered as a cycle where steps are executed until specific conditions will be satisfied.
There are three types of iterations used in C programming:
- While
- Do…While
- For