The statements which alter the flow of execution of a program are known as control statements. Discover the essentials of control statements in C programming. Learn how decision-making, looping, and branching structures help control the flow of a C program.
Table of Contents
ToggleThere are two types of control statements: Decision Making & Repeating construct
Decision-Making Statements
|
Loop Statements or Repeating construct
|
if statements | for loop |
if.. else statements | while loop |
if.. else if statements | do.. while loop |
Nested if.. else statement | |
switch statement |
Decision-Making Statements
i. if Statements
An if statement is used to control the flow of statement execution. The if statement first evaluates an expression, and then, if the value of the evaluated expression is true, it executes the statements within its block.
Example 1
Write a program to read a number from the user and test whether the number is negative [Show message “The number is negative ” if it is a negative number otherwise show nothing].
Nested if Statement
When one if statement is written within the body of another if statement, it is called a nested if statement. Several if statements shall be declared as nested if statements.
Syntax
Example
Write a program to read the percentage of marks obtained by a student in SLC and +2 levels, Display the message “Congratulation!! You have a first division in both SLC and +2” if both levels have a percentage greater than or equal to 60.
if…. else Statement
The if.. else statement is an extension of the simple if statement. It is used when there are only two possible actions one happens when a test condition is true, and the other when it is false.
Example
Write a program to read a number from the user and determine whether the number is even or odd.
Nested if.. else Statement
Similar to nested if statements if.. else statements shall also be written inside the body of another if.. else body called nested if.. else statement.
Example
Write a program to read three numbers from the user and determine the largest number among them.
if…. else if Statement
An if… else if statement is used when there are more than two possible actions depending upon the outcome of the test expression. When an action is taken, no others can be executed or taken. In such a situation, structure is used if…. else if… else if.. else.
Example
Write a program that reads the total marks of a student in seven subjects. Then calculate the percentage and determine the division. Use the following conditions:
- Percentage greater than or equal to 80- distinction,
- Percentage between 60 and 79- First Division
- Percentage between 45 and 59-second division
- Percentage between 32 and 44 – third division
- Percentage less than or equal to 31 -fail.
Loop or Iteration or Repeating Construct
Types of Loop
- for loop
- while loop
- do-while loop
i. for Loop
Example
Write a program to calculate the factorial of a number.
While Loop
Example
Write a program to calculate the factorial of a number using a while loop.
do-while Loop
Example
Write a program to read a number from a keyword until a zero or a negative number is keyed in. Finally, calculate the sum and average of the entered numbers.
break Statement
The break statement terminates the execution of the loop and the control is transferred to the statement immediately following the loop.
Example
What is the output of the following program?
continue Statement
The continue statement is used when we want to continue running the loop.
Example
Write a program that asks for a number n from the user and then displays only even numbers from 2 to n.
goto STATEMENT
The goto statement is used to after the normal sequence of program execution by unconditionally transferring control to some part of the program.
Example
Write a program to ask for two numbers. Display the message” Either number is negative ” if either number is negative; otherwise, display the message ” Both numbers are positive”.
switch STATEMENT
When there are a number of options available and one of them is to be selected on the basis of some criteria, a switch statement is used.
if
, else
, switch
), looping statements (for
, while
, do-while
), and branching statements (break
, continue
, return
). These constructs help the program make choices, repeat actions, or skip parts of the code based on conditions, making them key for dynamic and flexible programming.