C If Statement
C If Statement
C If Statement
else Statement
In this tutorial, you will learn about if statement (including if...else and nested if..else) in C programming
with the help of examples.
C if Statement
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.
Example 1: if statement
// Program to display a number if it is negative
#include <stdio.h>
void main()
{
int number;
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
C if...else Statement
The if statement may have an optional else block. The syntax of the if..else statement is:
if (test expression)
{
// statements to be executed if the test expression is true
}
else
{
// statements to be executed if the test expression is false
}
How if...else statement works?
C if...else Ladder
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.
Nested if...else
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);
return 0;
}
If the body of an if...else statement has only one statement, you do not need to use brackets {}.
In this tutorial, you will learn to create the switch statement in C programming with the help of an
example.
The switch statement allows us to execute one code block among many alternatives.
You can do the same thing with the if...else..if ladder. However, the syntax of the switch statement is
much easier to read and write.
Syntax of switch...case
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
How does the switch statement work?
If there is a match, the corresponding statements after the matching label are executed. For
example, if the value of the expression is equal to constant2, statements after case
constant2: are executed until break is encountered.
If we do not use break, all statements after the matching label are executed.
int main() {
char operator;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
return 0;
}
Output
Enter an operator (+, -, *,): -
Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1
The - operator entered by the user is stored in the operator variable. And, two
operands 32.5 and 12.4 are stored in variables n1 and n2 respectively.
C break
The break statement ends the loop immediately when it is encountered. Its syntax is:
break;
The break statement is almost always used with if...else statement inside the loop.
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
scanf("%lf", &number);
break;
}
}
return 0;
}
Output
Enter a n4: -3
Sum = 10.30
This program calculates the sum of a maximum of 10 numbers. Why a maximum of 10 numbers? It's
because if the user enters a negative number, the break statement is executed. This will end
the for loop, and the sum is displayed.
In C, break is also used with the switch statement. This will be discussed in the next tutorial.
C continue
The continue statement skips the current iteration of the loop and continues with the next iteration. Its
syntax is:
continue;
// If the user enters a negative number, it's not added to the result
#include <stdio.h>
int main() {
int i;
continue;
return 0;
Output
Enter a n10: 12
Sum = 59.70
In this program, when the user enters a positive number, the sum is calculated using sum +=
number; statement.
When the user enters a negative number, the continue statement is executed and it skips the negative
number from the calculation.