Control Statements in C [ Full Information With Examples ]

Control Statements in C
Control Statements in C Language

What is Control Statements in C

Such statements by which we determine the flow of a program are called Control Statements or Decision control statements.

In any programming language, various tasks need to be done depending on the condition and to do such tasks we use Control Statements or Decision-making statements.

For example, consider an online website, when you enter the wrong ID or password it displays an error page and when you enter the correct credential it displays a welcome page.

So there should be a logic that checks the condition (id and password) and if the condition is correct then it displays the welcome page otherwise it displays an error page.

When we create a program in C language and execute that program, the statements inside the program are executed in the same order in which they are written.

But sometimes we want a statement in the program to be executed on a particular condition or else that statement should not be executed.

Also, under certain circumstances we want to execute any statements, again and again, we use Control Statements or Condition statements for all of them.

Control Statements help us control the flow of the program. Through Control Statements we determine how the program will move from one statement to another.

In C Programming language we have different types of decision control statements.

Types of Control Statement in C

In C language we have the following decision control statements

If statements

If statement is one of the powerful conditional statements in C language. The If statement is always used with a condition.

The condition is checked before executing any statement inside the body of if statement.

Syntax

if (condition)
{
  Statements;
}

if statements are of 3 types:

  • If..else
  • Nested if esle
  • Else if ladder

if else Statement

In this decision control statement, we have two blocks of statements. One block is inside if and the other block is inside else.

If, If condition is true, then the statement inside the if block is executed, otherwise the statement inside the else block is executed.

Syntax

if (Condition)
{
    True block of statements
}
Else
{
    False block of statements
}

else if ladder

else if ladder control statement is also like if-else statement but there is a slight difference in it that inside else block one or more if-else statement conditions are present.

Syntax

if (condition1)
{
   // These statements would execute if the condition1 is true
}
else if (condition2)
{
   // These statements would execute if the condition2 is true
}
else if (condition3)
{
   // These statements would execute if the condition3 is true
}
.
.
else
{
   // These statements would execute if all the conditions return false.
}

Nested if else

When inside the if statement or else statement one or more if else statement, then it is called Nested

Syntax

if (condition) {
    // Nested if else inside the body of "if"
    if (condition2) {
       // Statements inside the body of nested "if"
    }
    else {
       // Statements inside the body of nested "else"
    }
}
             else {
    // Statements inside the body of "else"
}

Loop Statements

When we want to repeatedly run a particular statement until a particular condition is met, then in that case we use Loop Statements.

There are three types of loop statements in C language:

While loop

In the while loop, the first condition is checked and if the condition is true only then the statement inside the while loop is run until the statement inside the while loop runs as long as the condition of the while loop is true. as soon as the condition is False, the control is moved out of the while loop, and another statement is executed.

Syntax

while (Condition)
{
Statements;
}

Do while loop

The do-while loop is also similar to the while loop, but there is a difference in that the while loop first checks the condition, and the statements inside the while loop are run only when the condition is true whereas, in the do-while loop the condition is true Or False, the statement inside it runs once.

Syntax

Do
{
// statements inside the loop
}
While (condition);

For loop

In For Loop, we write both the initialization and control condition of the variable together inside the parentheses “()”. If the condition of For Loop is true, then the statement inside it is run, or else the statement is not run.

Syntax

for (initialization statement; condition)
{
// statements inside the loop
}

Switch statement

When we have a lot of conditions and we have to run a statement matching that condition, then we use the switch case statement. with the help of Switch Case Statement We can do all the work done by if-else

Syntax

Switch (expression)
{
Case label1:
           Statement (S);
           Break;
Case label2:
           Statement (S);
           Break;
Case label3;
           Statement (s);
           Break;
….
Case labelN:
          Statement (s);
           Break;
Default:
        Statement (s);
}

Conditional Operator Statement

The Conditional Operator Statement behaves like an if-else statement. With the help of conditional statements, we can do all the work done by if-else.

The conditional statement works on three operands, hence it is also called ternary operator.

The conditional statement is represented by two symbols, ie, represented by ‘?’ and ‘:’. (?:)

Syntax

Expression1? expression2: expression3;

Look at this syntax, if the condition of Expression1 is correct, then Expression2 runs, otherwise the Expression3 runs.

Goto Statement

The goto statement is also known as the jump control statement, it is used to transfer the control of the program from one block to another. The goto keyword is used when declaring the goto statement.

Syntax

goto labelname;
Labelname;

In the syntax above, goto is a keyword used to transfer control to labelname. labelname is a variable name. goto will transfer the control of the program to labelname and the statements after labelname will be executed.

Read More -:

Conclusion

Friends, I hope you have found the answer of your question and you will not have to search about the control statements in C language.

However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.

If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.  

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the  control statements in C Language. 

To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment