if and if-else statement in C programming language
- Get link
- X
- Other Apps
If and if-else statements in the C programming language
When we start writing a program, if the program is simple, like adding two numbers, or a Hello World program, we don't need any decision-making in such programs.
But as the programs get complicated, they become complex, and we must make decisions.
For such programs, we use conditional statements, which help in making decisions. One of the most important conditional statements is
1) if statement
2) if-else statement
What is the if condition?
It is a conditional statement used in almost every programming language. The if statement executes a piece of code based on a specific condition when it is true. When the condition is true, it executes the code with curly brackets.
What is the syntax of if statement
The if statement has a very simple syntax, as shown below
if (condition)
{
//This piece of code will run if the given condition is true
}
the above line of code tells us that when the condition is true the code written inside curly brackets { } will be executed. Let us understand by an example
now the if condition will check if num==5 then run the code with in brackets. This above piece of code will only run when the condition is true but what to do if the given condition is false. At this point, the if-else condition comes into the picture.
syntax of if-else condition
if(condition)
{
// If part of the code
}
else
{
// else part of code
}
in the above piece of code when the condition is true then the if part of the code will run which is num is equal to 5, and when the if condition is false then the else part will run which prints the message that num is not equal to 5
congratulations we have successfully implemented the if and if-else conditions in our code.
- Get link
- X
- Other Apps
Comments
Post a Comment