What is Control Structures?

What is Control Structures?

The control statement controls the flow of our C program.

what-is-control-structures
Source: Pixabay

There are 2 types of control statements.

  • Condition Statement
  • Loop Statement

1. Conditional Statement: -

The conditional statement is used in the C program to execute a certain code on a certain condition.

These are of the following types.

If the statement: -

In this, when the condition is true, the statement is effective.

  Syntax: -

  If (condition)

  ,

  // Statement

  ,

  If the statement example program: -

  # Include <stdio.h>

  int main ()

  ,

  int num = 10;

  If (number == 10)

  ,

  printf ("number is 10");

  ,

  return 0;

  ,

  Output:-

  The number is 10

ii.  if-else statement: -

In the if-else statement, the code of the if-statement will be executed if the condition is true, the code of the else statement will be executed if the condition is false.

  Syntax: -

  If (condition)

  ,

  // Statement

  ,

  other

  ,

  // Statement

  ,

  If-else statement example program: -

  # Include <stdio.h>

  int main ()

  ,

  int age;

  printf ("Enter your age:");

  scanf ("% d", & age);

  If (age> = 10)

  ,

  printf ("you are big");

  ,

  other

  ,

  printf ("you are young");

  ,

  return 0;

  Output: -

  Enter your age: 15

  You are young


  iii.  If nested: -

  When we use if inside if, it is called nested if.

  Syntax: -

  If (condition)

  ,

  If (condition)

  ,

  // Statement

  ,

  other

  ,

  // Statement

  ,

  ,

  other

  ,

  // Statement

  ,

  iv ladder if-otherwise: -

  Nested if is an improved form of -else, where we don't have to write if inside if.

  Sentence Structure :-

  If (condition 1)

  ,

  // Statement

  ,

  Otherwise (condition 2)

  ,

  // Statement

  ,

  ,

  ,

  ,

  Otherwise if (condition)

  ,

  // Statement

  ,

  other

  ,

  // Statement

  ,

  Ladder if example program: -

  # Include <stdio.h>

  int main ()

  ,

  int Persent;

  printf ("Enter your Persentage:");

  scanf ("% d", & Persent);

  If ((Persent> = 33) && (Persent <= 49))

  ,

  printf ("third division");

  ,

  Otherwise if ((Psent> = 50) && (Persent <= 59))

  ,

  printf ("2nd section");

  ,

  Otherwise if (permanent> = 60)

  ,

  printf ("1st Division");

  ,

  other

  printf ("failed");

  ,

  return 0;

  ,

  Output:

  Enter your percentage: 85

  1st Division

Loop Statement: -

A loop is used to execute a single statement or multiple statements more than once.

The loops in C programming are of the following types-

  •   For the loop
  •   When the loop
  •   Do-While Loop

  For I.  In this, the statements are executed until the condition is true.

  Sentence Structure :-

  For (startup; condition; increase / decrease)

  ,

  // code

  ,

  Example program: -

  # Include <stdio.h>

  int main ()

  ,

  int i;

  For (i = 0; i <= 10; i ++)

  ,

  printf ("i's value:% d \ n", i);

  ,

  return 0;

  ,

  Output: -

  The value of i: 0

  The value of i: 1

  The value of i: 2

  The value of i: 3

  The value of i: 4

  The value of i: 5

  The value of i: 6

  The value of i: 7

  The value of i: 8

  The value of i: 9

  The value of i: 10


  2.  When loop: -

  This is an entry controlled loop.  In this loop the condition is checked first.  In this, the statements are executed until the condition is true.

  Syntax: -

  Start

  When (condition)

  ,

  // code

  Increase / decrease;

  ,

  Example program: -

  # Include <stdio.h>

  int main ()

  ,

  int i = 0;

  When (i <= 10)

  ,

  printf ("i's value:% d \ n", i);

  i ++;

  ,

  return 0;

  ,

  Output:-

  The value of i: 0

  The value of i: 1

  The value of i: 2

  The value of i: 3

  The value of i: 4

  The value of i: 5

  The value of i: 6

  The value of i: 7

  The value of i: 8

  The value of i: 9

  The value of i: 10


  III. This loop is also like a while loop but it executes the statement first and then checks the condition.  In this loop, the statement task is performed only once.

  Syntax:

  Start

  do

  // code

  Increase / decrease;

  } while (condition);

  Example program: -

  # Include <stdio.h>

  int main ()

  ,

  int i = 0;

  do

  printf ("i's value:% d \ n", i);

  i ++;

  } while (i <= 10);

  return 0;

  ,

  Output:-

  The value of i: 0

  The value of i: 1

  The value of i: 2

  The value of i: 3

  The value of i: 4

  The value of i: 5

  The value of i: 6

  The value of i: 7

  The value of i: 8

  The value of i: 9

  The value of i: 10


  Break statement: -

  Use of Break Keyword is out of loop (for, while, do-while) and Swith statement when a certain condition is satisfied.

  Example of brake statement for loop with program: -

  # Include <stdio.h>

  int main ()

  ,

  int i;

  For (i = 1; i <= 10; i ++)

  ,

  If (i == 6)

  ,

  pause

  ,

  printf ("% d \ t", i);

  ,

  return 0;

  ,

  Output:-

  1 2 3 4 5

  Note: - You can use brakes in any loop in the same way as brakes are used in use for loop.


  Continue the statement: -

  Continued keyword usage avoids the exact repetition of a certain condition loop and continues the loop from the next iteration.

  Continue the example program: -

  # Include <stdio.h>

  int main ()

  ,

  int i;

  For (i = 1; i <= 10; i ++)

  ,

  If (i == 6)

  ,

  Continue;

  ,

  printf ("% d \ t", i);

  ,

  return 0;

  ,

  Output: -

  1 2 3 4 5 7 8 9 10


  Go to statement: -

  Goto Statement C can also be used inside a function of the program.  With the help of Goto statement you can move from one place to another in the flow control function of the program.

  Sentence Structure :-

  // Statement

  Labels:

  // Statement

  Goto label;

  // Statement

  Go to the example program: -

  # Include <stdio.h>

  int main ()

  ,

  int i = 1;

  Myprint: // label

  printf ("% d \ t", i);

  i ++;

  If (i <= 10)

  ,

  Goto Myprint;  // Go to the statement with the count label

  ,

  return 0;

  ,

  Output: -

  1 2 3 4 5 6 7 8 9 10

  Switch statement: -

  The switch statement is also used like if else.

  Syntax: -

  Switch (variable or integer expression)

  ,

  Case value 1:

  // statement;

  pause

  Case value 2:

  // statement;

  pause

  Case value 3:

  // statement;

  pause

  Default:

  // statement;

  ,

  Change the statement example program: -

  # Include <stdio.h>

  int main ()

  ,

  int num1, num2, answer, choice;

  printf ("Enter first number:");

  scanf ("% d", & num1);

  printf ("Enter the second number:");f

  scanf ("% d", & num2);

  printf ("1. Addition \ n");

  printf ("2. minus \ n");

  printf ("Enter your preferences:");

  scanf ("% d", & Choice);


  Switch (choice).

  Case 1:

  Ans = num1 + num2;

  printf ("Add:% d", Ans);

  pause

  Case 2:

  Ans = num1-num2;

  printf ("minus:% d", Ans);

  pause

  Default:

  printf ("invalid choice");

  ,

  return 0;

  ,

  Output:-

  Enter the first number: 36

  Enter the second number: 25

  1. Addition

  2. Subtraction

  Enter your preferences: 1

  Additions: 61