EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Control Statements in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Control Statements in C

Control Statement in C

Introduction to Control Statements in C

In C, the control flows from one instruction to the next instruction until now in all programs. This control flow from one command to the next is called sequential control flow. Nonetheless, in most C programs the programmer may want to skip instructions or repeat a set of instructions repeatedly when writing logic. This can be referred to as sequential control flow. The declarations in C let programmers make such decisions which are called decision-making or control declarations. Below we will discuss the types of Control Statements in C.

Types of Control Statements in C

C also supports an unconditional set of branching statements that transfer the control to another location in the program. Selection declarations in C.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  1. If statements
  2. Switch Statement
  3. Conditional Operator Statement
  4. Goto Statement
  5. Loop Statements

1. If Statements

If statement enables the programmer to choose a set of instructions, based on a condition. When the condition is evaluated to true, a set of instructions will be executed and a different set of instructions will be executed when the condition is evaluated to false. We have 4 types of if Statement which are:
1. If..else
2. Nested if
3. Else if ladder
4. Simple if or null else
5. Null else or Simple else

  • If…else Statement

In this statement, there are two types of statements execute. First, if the condition is true first statement will execute if the condition is false second condition will be executed.

Syntax:

If(condition)
{
Statement(s);
}
else
{
Statement(s)
}
Statement

  • Nested if

If the condition is evaluated to true in the first if statement, then the condition in the second if statement is evaluated and so on.

Syntax:

If(condition)
{
If(condition)
{
Statement(s);
}
Else
{
Statement(s)
}
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)
  • else if Ladder

The corresponding array of instructions is executed when the first condition is correct. If the condition is incorrect, the next condition will be verified. If all the specifications fail, the default block statements will be executed. The remainder of the ladder can be shown as shown below.

Syntax:

If(condition)
{
Statement(s);
}
Else if(condition)
{
Statement(s);
}
else if(condition)
{
Statement(s)
}
…
Else
{
Statement(s)
}
Statement(s);

  • Null else or Simple else

If the programmer can execute or skip a set of instructions based on the condition value. The simple one-way statement is selected. A set of statements is carried out if the condition is true. If the condition is false, the control will proceed with the following declaration after the if declaration. Simple else statement:

Syntax:

If(condition)
{
Statement(s);
}
Statement(s);

2. Switch Statement

C offers a selection statement in several ways as if the program becomes less readable when the number of conditions increases. C has a multi-way selection statement called the switch statement that is easy to understand to resolve this problem. The switch declaration is easy to understand if more than 3 alternatives exist. The command switches between the blocks based on the expression value. Each block will have a corresponding value.

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);
Break;
}

Using the case keyword every block is shown and the block label follows the case keyword. The default block and the break statement are optional in a switch statement.

3. Conditional Operator Statement

C language provides an unusual operator, which is represented as a conditional operator.

Syntax:

(condition)? expr1: expr2

Expr1 is executed when the condition is valid. Then Expr2 will be executed if the statement is incorrect.

4. goto Statement

goto statement is known for jumping control statements. It is used to transfer the control of the program from one block to another block. goto keyword is used to declare the goto statement.

Syntax:

goto labelname;
labelname;

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

5. Loop Statements

The programmer may want to repeat several instructions when writing C programs until some requirements are met. To that end, C makes looping declarations for decision-making. We have three types of loops,

  1. For Loop
  2. While Loop
  3. Do While Loop
For Loop

In the For loop, the initialization statement is executed only one time. After that, the condition is checked and if the result of condition is true it will execute the loop. If it is false, then for loop is terminated. However, the result of condition evaluation is true, statements inside the body of for loop gets executed, and the expression is updated. After that, the condition is checked again. This process goes on until the result of the condition becomes false. When the condition is false, the loop terminates.

Syntax:

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

While Loop

In C, the while loop is a guided entry loop. The body of the while loops is only performed if the condition is valid. The loop structure is not executed if the condition scores to incorrect.
The while loops are usually used when several instructions have to be repeated for an indefinite time.

Syntax:

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

Do While Loop

Unlike while loop, the body of the do is the difference between while and … while loop is guaranteed to be done once at a time.

Syntax:

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

Conclusion

In this article, we have seen what are the various control statements in C along with their syntax and examples.

Recommended Article

This is a guide to Control Statements in C. Here we discuss the different types of Control Statements in C like If, Switch, Conditional Operator, goto and Loop along with syntax. You can also go through our other suggested articles to learn more –

  1. Continue statement in C#
  2. Break Statement in Python
  3. Control Statement in C++
  4. Control Statement in PHP

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More