The syntax of the if statement in C programming is:
if (test expression)
{
// code
}
The if statement evaluates the test expression inside the parenthesis ().
If the test expression is evaluated to true, statements inside the body of if are executed.
If the test expression is evaluated to false, statements inside the body of if are not executed.
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output 1
Enter an integer: -2
You entered -2.
The if statement is easy.
When the user enters -2, the test expression number<0 is evaluated to true. Hence, You entered -2 is displayed on the screen.
Output 2
Enter an integer: 5
The if statement is easy.
When the user enters 5, the test expression number<0 is evaluated to false and the statement inside the body of if is not executed
The if statement may have an optional else block. The syntax of the if..else statement is:
if (test expression) {
// run code if test expression is true
}
else {
// run code if test expression is false
}
If the test expression is evaluated to true,
statements inside the body of if are executed.
statements inside the body of else are skipped from execution.
If the test expression is evaluated to false,
statements inside the body of else are executed
statements inside the body of if are skipped from execution.
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Output
Enter an integer: 7
7 is an odd integer.
When the user enters 7, the test expression number%2==0 is evaluated to false. Hence, the statement inside the body of else is executed.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The if...else ladder allows you to check between multiple test expressions and execute different statements.
if (test expression1) {
// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
// Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Output
Enter two integers: 12
23
Result: 12 < 23
It is possible to include an if...else statement inside the body of another if...elsestatement.
This program given below relates two integers using either <, > and = similar to the if...else ladder's example. However, we will use a nested if...else statement to solve this problem.
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2) {
if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
If the body of an if...else statement has only one statement, you do not need to use brackets {}.
For example, this code
if (a > b) {
printf("Hello");
}
printf("Hi");
is equivalent to
if (a > b)
printf("Hello");
printf("Hi");
The goto statement in C provides a way to jump to a specific labeled point in the code. This can be useful in certain situations, but it can also lead to code that is difficult to understand and maintain.
Label Definition: A label is defined by an identifier followed by a colon. For example:
label_name:
Unconditional Jump: When the goto statement is executed, control jumps to the specified label, skipping any code in between.
Scope: Labels are local to the function in which they are defined. You cannot jump to a label defined in another function.
Use Cases: Common scenarios for using goto include:
Breaking out of deeply nested loops.
Handling error cleanup in resource management.
Let's consider a simple example where we want to read numbers from the user until they enter a negative number. We will use goto to handle the loop control and exit the loop when a negative number is encountered.
#include <stdio.h>
int main() {
int number;
printf("Enter numbers (negative number to exit):\n");
start: // Label for the start of the loop
scanf("%d", &number); // Read a number from the user
if (number < 0) {
goto end; // Jump to the end label if the number is negative
}
printf("You entered: %d\n", number); // Print the entered number
goto start; // Jump back to the start label to read the next number
end: // Label for the end of the program
printf("Exiting the program.\n");
return 0;
}
Initialization: The program prompts the user to enter numbers.
Label start: This label marks the beginning of the loop where the program will read user input.
Input Handling: The scanf function reads an integer from the user.
Condition Check: If the entered number is negative, the program uses goto end to jump to the end of the program, skipping any further processing.
Output: If the number is non-negative, it prints the number and then uses goto start to repeat the process.
Label end: This label is where the program jumps when a negative number is entered, allowing for a clean exit.
Simplicity in Control Flow: In certain situations, goto can simplify the control flow, particularly in error handling or breaking out of nested loops.
Readability Issues: Code with goto can become hard to follow, especially in larger programs, leading to what is often referred to as "spaghetti code."
Maintainability: Future maintainers of the code may find it challenging to understand the flow of control, increasing the likelihood of bugs.
While the goto statement can be a useful tool in specific scenarios, it is essential to use it judiciously. In most cases, structured programming constructs such as loops and conditionals are preferred for their clarity and maintainability. When using goto, ensure that it enhances the readability of your code rather than detracting from it.