C If Statement Types Of Conditional Statements In C
Download PDF
C – if Statement
The if in C is the most simple decision-making statement. It consists of the test condition and if block or body. If the given condition is true only then the if block will be executed.
What is if in C?
The if in C is a decision-making statement that is used to execute a block of code based on the value of the given expression. It is one of the core concepts of C programming and is used to include conditional code in our program.
Syntax of if Statement in C
if(condition) < // if body // Statements to execute if condition is true >
How to use if statement in C?
The following examples demonstrate how to use the if statement in C:
C
// C Program to demonstrate the syntax of if statement int gfg = 9; // if statement with true condition if (gfg < 10) < printf ( "%d is less than 10" , gfg); // if statement with false condition if (gfg > 20) < printf ( "%d is greater than 20" , gfg); Output9 is less than 10
How if in C works?

Working of if Statement in C
The working of the if statement in C is as follows:
- STEP 1: When the program control comes to the if statement, the test expression is evaluated.
- STEP 2A: If the condition is true, the statements inside the if block are executed.
- STEP 2B: If the expression is false, the statements inside the if body are not executed.
- STEP 3: Program control moves out of the if block and the code after the if block is executed.
Flowchart of if in C

Flow Diagram of if Statement in C
Examples of if Statements in C
Example 1: C Program to check whether the number is even or odd.
In this program, we will make use of the logic that if the number is divisible by 2, then it is even else odd except one.
C
// C Program to check if the number is even or odd int n = 4956; // condition to check for even number if (n % 2 == 0) < printf ( "%d is Even" , n); // condition to check for odd number printf ( "%d is Odd" , n); Output4956 is Even
Example 2: C Program to check whether a number is prime or not.
In this program, we will check for the smallest factor of the given number N starting from 2 to sqrt (N) using a loop. Whenever we find the factor, we will set the flag and exit the loop. The code to be executed will be contained inside the if statement.
C
// C program to check whether a number is prime or not int flag = 0; for ( int i = 2; i * i <= n; i++) < // If n is divisible by any number between // 2 and n/2, it is not prime if (n % i == 0) < printf ( "%d is " , n); if (flag == 1) < // it is only printed if the number is not prime printf ( "not " ); printf ( "a prime number.\n" ); Output19 is a prime number.
Advantages of if Statement
Following are the main advantages of the if statement in C:
- It is the simplest decision-making statement.
- It is easy to use and understand.
- It can evaluate expressions of all types such as int, char, bool, etc.
Disadvantages of if Statement
The main limitations of if block is listed below:
- It contains only a single block. In case when there are multiply related if blocks, all the blocks will be tested even when the matching if block is found at the start
- When there are a large number of expressions, the code of the if block gets complex and unreadable.
- It is slower for a large number of conditions.
Conclusion
The if statement is the simplest decision-making statement due to which it is easy to use and understand. But being simple, it also has many limitations. We can use if-else, if-else-if ladder, or switch statements to overcome these limitations. Still, the if statement is widely used in C programming to add some conditional code to the program.
FAQs on if in C
1. Define C if staement.
The if statement is a program control statement in C language that is used to execute a part of code based on some condition.
2. How many types of decision-making statements are there in the C language?
There are 5 types of conditional statements or decision-making statements in C language:
- if Statement
- if-else Statement
- if-else-if Ladder
- switch Statement
- Conditional Operator
3. Can we specify multiple conditions in if statement?
We can specify multiple conditions in the if statement but not separately. We have to join these multiple conditions using logical operators making them into a single expression. We can then use this expression in the if statement.
Valid Expressions
if (a < b && a < c); if (a == 25 || a < 25);
Invalid Expressions
if (a < b, a < c);
In the above expression, the rightmost expression in the parenthesis will be considered.
Like Article -->Please Login to comment.
Similar Reads
Print "Even" or "Odd" without using conditional statementWrite a program that accepts a number from the user and prints "Even" if the entered number is even and prints "Odd" if the number is odd. You are not allowed to use any comparison (==, <,>. etc) or conditional statements (if, else, switch, ternary operator,. Etc). Method 1 Below is a tricky code can be used to print "Even" or "Odd" accordi
4 min read Return Statement vs Exit() in main() in C++The return statement in C++ is a keyword used to return the program control from the called function to the calling function. On the other hand, the exit() function in C is a standard library function of <stdlib.h> that is used to terminate the process explicitly. The operation of the two may look different but in the case of the main() funct
3 min read C++ Break StatementThe break in C++ is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there and control returns from the loop immediately to the first statement after the loop. Syntax: break; Basically, break statements are used in situations when we are not sure
5 min read goto Statement in CThe C goto statement is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label; In the above syntax, the first line te
3 min read Break Statement in CThe break statement is one of the four jump statements in the C language. The purpose of the break statement in C is for unconditional exit from the loop What is break in C? The break in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block.
6 min read C if. else StatementThe if-else statement in C is a flow control statement used for decision-making in the C program. It is one of the core concepts of C programming. It is an extension of the if in C that includes an else block along with the already existing if block. C if Statement The if statement in C is used to execute a block of code based on a specified condit
6 min read Return Statement in CPre-requisite: Functions in C C return statement ends the execution of a function and returns the control to the function from where it was called. The return statement may or may not return a value depending upon the return type of the function. For example, int returns an integer value, void returns nothing, etc. In C, we can only return a single
4 min read Continue Statement in CThe continue statement in C is a jump statement that is used to bring the program control to the start of the loop. We can use the continue statement in the while loop, for loop, or do..while loop to alter the normal flow of the program execution. Unlike break, it cannot be used with a C switch case. What is continue in C? The C continue statement
5 min read Data type of case labels of switch statement in C++?In C++ switch statement, the expression of each case label must be an integer constant expression. For example, the following program fails in compilation. C/C++ Code /* Using non-const in case label */ #include<stdio.h> int main() < int i = 10; int c = 10; switch(c) < case i: // not a "const int" expression printf("Value of c
2 min read Interesting facts about switch statement in CPrerequisite - Switch Statement in C Switch is a control statement that allows a value to change control of execution. C/C++ Code // Following is a simple program to demonstrate syntax of switch. #include <stdio.h> int main() < int x = 2; switch (x) < case 1: printf("Choice is 1"); break; case 2: printf("Cho
3 min read Implementing ternary operator without any conditional statementHow to implement ternary operator in C++ without using conditional statements.In the following condition: a ? b: c If a is true, b will be executed. Otherwise, c will be executed.We can assume a, b and c as values. 1. Using Binary Operator We can code the equation as : Result = (!!a)*b + (!a)*c In above equation, if a is true, the result will be b.
4 min read Switch Statement in CSwitch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases). Switch case statements follow a selection-control mechanism and allow a value to change control of
8 min read C Programming Language TutorialIn this C Tutorial, you’ll learn all C programming basic to advanced concepts like variables, arrays, pointers, strings, loops, etc. This C Programming Tutorial is designed for both beginners as well as experienced professionals, who’re looking to learn and enhance their knowledge of the C programming language. What is C?C is a general-purpose, pro
8 min read printf in CIn C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). The printf function is a part of the C standard library <stdio.h> and it can allow formatting the output in numerous ways. Syntax of printfprintf ( "formatted_string", arguments_list);Parametersformatted_st
5 min read Pattern Programs in CPrinting patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems generally require the knowledge of loops and if-else statements. In this article, we will discuss the following example progra
15+ min read C ProgramsTo learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions,
8 min read C Programming Interview Questions (2024)At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the old and most popular programming languages. There are many applications in which C programming language is used, including language compilers, operating systems,
15+ min read scanf in CIn C programming language, scanf is a function that stands for Scan Formatted String. It is used to read data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments. It accepts character, string, and numeric data from the user using standard input.scanf also uses format specifiers like printf.sc
2 min read C Hello World ProgramThe “Hello World” program is the first step towards learning any programming language and also one of the simplest programs you will learn. To print the "Hello World", we can use the printf function from the stdio.h library that prints the given string on the screen. C Program to Print "Hello World"The following C program displays "Hello World" in
2 min read Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations. As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example, If there is
9 min read Format Specifiers in CThe format specifier in C is used to tell the compiler about the type of data to be printed or scanned in input and output operations. They always start with a % symbol and are used in the formatted string in functions like printf(), scanf, sprintf(), etc. The C language provides a number of format specifiers that are associated with the different
6 min read Substring in C++The substring function is used for handling string operations like strcat(), append(), etc. It generates a new string with its value initialized to a copy of a sub-string of this object. In C++, the header file which is required for std::substr(), string functions is <string>. The substring function takes two values pos and len as an argument
8 min read Strings in CA String in C programming is a sequence of characters terminated with a null character '\0'. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'. C String Declaration SyntaxDeclaring a string in C is as simple as declaring a one-
8 min read Decision Making in C (if , if..else, Nested if, if-else-if )The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C programs. They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not. These decision-making sta
11 min read Operators in CIn C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples. What is a C Operator?An operator in C can be defined as the symbol that helps us to perform some specific math
14 min read C PointersPointers are one of the core components of the C programming language. A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C. In this article, we will discuss C pointers in detail, their
14 min read Data Types in CEach variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data type is a collection of data with values having fixed values, meaning
7 min readArray in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array syntax, advantages and disadvantages, and many more
15+ min read Bitwise Operators in CIn C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The | (bitwise OR) in C takes two n
7 min read C Language IntroductionC is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language to write the UNIX operating system. The main features of the C language include: General Purpose and PortableLow-level Memory AccessFast SpeedClean SyntaxThese