Fundamental Control Structure Statements in C #Part-1

Any real-life coding solution is almost impossible without decision making or control structures in programming. These decision making enable us to make program more actionable and determine the order in which the program’s instructions have to be executed and govern how control is transferred to various parts of the program. Control statements are classified into the following categories. Control statements in C enable us to determine the order in which the program’s instructions are executed and govern how control is transferred to various parts of the program. Control statements are classified into the following categories:

  1. Selection statements
  2. Iteration statements
  3. Jump statements.
c control statements
Table of Contents

Decision Making in C Language

Decision making statements are also known as selection statements or branching statements. C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value. A snapshot of all available control /decision making statements is –

#Statement & Description
1if statement — An if statement consists of a Boolean expression followed by one or more statements.
2if…else statement — An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
3if…else if… else statement — An if statement can be followed by one or more else…if  (condition checking)  statement(s), which executes when the any Boolean expression is true otherwise else part will be executed.
4Nested  if statements — You can use one if or else if statement inside another  if or else… if statement(s).
5switch  statement — A switch statement allows a variable to be tested for equality against a list of values.

1) If statement as a control structure

 Syntax: The syntax of an if( ) statement in C programming language is:
if(boolean_expression)

{ /* statement(s) will execute if the Boolean expression is true */
}
  • If the Boolean expression evaluates to true, the code block inside the if statement will be executed.
  • If the Boolean expression evaluates to false, the code block following the end of the if statement (after the closing curly brace) will be executed.
  • In the C programming language, any non-zero and non-null values are considered true, while zero or null values are considered false.
Flow Diagram:

Decision making statement specifies: –

  • one or more conditions to be evaluated by the program, and
  • Based on result (as true or false) the specific branch/part of the program has to be executed.
  • The general form of a typical decision making structure is shown here in the fig:
if statement flow chart

2) if –else statement as a control structure

Flow Diagram:

Once the Boolean condition is true then one set of statement block will undergo the execution, otherwise the false set o statement block will undergo the execution. Here in flowchart Statement-1 are being followed when condition is false, and Statement-2 is being followed when condition is true.

Syntax: The syntax of an if…else statement in C programming language is:
if(boolean_expression)

{ /* statement(s) will execute if the boolean expression is true */

}

else

{ /* statement(s) will execute if the boolean expression is false */

}

If the Boolean expression evaluates to true then the if block of code will be executed otherwise else block of code will be executed programming language assumes any non-zero and non-null values as true and if it is either zero or null then it is assumed as false value.

Please note, in case you have only one statement under if () of else part then use of curly brash { } is optional, because if () or else part, by default automatically associate one statement and having more than one statements association with if () or else is fully dependable on curly brash {}.

Example: Are you permit to cast your vote?
// Example of If...else :
// Program to permit to cast vote if your age is 18 or more,
// otherwise give message to wait till 18.
#include<stdio.h>
 void main()
 {
 	int age;
 	printf("\n Kindly share your age : ");
 	scanf("%d",&age);
 	
 	// check age and give a message
 	if (age >= 18)
 		printf("\n You can cast your vote");
 	else 
 		printf("\n Oh! you have wait till your age get 18");
 }

In a next post you will find details about nested if…else, if…else if… else and switch statements.

Posted in Uncategorized
Write a comment