Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.
The Ultimate Guide to Understand Everything on Control Statements in C

Every statement in a computer is executed based on pre-defined rules. The control flow is also based on logic. At times, you find a necessity to execute a few customized logics. Custom statements can be executed using control statements. 

Here, the control enters the statements block and gets executed if the logic is satisfied. Hence, they are called control statements. They are often used to determine the order in which statements must be executed. This tutorial will help you learn more fundamentals of control statements in C.

Learn from the Best in the Industry!

Caltech PGP Full Stack DevelopmentExplore Program
Learn from the Best in the Industry!

What Are Control Statements in C?

In simple words, Control statements in C help the computer execute a certain logical statement and decide whether to enable the control of the flow through a certain set of statements or not. Also, it is used to direct the execution of statements under certain conditions.

Types of Control Statements in C

  • Decision-making control statements.
  • Conditional statements 
  • Goto statements in C
  • Loop control statements in C

Decision-Making Control Statements Are:

  • Simple if statement
  • If-else statements 
  • Nested if-else statements
  • else-if ladder

Now, you will go through them in detail.

Get All Your Questions Answered Here!

Caltech PGP Full Stack DevelopmentExplore Program
Get All Your Questions Answered Here!

  • Simple if Statement

Simple if statements are carried out to perform some operation when the condition is only true. If the condition of the if statement is true then the statements under the if block is executed else the control is transferred to the statements outside the if block.  

Syntax of the if statement is as given below:

Control_Statements_In_C_1

Flow Chart:

Control_Statements_In_C_2

Example: 

Control_Statements_In_C_3

Output:

Control_Statements_In_C_4

  • Basics to Advanced - Learn It All!

    Caltech PGP Full Stack DevelopmentExplore Program
    Basics to Advanced - Learn It All!

    If-else Statement 

In some situations, you may have to execute statements based on true or false under certain conditions, therefore; you use if-else statements. If the condition is true, then if block will be executed otherwise the else block is executed.

Syntax of the if-else statement is as given below:

Control_Statements_In_C_5

Flow Chart:

Control_Statements_In_C_6

Example:

Control_Statements_In_C_7

Output:

Control_Statements_In_C_8

  • Create and Showcase Your Portfolio from Scratch!

    Caltech PGP Full Stack DevelopmentExplore Program
    Create and Showcase Your Portfolio from Scratch!

    Nested if-else Statements

The nested if-else statements consist of another if or else. Therefore; if the condition of “if” is true (i.e., an outer if) then outer if’s if block is executed which contains another if (that is inner if) and if the condition of if block is true, statements under if block will be executed else the statements of inner if’s “else” block will be executed.

If the outer “if” condition is not true then the outer if’s “else” block is executed which consists of another if. The outer else’s inner if the condition is true then the statement under outer else’s inner if is executed else the outer else’s else block is executed.  

Syntax of the nested if-else statement is as given below:

Control_Statements_In_C_9Control_Statements_In_C_9_2

Flow Chart:

Control_Statements_In_C_11

Example:

Control_Statements_In_C_12

Output:

Control_Statements_In_C_13

  • Become a Skilled Web Developer in Just 9 Months!

    Caltech PGP Full Stack DevelopmentExplore Program
    Become a Skilled Web Developer in Just 9 Months!

    Else-if Ladder Statements

The else-if ladder statements contain multiple else-if, when either of the condition is true the statements under that particular “if” will be executed otherwise the statements under the else block will be executed.

Suppose the “if” condition is true, statements under “if” will be executed else the other “if” condition is tested, and if that condition is true statements under that particular “if” will be executed. This process will repeat as long as the else-if’s are present in the program.

Syntax of the else-if ladder statement is as given below:

Control_Statements_In_C_14.

Flow Chart:

Control_Statements_In_C_15.

Example:

Control_Statements_In_C_16.

Output:

Control_Statements_In_C_17.

So far, you have looked at the decision-making control statements in C. Now, go ahead and take the next step and learn conditional statements in C.

Learn from the Best in the Industry!

Caltech PGP Full Stack DevelopmentExplore Program
Learn from the Best in the Industry!

Conditional Control Statements in C

