1 of 23

Control Structures

UNIT II

2 of 23

Control Structures

  • There are four types of control structures /statements
  • 1. Sequence statements
  • 2. Selection also called as decision making statements
  • 3. Iteration statements
  • 4 Jump statements

3 of 23

Control Structures

  • 1. Sequential Control

  • Default flow: statements are executed one after another in order.

4 of 23

Sequential Control

  • #include <stdio.h>
  • int main() {
  • int a = 10, b = 20, sum;
  • sum = a + b; // executed first
  • printf("%d\n", sum); // executed next
  • return 0;
  • }

5 of 23

Sequential Control

Statement 1

Statement 2

Statement 3

6 of 23

Conditional Control (Selection Control or Decision Control) :-

  • In conditional control , the execution of statements depends upon the condition-test. If the condition evaluates to true, then a set of statements is executed otherwise another set of statements is followed.
  • This control is also called Decision Control because it helps in making decision about which set of statements is to be executed.

7 of 23

  • Decision control structure in C can be implemented by using:-
  • 1. If statement
  • 2. If-else statement
  • 3. Nested if else statement
  • 4. else-if ladder
  • 5. switch case

8 of 23

if statement:-

  • This is the most simple form of decision control statement.
  • In this form, a set of statements are executed only if the condition given with if evaluates to true.
  • Its general syntax and flow chart is as under:
  • - if(condition)
  • {
  • Statements ;
  • }

9 of 23

1. If Statement Flow chart

10 of 23

2. The if-else Statement:

  • In the if–else statement, if the expression/condition is true, the body of the if statement is executed; otherwise, the body of the else statement is executed.

11 of 23

2. The if-else Statement: Syntax

  • Syntax:
  • if(condition)
  • {
  • execute the statement1;
  • }
  • else
  • {
  • execute the statement2;
  • }

12 of 23

2. The if-else Statement: Syntax

  •  condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
  • �If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed

13 of 23

2. The if-else Statement: Flow Chart

14 of 23

3. Nested if else statement�

  •  
  • When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.

15 of 23

Nested if else statement Syntax

  • if (condition1) {
  • // code if condition1 is true
  • if (condition2) {
  • // code if BOTH condition1 and condition2 are true
  • }
  • else {
  • // code if condition1 is true but condition2 is false
  • }
  • }
  • else {
  • // code if condition1 is false
  • }

16 of 23

3.Nested if else statement: Flow chart

17 of 23

Nested if else statement Flow Chart�

18 of 23

�If else Ladder�

  • An if else ladder is a programming construct used for decision-making.
  • It consists of a series of if else statements chained together, forming a ladder-like structure.
  • Each if else statement is evaluated sequentially until a condition is found to be true, and the corresponding block of code is executed.
  • If none of the conditions are true, the code within the else block (if present) is executed.

19 of 23

Basic Syntax of If else Ladder:

  • if (condition1) {� // Code to be executed if condition1 is true�} else if (condition2) {� // Code to be executed if condition2 is true�} else if (condition3) {� // Code to be executed if condition3 is true�} else {� // Code to be executed if none of the conditions are true�}

20 of 23

21 of 23

switch case

  • The switch statement in C is a multi-way decision control structure that allows a program to execute different blocks of code based on the value of a single expression. 
  • It provides an alternative to using a series of if-else if statements when dealing with multiple specific conditions. 

22 of 23

switch case syntax

  • switch (expression) {�case constant1:�// Code to execute if expression matches constant1�break;�case constant2:�// Code to execute if expression matches constant2�break;�// ... more case statements�default:�// Code to execute if no case matches�}

23 of 23