0% found this document useful (0 votes)
5 views

Conditional Statements

Conditional statements in programming languages like C evaluate one or more conditions and determine whether to execute a set of statements or not. The document discusses different conditional statements in C - if, if-else, nested if-else, if-else-ladder, and switch. It provides the syntax for each statement and includes sample programs to find the greatest of two numbers, determine if a number is positive or negative, find the greatest of three numbers using nested if-else, and use an if-else-ladder and switch statements to evaluate user input.

Uploaded by

Bhanesh Katari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Conditional Statements

Conditional statements in programming languages like C evaluate one or more conditions and determine whether to execute a set of statements or not. The document discusses different conditional statements in C - if, if-else, nested if-else, if-else-ladder, and switch. It provides the syntax for each statement and includes sample programs to find the greatest of two numbers, determine if a number is positive or negative, find the greatest of three numbers using nested if-else, and use an if-else-ladder and switch statements to evaluate user input.

Uploaded by

Bhanesh Katari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Conditional Statements

• The conditional statements (also known as decision control structures)


are used to evaluate one or more conditions and make the decision
whether to execute a set of statements or not. These decision-making
statements in programming languages decide the direction of the flow
of program execution.

• Following are the decision-making statements available in C


if Statement
if-else Statement
Nested if-else
if-else-if Ladder
switch Statement
If statement
• The syntax of the if statement in C programming is:

if (test expression)
{
// code or statements
}

• 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 it is false, statements inside the body of if are not
executed.
Program 1
• Write a program to find out the greatest among two given numbers
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter number 1: ");
scanf("%d", &num1);
printf("Enter number 2: ");
scanf("%d", &num2);
if (num1>num2)
{
printf(" %d is greater than %d", num1, num2);
}
if (num2>num1)
{
printf(" %d is greater than %d", num2, num1);
}
return 0;
}
If-else statement
• 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
}
Program 2
• Write a program to find out the given number is positive or negative
using if-else.
#include <stdio.h>

int main()
{
int num1;
printf("Enter a non zero number : ");
scanf("%d", &num1);
if (num1>0)
{
printf(" %d is a positive number ", num1);
}
else
{
printf(" %d is a negative number ", num1);
}
return 0;
}
Nested If-else
• A nested if in C is an if statement that is the target of another if
statement. Nested if statements mean an if statement inside another
if statement. The syntax of the nested if..else statement is:

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
Program 3
• Write a program to find out the biggest among three given numbers
using nested if-else.
#include <stdio.h>

int main() Program 3


{
• Write a intprogram to find
num1, num2, out the biggest among three
num3; given numbers
printf("Enter three numbers: ");
using nested if-else.
scanf("%d %d %d", &num1, &num2, &num3);
if (num1>num2 && num1>num3)
{
printf(" %d is the greatest number ", num1);

}
else
{
if (num2>num1 && num2>num3)
{
printf(" %d is the greatest number ", num2);
}
else
{
printf(" %d is a negative number ", num3);
}
}
return 0;
}
If-elseif ladder
• In If elseif ladder the statements are executed from the top down. As
soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the C else-if ladder
is bypassed. If none of the conditions is true, then the final else
statement will be executed. if-else-if ladder is similar to the switch
statement.
if (condition)
{
statement;
else if (condition)
statement;
.
.

else
statement;
Program 4
• Write a program to display ONE if user enters 1, display TWO if user
enters 2 , display THREE if the input is 3 and for any other input display
invalid input using if-elif ladder.
#include <stdio.h>

int main() Program 4


{
int num1;
• Write a printf("Enter
program to adisplay
number ONE
: "); if user enters 1, display TWO if user
enters 2scanf("%d",
, display THREE if the input is 3 and for any other input display
&num1);
if (num1==1)
invalid input
{ using if-elif ladder.
printf(" The entered number is ONE ");

}
else if(num1==2)
{
printf(" The entered number is TWO ");
}
else if(num1==3)
{
printf(" The entered number is THREE ");
}
else
{
printf(" The entered number is INVALID ");
}
return 0;
Program 5
• Display the grade of a student based on the marks. The grades are
given as follows
Marks >=90 - S grade
Marks 80 to 89 - A grade
Marks 70 to 79 - B grade
Marks 60 to 69 - C grade
Marks 55 to 59 - D grade
Marks 50 to 54 - E grade
Marks < 50 - F grade
Program 6
• Write a program to print the given THREE numbers in descending order.
Switch statement
• The switch statement in C is an alternate to if-else-if ladder statement
which allows us to execute multiple operations for the different
possible values of a single variable called switch variable. Here, We can
define various statements in the multiple cases for the different values
of a single variable.

• 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.
Switch statement
• The syntax is
switch (expression)
​{
case constant1:
// statements
break;

case constant2:
// statements
break;
.
.
.
default:
// default statements
}
Rules for Switch statement
• The switch expression must be of an integer or character type.

• The case value must be an integer or character constant.

• The case value can be used only inside the switch statement.

• The break statement in switch case is not must. It is optional. If there is


no break statement found in the case, all the cases will be executed
present after the matched case. It is known as fall through the state of
C switch statement.
Program 7
• Write a program to create a simple calculator for +, - , * and /
operations based on the user input.
#include <stdio.h>
int main() {
char operation; case '*':
double n1, n2; printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
printf("Enter an operator (+, -, *, /): "); break;
scanf("%c", &operation);
printf("Enter two operands: "); case '/':
scanf("%lf %lf",&n1, &n2); printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
switch(operation) break;
{
case '+': // operator doesn't match any case constant +,
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2); -, *, /
break; default:
printf("Error! operator is not correct");
case '-': }
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break; return 0;
}

You might also like