C control flow loops macros operators examples



Share




Learn C control flow statements, loops with examples using if...else, for Loop, while Loop, break and continue, switch...case..

Online courses related to this article


Course description

Learn, test your C programming skills and earn certificate for software jobs



Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs



Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs



Course description

Learn firmware development theory, applications, get practical skills from online tests, projects and earn certificate for software jobs

What are control statements in C?

Every program we write has a particular sequence of execution. Statements in a program are executed one after other. However, you need to control the sequence of execution of statement/s based on conditions. For example you need to display user credentials only if that particular user is logged in.

So to write good quality C programs to implement your project requirements, you must have a good control over your program execution sequence. You must have a good understanding over control statements.

Control statements in C are divided in three categories. Decision statements, Loop statements and jump statements.

if ... else statements, nested if ... else statements

if else condition is used to check if a given condition is met. We do certain things if the condition is met. Let's write a small program function to increase the integer passed to it and print the result.


#include "stdio.h"
void checkCond(int );
int main()
{
    int num;
    num = 12;
    checkCond(num);
    num = 20;
    checkCond(num);
    return 0;
}
void checkCond(int a)
{
    if((a > 10) && (a < 20))
    {
        a++;
        printf("Result is:%d\n", a);   
    }else
    {
        printf("Number is out of range!!");
    }
}
Program output:
==============
Result is:13
Number is out of range!!

In the above program the function checkCond accepts an integer, checks if the number is between 10 and 20(excluding 10 and 20) and prints the result after incresing by 1. If the condition is not true it prints a message "Number is out of range!!"

In nested if statements an if statement is included inside another if statement.

Let's write a small program to demonstrate the use of nested if statements as shown below.


#include "stdio.h"
void checkNestedCond(int );
int main()
{
    int num;
    num = 12;
    checkNestedCond(num);
    num = 15;
    checkNestedCond(num);
    num = 19;
    checkNestedCond(num);
    num = 21;
    checkNestedCond(num);
    return 0;
}
void checkNestedCond(int a)
{
    if((a > 10) && (a < 20))
    {
        if((a > 10) && (a < 14))
        {
            printf("Number is between 10 and 14\n");
        }else if((a > 14) && (a < 18))
        {
            printf("Number is between 14 and 18\n");
        }else
        {
            a++;
            printf("Result is:%d\n", a);   
        }
    }else
    {
        printf("Number is out of range!!");
    }
}
Program output:
==============
Number is between 10 and 14
Number is between 14 and 18
Result is:20
Number is out of range!!

In the above program within range of 10 and 20, we checked if number is between 10 to 14 or if the number is between 14 to 18 or the number is greater than 17 using nested if statements.

for loop, while loop, do while loop, goto, switch statements


Share