Place Your Query
-
C Programming
-
- Control flow-based C Programs
- Enjoy Conditional Programming in C using If...else and switch statement.
- Good, Now write a C program to display "Hello World" on the screen.
- Write a C program to display Your Name, Address and City in different lines.
- C program to find sum of n numbers
- Write a C program For Addition of two numbers using Function.
- Write a Program to Find Simple Interest and Compound Interest.
- Write a Program to Convert Celsius To Fahrenheit and Vice Versa.
- Write a Program to Find Area and Perimeter of Circle, Triangle, Rectangle and Square.
- Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables
- Write a C Program to Design a Simple Menu Driven Calculator
- Simple Expression Based C Programs
-
- 7. Components of C language
- 1. Introduction to C Programming Language
- 10. Operator Precedence and Associativity
- 11. Comments in C Programming
- 14. Fundamental Control Structure Statements in C [Part-1]
- 15. Fundamental Control Structure Statements in C [Part-2]
- 16. Looping Statements [Fundamental Control Structure Statements in C. #Part-3]
- 17. Keyword break, continue, return and exit [Fundamental Control Structure Statements in C. #Part-4]
- 2. Computer Languages
- 3. Interpreters vs Compilers vs Assemblers in programming languages
- 4. C Program Structure
- 5. Compile and Execute C Program
- 6. Errors in C Program
- 8. C Datatypes
- 9. Operators in C
- Control flow-based C Programs
- Demystifying Bit Masking: Unlocking the Power of Bitwise Operators in C-Language with best Examples.
- 18. Fundamentals of C Functions
- Show Remaining Articles (3)Collapse Articles
-
-
Java
-
AI ML
-
FAQs
- A Program to find Tokens in C code?
- What are the Common Coding Mistakes.
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
-
Python Interview Questions
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
14. 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:
- Selection statements
- Iteration statements
- Jump statements.
Quick Links
Decision Making in C Language
Decision making statements are also known as selection statements or branching statements. C programming language assumes anynon-zeroandnon-nullvalues astrue, and if it is eitherzeroornull, then it is assumed asfalsevalue. A snapshot of all available control /decision making statements is –
# | Statement & Description |
---|---|
1 | if statement — An if statement consists of a Boolean expression followed by one or more statements. |
2 | if…else statement — An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. |
3 | if…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. |
4 | Nested if statements — You can use one if or else if statement inside another if or else… if statement(s). |
5 | switch 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:
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.