Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » C Language » Statements in C Language | Types of statements in C Language

  • Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा
    Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा Festival
  • Kapil Sharma Show, Comedy Show in Hindi
    Kapil Sharma Show, Comedy Show in Hindi Biography
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • Ayodhya Kand in Hindi | अयोध्या काण्ड | राम को 14 वर्ष का वनवास
    Ayodhya Kand in Hindi | अयोध्या काण्ड | राम को 14 वर्ष का वनवास Spiritual
  • Diwali The Festival of Lights
    Diwali 2022, Indian Festival of Lights essay (दिवाली त्योहार पर निबंध, कहानी) Festival
  • Human rights day
    Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे General Knowledge
  • Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय)
    Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय) Biography
  • Mahatma Gandhi Essay in Hindi | Gandhiji Biography
    Mahatma Gandhi Essay in Hindi | Gandhiji Biography Biography

Statements in C Language | Types of statements in C Language

Posted on November 20, 2021June 5, 2022 By GeekCer Education No Comments on Statements in C Language | Types of statements in C Language
Statements in C Language | Types of statements in C Language

Statements in language are the sections of a C program that are executed in a certain order and are used to construct the body of a function. Any function’s body is a compound statement, which is made up of a series of statements and declarations.

How many types of statements in C? We can divide the statements in C language in a variety of ways, but we’ve categorized them into the following 3 main categories.

  • Conditional Statements
  • Loop Statements
  • Jump Statements

Table of Contents

  • Conditional Statements in C – if, if-else, nested if, else-if ladder and switch
    • if statement
      • Syntax of if statement
      • Write a program using if statement
    • if-else statement
      • Syntax of if-else statement
      • Write a program using if-else statement
    • nested if statement
      • Syntax of nested if statement
      • Write a program using nested if statement
    • else-if ladder
      • Syntax of nested else-if ladder statement
      • Write a program using if-else ladder statement
    • switch statement in C language
      • Syntax of switch statement
      • Write a program using switch statement
      • Key points related to switch statement
  • Loop Statements in C – while Loop, do..while loop, for loop
    • while loop in C
      • Syntax of While Loop
      • Write a program using while loop
    • do…while loop in C
      • Syntax of do…while loop
      • Write a program using do…while loop
    • for loop in C
      • Syntax of do…while loop
      • Write a program using for loop
  • Jump Statements in C – break, continue, goto statement
    • Break Statement in C
      • Example of break statement
    • Continue Statement in C
      • Example of continue Statement
    • goto statement in C
      • Syntax of goto statement
      • Unconditional goto statement
        • Example of unconditional goto statement
      • Conditional goto statement
        • Example of conditional goto statement

Conditional Statements in C – if, if-else, nested if, else-if ladder and switch

Conditional statements are mostly used to make decisions. In decision-making, a statement performs a task based on whether or not a condition is true or false.

In the C programming language, the conditional statements can be used in a variety of ways.

  • if statement
  • if-else statement
  • nested if statement
  • else-if Ladder
  • switch statement

if statement

A conditional statement is written using the if statement. If the specified condition is true, the statements will be executed; else, the optional statements will be executed.

Syntax of if statement


if(condition)
{
  -----
  -----
}

Write a program using if statement

#include <stdio.h>
void main()
{
  int a = 12;
  if(a % 2 == 0)
  {
    printf("Number is Even");
  }
}     

if-else statement

A conditional statement is written using an if-else statement. If the specified condition is true, the statements will be executed. Otherwise, the else block statement will be executed.

Syntax of if-else statement


if(condition)
{
  -----
  -----
}
else 
{
  -----
  -----
}

Write a program using if-else statement

#include <stdio.h>
void main()
{
  int a = 12;
  if(a % 2 == 0)
  {
    printf("Number is Even");
  }
  else
  {
    printf("Number is Odd");
  }
}     

nested if statement

Nested if is an if statement within an if statement.

Syntax of nested if statement


if(outer-condition)
{
  if(inner-condition)
  {
    -----
    -----
  }
}

