C If Statement

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

C if...

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

The syntax of the if statement in C programming is:


if (test expression)
{
// statements to be executed if the test expression is true
}
// Enter marks print pass if marks are above 32 else print fail
printf(“Enter Marks: “);
scanf(“%d”,&marks);
if (marks > 32)
{
printf(“Pass”);
}

How if statement works?

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.

Example 1: if statement
// Program to display a number if it is negative
#include <stdio.h>
void 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

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?

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.

Example 2: if...else statement


// 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.

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.

Syntax of if...else Ladder


if (test expression1)
{
// statement(s)
}
else if(test expression2)
{
// statement(s)
}
else if (test expression3)
{
// statement(s)
}
.
.
else {
// statement(s)
}

Example 3: C if...else Ladder


// 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 are equal",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);
}
}
Output
Enter two integers: 12
23
Result: 12 < 23

Nested if...else

It is possible to include an if...else statement inside the body of another if...else statement.

Example 4: 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);

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) {
print("Hello");
}
print("Hi");
is equivalent to
if (a > b)
print("Hello");
print("Hi");
C switch Statement

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?

The expression is evaluated once and compared with the values of each case label.

 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 there is no match, the default statements are executed.

If we do not use break, all statements after the matching label are executed.

By the way, the default clause inside the switch statement is optional.

switch Statement Flowchart


Example: Simple Calculator

// Program to create a simple calculator


#include <stdio.h>

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;

// operator doesn't match any case constant +, -, *, /


default:
printf("Error! operator is not correct");
}

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.

Since the operator is -, the control of the program jumps to

printf("%.1lf - %.1lf = %.1lf", n1, n2, n1-n2);

Finally, the break statement terminates the switch statement.

C break and continue


We learned about loops in previous tutorials. In this tutorial, we will learn to use break and continue
statements with the help of examples.

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.

How break statement works?

Example 1: break statement

 // Program to calculate the sum of numbers (10 numbers max)

 // If the user enters a negative number, the loop terminates

 #include <stdio.h>

 int main() {

 int i;
 double number, sum = 0.0;

 for (i = 1; i <= 10; ++i) {

 printf("Enter a n%d: ", i);

 scanf("%lf", &number);

 // if the user enters a negative number, break the loop

 if (number < 0.0) {

 break;

 }

 sum += number; // sum = sum + number;

 }

 printf("Sum = %.2lf", sum);

 return 0;

 }

Output

 Enter a n1: 2.4

 Enter a n2: 4.5

 Enter a n3: 3.4

 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;

The continue statement is almost always used with the if...else statement.

How continue statement works?

Example 2: continue statement

// Program to calculate the sum of numbers (10 numbers max)

// If the user enters a negative number, it's not added to the result

#include <stdio.h>

int main() {

int i;

double number, sum = 0.0;

for (i = 1; i <= 10; ++i) {

printf("Enter a n%d: ", i);


scanf("%lf", &number);

if (number < 0.0) {

continue;

sum += number; // sum = sum + number;

printf("Sum = %.2lf", sum);

return 0;

Output

Enter a n1: 1.1

Enter a n2: 2.2

Enter a n3: 5.5

Enter a n4: 4.4

Enter a n5: -3.4

Enter a n6: -45.5

Enter a n7: 34.5

Enter a n8: -4.2

Enter a n9: -1000

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.

You might also like