As per the value of the switch expression, the switch statement will allow multi-way branching. 

Depending on the expression, the control is transferred to that particular case label and executed the statements under it. If none of the cases are matched with the switch expression, then the default statement is executed.

The syntax of the switch statement is as given below:

Control_Statements_In_C_18

Flow Chart:

Control_Statements_In_C_19

Example:

Control_Statements_In_C_20.

Output:

Control_Statements_In_C_21

After the conditional statement, you have the goto statement in C.

<

Get All Your Questions Answered Here!

Caltech PGP Full Stack DevelopmentExplore Program
Get All Your Questions Answered Here!

Goto Statements in C

The goto statements are used to transfer the flow of control in a program, goto statement is also known as a jump control statement because it is used to jump to the specified part of the program. The label in goto statement is a name used to direct the branch to a specified point in the program. 

Syntax of the goto statement is as given below:

Control_Statements_In_C_22

After goto statements in C, you are now heading towards the loop control statements in C.

Loop Control Statements in C

  • While Loop

A while loop is also known as an entry loop because in a while loop the condition is tested first then the statements underbody of the while loop will be executed.

If the while loop condition is false for the first time itself then the statements under the while loop will not be executed even once. 

The syntax of the while loop is as given below:

Control_Statements_In_C_23

Flow Chart:

Control_Statements_In_C_24

Example:

Control_Statements_In_C_25

Output:

Control_Statements_In_C_26

  • Basics to Advanced - Learn It All!

    Caltech PGP Full Stack DevelopmentExplore Program
    Basics to Advanced - Learn It All!

    do-while Loop

The do-while is also known as an exit loop because in the do-while loop, the statements will be executed first and then the condition is checked. 

If the condition of the while loop is true then the body of the loop will be executed again and again until the condition is false. Once the condition is false, the control will transfer outside the do-while loop and execute statements followed soon after the do-while loop.

The syntax of the do-while loop is as given below:

Control_Statements_In_C_27

Flow Chart:

Control_Statements_In_C_28

Example:

Control_Statements_In_C_29

Output:

Control_Statements_In_C_30

Up next, you have the for loop statement in C.

  • Create and Showcase Your Portfolio from Scratch!

    Caltech PGP Full Stack DevelopmentExplore Program
    Create and Showcase Your Portfolio from Scratch!

    For Loop

The for loop is also known as a pre-test loop. From the following syntax, expression1 is an initialization, expression2 is the conditional expression and expression3 is an updation. The variables can be initialized in for the statement itself.

The syntax of the do-while loop is as given below:

Control_Statements_In_C_31

In the for loop, expression1 is used to initialize the variable, expression2 is evaluated and if the condition is true, then the body of for loop will be executed and then the statements under expression3 will be executed. This process is repeated as long as the for loop condition is true, once the condition is false control will return to the statements following the for loop and execute those statements.

Flow Chart:

Control_Statements_In_C_32.

Example:

Control_Statements_In_C_33

Output:

Control_Statements_In_C_34

Become a Skilled Web Developer in Just 9 Months!

Caltech PGP Full Stack DevelopmentExplore Program
Become a Skilled Web Developer in Just 9 Months!

Next Steps

"Data Structures in C" can be your next topic. So far, you have learned the control statements in C programming Language. The next fundamentals will be the data structures and the varieties in data structures used for different purposes.

If you are interested in building a career in software development, then feel free to explore Simplilearn's Courses that will give you the work-ready software development training you need to succeed today. Are you perhaps looking for a more comprehensive training program in the most in-demand software development skills, tools, and languages today? If yes, our Post Graduate Program in Full Stack Development should be just the right thing for your career. Explore the course and enroll soon.

If you have any questions regarding the "control statements in C” tutorial, please let us know in the comment section below. Our experts will get back to you ASAP.

Create and Showcase Your Portfolio from Scratch!

Caltech PGP Full Stack DevelopmentExplore Program
Create and Showcase Your Portfolio from Scratch!

About the Author

Hoor Sania SHoor Sania S

Hoor is a Bangalore-based Research Analyst. She has a knack for learning computer languages and technologies. In her free time, she enjoys dancing and singing.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.