Write a program using nested if statement

#include <stdio.h>
void main()
{
  int a = 10;
  if(a % 2 == 0)
  {
    if(a % 5 == 0)
    {
      printf("Number is Divisible by 5");
    }
    printf("Number is Divisible by 2");
  }
}     

else-if ladder

Syntax of nested else-if ladder statement


if(condition 1)
{
  -----
  -----
}
else if(condition 2)
{
  -----
  -----
}
else if(condition 3)
{
  -----
  -----
}
else 
{
  -----
  -----
}

Write a program using if-else ladder statement

#include <stdio.h>
void main()
{
  int a = 15;
  if(a % 2 == 0)
  {
    printf("Number is Divisible by 2");
  }
  else if(a % 5 == 0)
  {
    printf("Number is Divisible by 5");
  }
  else if(a % 7 == 0)
  {
    printf("Number is Divisible by 7");
  }
  else
  {
    printf("Number is not Divisible by 2, 5 or 7");
  }
}     

switch statement in C language

The switch statement is a multiway decision maker that compares an expression to one of a set of constant values and braces them accordingly. Because several if-statements are time expensive, a switch case statement is a better solution.

Syntax of switch statement


switch (expression)
{
  case constant-1: 
    statement;  
    break;
  case constant-2: 
    statement;  
    break;
  .
  .
  case constant-n: 
    statement;  
    break;
  default: 
    statement;
}

Write a program using switch statement


#include <stdio.h>
#include <conio.h>
void main()
{
  int a, b, c, choice;
  while(choice != 3)
  {
    printf("\n 1. Press 1 for Addition");
    printf("\n 2. Press 2 for Subtraction");
    printf("\n Enter your choice: ");
    scanf("%d",&choice);
    switch(choice)
    {
      case 1:
        printf("Enter two numbers: ");
        scanf("%d%d",&a,&b);
        c = a + b;
        printf("%d",c);
        break;;
      case 2:
        printf("Enter two numbers: ");
        scanf("%d%d",&a,&b);
        c = a - b;
        printf("%d",c);
        break;;
      default:
        printf("You have passed a wrong key.");
        printf("\n Press any key to continue"); 
    }
  } 
}   

Key points related to switch statement

  • The break statement is optional in this case. However, if we do not write a break, many lines of code will be run.
  • The inside-the-switch expression should provide a constant value.
  • Case values that are similar are not permitted.
  • Each of the case statements constants must be of the same type.
  • The value after the keyword case can only be constants; expressions are not allowed. Integers and characters are allowed, but not floating point values or character strings.

Loop Statements in C – while Loop, do..while loop, for loop

Loop statements repeat the execution of a statement.

while loop in C

When we aren’t sure if the loop will be run, we use a while loop. The condition of a while loop is verified first, and then the statement is executed.

Syntax of While Loop

For a single statement


Variable initialization;
while (condition)
{
  statement 1;
  statement 2;
  ...
}

For a block of statements


#include <stdio.h>
int main()
{
  int i;
  i = 1;
  while(i <= 10)
  {
    printf("%d\t", i);
    i++;
  }
  return 0;
}   

Write a program using while loop


#include <stdio.h>
int main()
{
  int i;
  i = 1;
  while(i <= 10)
  {
    printf("%d\t", i);
    i++;
  }
  return 0;
}   

Output:
1  2  3  4  5  6  7  8  9  10

do…while loop in C

When we are certain about the condition, we use a do-while loop. It enters the loop at least once before determining if the provided condition is true or false. It is run at least once to see if any of the conditions are true or false.

Syntax of do…while loop


Variable initialization;
do
{
  statement 1;
  statement 2;
  ...
} while (condition);

Write a program using do…while loop


#include <stdio.h>
int main()
{
  int i;
  i = 1;
  do
  {
    printf("%d\t", i);
    i++;
  } while(i <= 10);
  return 0;
}   

Output:
1  2  3  4  5  6  7  8  9  10

for loop in C

