CONTROL STRUCTURES – DECISION MAKING STATEMENTS
Introduction
Control structures in C are used to control the flow of execution of a program. Decision-making
statements allow the program to choose different paths of execution depending on whether a given condition is
true or false.
These statements are also called conditional statements.
The main decision-making statements in C are:
1. Simple if
2. if–else
3. Nested if–else
4. else if ladder
5. switch case
1. Simple if Statement
The simple if statement is the most basic decision-making statement in C. It is used to execute a block of
code only when a given condition is true. If the condition evaluates to false, the statements inside the if block
are skipped and program control moves to the next statement.
The condition given inside the if statement is usually a relational or logical expression. The condition
must evaluate to either true (non-zero) or false (zero).
Syntax:
if(condition)
{
statements;
}
Example:
int a = 10;
if(a > 5)
{
printf("a is greater than 5");
}
2. if–else Statement
The if–else statement is an extension of the simple if statement. It is used when there are two alternative
paths of execution. If the condition is true, the if block is executed; otherwise, the else block is executed.
This statement ensures that one and only one block of code executes, making it suitable for decision-
making situations like pass/fail, positive/negative, even/odd, etc.
Syntax
if(condition)
{
statements;
}
else
{
statements;
}
Example
int n = 7;
if(n % 2 == 0)
{
printf("Even number");
}
else
{
printf("Odd number");
}
3. Nested if–else Statement
A nested if–else statement is an if–else statement placed inside another if or else block. It is used when a
program needs to make multiple decisions based on different conditions.
In nested if–else, conditions are checked one after another. The execution depends on the outcome of
previous conditions. This structure is useful when decisions depend on more than one condition.
Syntax:
if(condition1)
{
if(condition2)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
Example:
int a = 10, b = 20;
if(a > b)
{
printf("a is greater");
}
else
{
if(b > a)
printf("b is greater");
else
printf("Both are equal");
}
4. else if Ladder
The else if ladder is used when there are multiple conditions to be checked sequentially. The conditions
are evaluated from top to bottom. As soon as one condition becomes true, its corresponding block is executed
and the rest of the ladder is skipped.
If none of the conditions are true, the optional final else block is executed. This structure is useful when
selecting one option out of many, such as grading systems or menu-driven programs.
Syntax:
if(condition1)
{
statements;
}
else if(condition2)
{
statements;
}
else if(condition3)
{
statements;
}
else
{
statements;
}
Example:
int marks = 75;
if(marks >= 90)
printf("Grade A");
else if(marks >= 75)
printf("Grade B");
else if(marks >= 50)
printf("Grade C");
else
printf("Fail");
5. Switch Case Statement
The switch case statement is a multi-way decision-making statement. It is used when the value of a
variable or expression is compared against multiple constant values. Based on the matching case, the
corresponding block of code is executed.
Each case must end with a break statement to prevent fall-through. The default case is executed when no
case matches. Switch case improves readability when dealing with menu-based or option-based programs.
Syntax Example
switch(expression) int ch = 2;
{ switch(ch)
case value1: {
statements; case 1:
break; printf("One");
case value2: break;
statements; case 2:
break; printf("Two");
default: break;
statements; default:
} printf("Invalid");
}
Decision Making Statements (Unconditional Statements) - goto, break and continue.
1. The Goto Statement
C supports the goto statement, which allows unconditional transfer of program control to another part of the code, marked by a label.
While the use of goto is discouraged in structured programming due to poor readability and maintenance challenges, it can be helpful in
some special cases, such as exiting from nested loops.
goto can make control jump both forward and backward.
Syntax:
goto label; or label: statement;
label: statement; goto label;
The label is a valid identifier followed by a colon and must be unique within the function.
Example:
if(a > b)
goto finish;
.
.
.
finish:
printf("Completed\n");
2. Break Statement
The break statement in C is used to terminate the execution of a loop or a switch statement immediately.
When a break is encountered inside a loop, control jumps to the “next statement” immediately following the loop.
Useful in nested loops to exit the innermost loop.
Syntax:
break;
Example:
for(int i = 1; i <= 10; i++)
{
if(i == 5)
break;
printf("%d ", i);
}
Here, the output will be 1 2 3 4.
3. Continue Statement
The continue statement is used to skip the remaining part of the loop body for the current iteration and transfers control to the next
iteration.
In for and while loops, it causes the control to move directly to the conditional test part of the loop.
Syntax:
continue;
Example:
for(int i = 1; i <= 10; i++)
{
if(i%2 == 0)
continue;
printf("%d ", i);
}
This program prints the odd numbers from 1 to 10. Continue statements skips the printf() for even numbers.
LOOPING STATEMENTS IN C
Looping statements in C are used to execute a set of statements repeatedly until a given condition becomes
false. Instead of writing the same code multiple times, loops allow programmers to write the code once and execute it
many times. This makes programs shorter, efficient, and easy to maintain.
In C programming, loops are broadly classified into entry controlled loops and exit controlled loops,
depending on when the condition is checked.
1. Entry Controlled Loops
In entry controlled loops, the condition is checked before entering the loop body.
If the condition is false at the beginning, the loop body will not execute even once.
Examples:
• while loop
• for loop
2. Exit Controlled Loops
In exit controlled loops, the condition is checked after executing the loop body.
Therefore, the loop body executes at least once, regardless of the condition.
Example:
• do–while loop
1. while Loop
The while loop is an entry controlled looping statement used when the number of repetitions is not known
in advance. The loop continues executing as long as the given condition remains true.
Syntax
while(condition)
{
statements;
}
How the Syntax Works (Step-by-Step)
1. The condition inside while() is evaluated first.
2. If the condition is true, the loop body executes.
3. After executing the loop body, control goes back to the condition.
4. This process repeats until the condition becomes false.
5. When the condition becomes false, the loop terminates.
Example Program
#include <stdio.h>
int main()
{
int i = 1; // initialization
while(i <= 5) // condition
{
printf("%d\n", i);
i++; // increment
}
return 0;
}
2. do–while Loop
The do–while loop is an exit controlled loop in which the loop body executes at least once, because the
condition is tested only after executing the statements.
Syntax
do
{
statements;
}
while(condition);
How the Syntax Works
1. The loop body executes first, without checking any condition.
2. After execution, the condition is checked.
3. If the condition is true, the loop repeats.
4. If the condition is false, the loop terminates.
Example Program
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d\n", i);
i++;
}
while(i <= 5);
return 0;
}
3. for Loop
The for loop is an entry controlled loop that is generally used when the number of iterations is known in
advance. It combines initialization, condition checking, and increment/decrement in a single line.
Syntax:
for(initialization; condition; increment/decrement)
{
statements;
}
How the Syntax Works
1. Initialization is executed once at the beginning.
2. Condition is checked before each iteration.
3. If condition is true, loop body executes.
4. After execution, increment/decrement is performed.
5. Control returns to condition checking.
6. Loop stops when condition becomes false.
Example Program
#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 5; i++)
{
printf("%d\n", i);
}
return 0;
}
4. Nested Loops
A nested loop is a loop inside another loop. The inner loop executes completely for each iteration of the
outer loop. Nested loops are commonly used for pattern printing, matrices, and tables.
General Syntax:
outer_loop(condition)
{
// outer loop statements
inner_loop(condition)
{
// inner loop statements
}
// statements after inner loop
}
How Nested Loops Work
1. Outer loop starts execution.
2. Inner loop executes fully.
3. Inner loop finishes → outer loop moves to next iteration.
4. Process repeats until outer loop condition becomes false.
Example Program:
#include <stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 3; i++)
{
for(j = 1; j <= 3; j++)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}
STRINGS IN C
In C programming, a string is defined as a sequence of characters stored in a character array and terminated
by a special character called the null character ('\0'). Since C does not provide a built-in string data type, strings are
handled using arrays of characters and a set of predefined string handling functions available in the <string.h> header
file. Strings are widely used to store and manipulate textual data such as names, addresses, and messages.
String Constant and String Variable
A string constant is a group of characters enclosed within double quotation marks and stored temporarily in memory.
A string variable is a character array used to store and manipulate string data during program execution.
Example
char name[20]; // string variable
printf("Hello"); // string constant
Declaration of String
A string is declared in C by declaring a character array of sufficient size to hold the characters along with the
null character. The size of the array determines the maximum length of the string that can be stored.
Size must include space for '\0'. Uses char data type. Acts like a normal array
Syntax
char string_name[size];
Example
char city[15];
Initialization of String
Initialization of a string means assigning an initial value to the string at the time of declaration. The compiler
automatically appends the null character at the end of the string.
Can be initialized using double quotes. '\0' added automatically. Easier and safer than manual initialization.
Example
char course[] = "C Programming";
Input and Output of String Data
Input and output of strings allow the user to enter and display textual data. Functions like scanf(), gets(),
printf(), and puts() are used for string input and output.
scanf("%s") reads till space, gets() reads full line (unsafe, deprecated), puts() automatically prints newline.
Example
char name[20];
scanf("%s", name);
printf("%s", name);
STRING LIBRARY FUNCTIONS(Header file: <string.h>)
1. strlen()
The strlen() function is used to determine the length of a given string. It counts the total number of characters
present in the string starting from the first character up to, but not including, the null character ('\0'). The function
returns the length as an integer value and is commonly used in loops, validations, and string processing operations.
Syntax
strlen(string_name);
Example
int len;
len = strlen("Hello"); // len = 5
2. strcat()
The strcat() function is used to concatenate two strings. It appends the contents of the second string to the end
of the first string by replacing the null character of the first string with the first character of the second string. After
concatenation, the resulting string is stored in the first string, which must have sufficient memory to hold the
combined string.
Syntax
strcat(string1, string2);
Example
char a[20] = "C";
char b[] = " Language";
strcat(a, b); // a becomes "C Language"
3. strcmp()
The strcmp() function is used to compare two strings character by character using their ASCII values. It
determines whether the strings are equal or which string is greater. The function returns zero if both strings are equal,
a positive value if the first string is greater than the second, or a negative value if the first string is smaller than the
second.
Syntax
strcmp(string1, string2);
Example
int result;
result = strcmp("abc", "abc"); // result = 0
4. strcpy()
The strcpy() function is used to copy one string into another string. It copies all characters of the source string,
including the null character, into the destination string. The destination string must be declared with sufficient size
before copying. After execution, both strings contain the same data, but they occupy different memory locations.
Syntax
strcpy(destination, source);
Example
char s1[20];
char s2[] = "Hello";
strcpy(s1, s2); // s1 becomes "Hello"
5. strrev()
The strrev() function is used to reverse the characters of a given string. It modifies the original string by
rearranging the characters in reverse order. Although strrev() is not part of the ANSI C standard, it is supported by
many compilers and is commonly included in academic syllabi and examinations.
Syntax
strrev(string_name);
Example
char name[] = "CSE";
strrev(name); // name becomes "ESC"