In a for loop, there are three expressions. The first expression sets the index value, the second checks if the loop should be iterated, and the third sets the index value for future iteration.

Syntax of do…while loop


for (Initialization; condition; Increment/Decrement)
{
  statement 1;
  statement 2;
  ...
}

Write a program using for loop


#include <stdio.h>
int main()
{
  int i;
  for (i = 1; i <=10; i++)
  {
    printf("%d\t", i);
  }
  return 0;
}   

Output:
1  2  3  4  5  6  7  8  9  10

Jump Statements in C – break, continue, goto statement

  • break statement
  • continue statement
  • goto statement

Break Statement in C

  • Loop: The break statement is used to stop the loop statement from running.
  • Switch: When using the break statement in a switch-case, it must be used in each instance. If this is not the case, control will be passed at a next case.

The break statement will end control of the innermost loop in a nested loop, and execution will begin on the line after the innermost loop block.

Example of break statement


#include <stdio.h>
int main()
{
  int i;
  i = 1;
  while(i <= 10)
  {
    printf("%d\t", i);
    i++;
    if (i == 5) 
    {
      break; // Terminates the loop using break statement.
    }
  }
  return 0;
}   

Output:
1  2  3  4

Continue Statement in C

The continue statement skips the current iteration but keeps the loop running. It is used just for a condition, allowing us to bypass code for that condition.

Example of continue Statement


#include <stdio.h>
int main()
{
  int i;
  i = 1;
  while(i <= 10)
  {
    if (i == 5) 
    {
      i++;
      continue; // Skip the current iteration based on condition.
    }
    printf("%d\t", i);
    i++;

  }
  return 0;
}   

Output:
1  2  3  4  6  7  8  9  10

goto statement in C

By transferring control to another part of the program, the goto statement can modify the program’s execution sequence. The goto statement can be used in two ways.

  1. Unconditional goto
  2. Conditional goto

Syntax of goto statement


#include <stdio.h>
int main()
{
  start: printf("Welcome to Geek World!");
  goto start;
  return 0;
}   

Unconditional goto statement

The unconditional goto statement is used to transfer control from one program section to another without verifying any conditions.

Example of unconditional goto statement

#include <stdio.h>
int main()
{
  start: printf("Welcome to Geek World!");
  goto start;
  return 0;
}   

Output:
1  2  3  4  6  7  8  9  10

Conditional goto statement

By testing a condition, conditional goto is used to transfer control of execution from one area of the program to another.

Example of conditional goto statement

#include <stdio.h>
int main()
{
  int i;
  i = 10;
  while(true)
  {
    if (i < 0) 
    {
      goto error;
    }
    printf("%d\t", i);
    i--;

  }
  error: printf("\nNegative data found.");
  return 0;
}   

Output:
10  9  8  7  6  5  4  3  2  1  0
Negative data found.

Also Read:

  • C Programming File Handling
  • Importance of storage classes and example
  • How pointer is used in C language?
  • DBMS Data Independence
  • Notes of linked list in data structure
  • HR Interview Questions and Answers

C Programming File Handling
How pointer is used in C language?
Importance of storage classes and example

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

C Language Tags:How many types of statements in c?

Post navigation

Previous Post: Constants and Operators in C Language
Next Post: Array in C programming | Character Array in C

More Related Articles

Structure in C program with example | Nested structure in C Structure in C program with example | Nested structure in C C Language
Pointers in C Programming, Definition, Types & Example Pointers in C Programming, Definition, Types & Example C Language
Storage classes in C with examples (auto, extern, static, register) Storage classes in C with examples (auto, extern, register, static) C Language
Constants and Operators in C Language Constants and Operators in C Language C Language
Top 100 C programs for geeks Top 100 C programs for geeks C Language
Array in C programming | Character Array in C Array in C programming | Character Array in C C Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • C Home
  • Constants and Operators
  • Statements in C Language
  • Array in C
  • String Array in C
  • Functions in C
  • Pointers in C
  • Structure in C
  • Storage Classes
  • File Handling in C
  • Top C Programs
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved