Decision-making is very important in any king of programming, It would not be possible to solve most problems without the ability to alter the sequence of instructions in a program based on the result of comparing data values. you make choices and decisions in C programs. This enables you to write programs that can adopt their actions depending on the input data.
Computer Languages are also able to perform different tasks depending on the circumstances uses the following statements to implement choices and decisions in programs.
This statement allows your program to execute a single statement or a block of statements enclosed between braces, if a given condition is ture.
SYNTAX:
If statement has the general form
If (condition)
{
Block of statements
}
EXAMPLE:
Let us have a look at a simple program that uses if statement
#include
#include
main()
{
int marks;
printf("Enter your marks;");
scanf("%d",&marks);
if (Marks>32)
{
Printf("\nCongratulations");
Printf("\nYou passed");
}
}
When this program is executed, it will prompt you to enter your marks.If you enter marks greater then 32 then program will print:
Congrautlations
You Passed
THE IF-ELSE STATEMENT:
The if statement executes a statement if the condition specified is true. Program execution then continues with the next statement in sequence. It might be that we want to execute a particular statement or block of statements only when the condition is false. It has an extension of the if that allows one course of action to be followed if the condition is true and another to be executed if the condition is false.
SYNTAX:
If has the general form
if (condition)
{
block of statements
}
else
{
block of statements
}
EXAMPLE:
#include
#include
main()
{
int marks;
print("\enter your marks");
scanf("%d",&marks);
if (Marks>32)
{
printf("\nCongratutions");
printf("\nYOu are passed");
}
else
printf("\nYou are failed");
}
THE ELSE-IF STATEMENT:
This statement is very useful in programming when we have more than two choices to be taken based on different conditions. During its execution each condition is checked starting from the first. When a condition that results in true is encountered, The block of statements following it are executed. The else in this statement is optional. If all the conditions are false and there is no else clause then none of the instructions in the else-if statement will e executed and the control will be transferred to the next instruction.
SYNTAX:
The else-if statement has the general from is:
if(condition)
{
block of statements
}
else if(condition)
{
block of statements
}
else if(condition)
block of statements
}
EXAMPLE:
#include
#include
main()
{
int choice;
float x, y;
printf("\nperform arithmetic operations.\n");
printf("\n1. Addition");
printf("\n2. subtraction")
printf("\n3. multiplication");
printf("\n3. division\n");
printf("\nEnter your choice:\n");
scaf("%d",&choice);
printf(\n enter 2 numbers;");
scanf("\n%f %f",&x,&y);
if(choice==1)
printF("\nsum=&6.2f" ,x+y);
else if(choice==2)
printf("\ndifference=%6.2f",x=y);
else if(choice==3)
prinf("\nproduct=%6.2f",x*y);
else if (choice==4)
printf("\nquotient=%6.2f",x/y)
else
printf("\ninvalid Command");
}
THE SWITCH STATEMENT:
Computer Languages are also able to perform different tasks depending on the circumstances uses the following statements to implement choices and decisions in programs.
- The if statement
- The if-else statement
- The else-if statement
- The switch statement
- The Conditional operator
This statement allows your program to execute a single statement or a block of statements enclosed between braces, if a given condition is ture.
SYNTAX:
If statement has the general form
If (condition)
{
Block of statements
}
EXAMPLE:
Let us have a look at a simple program that uses if statement
#include
#include
main()
{
int marks;
printf("Enter your marks;");
scanf("%d",&marks);
if (Marks>32)
{
Printf("\nCongratulations");
Printf("\nYou passed");
}
}
When this program is executed, it will prompt you to enter your marks.If you enter marks greater then 32 then program will print:
Congrautlations
You Passed
THE IF-ELSE STATEMENT:
The if statement executes a statement if the condition specified is true. Program execution then continues with the next statement in sequence. It might be that we want to execute a particular statement or block of statements only when the condition is false. It has an extension of the if that allows one course of action to be followed if the condition is true and another to be executed if the condition is false.
SYNTAX:
If has the general form
if (condition)
{
block of statements
}
else
{
block of statements
}
EXAMPLE:
#include
#include
main()
{
int marks;
print("\enter your marks");
scanf("%d",&marks);
if (Marks>32)
{
printf("\nCongratutions");
printf("\nYOu are passed");
}
else
printf("\nYou are failed");
}
THE ELSE-IF STATEMENT:
This statement is very useful in programming when we have more than two choices to be taken based on different conditions. During its execution each condition is checked starting from the first. When a condition that results in true is encountered, The block of statements following it are executed. The else in this statement is optional. If all the conditions are false and there is no else clause then none of the instructions in the else-if statement will e executed and the control will be transferred to the next instruction.
SYNTAX:
The else-if statement has the general from is:
if(condition)
{
block of statements
}
else if(condition)
{
block of statements
}
else if(condition)
block of statements
}
EXAMPLE:
#include
#include
main()
{
int choice;
float x, y;
printf("\nperform arithmetic operations.\n");
printf("\n1. Addition");
printf("\n2. subtraction")
printf("\n3. multiplication");
printf("\n3. division\n");
printf("\nEnter your choice:\n");
scaf("%d",&choice);
printf(\n enter 2 numbers;");
scanf("\n%f %f",&x,&y);
if(choice==1)
printF("\nsum=&6.2f" ,x+y);
else if(choice==2)
printf("\ndifference=%6.2f",x=y);
else if(choice==3)
prinf("\nproduct=%6.2f",x*y);
else if (choice==4)
printf("\nquotient=%6.2f",x/y)
else
printf("\ninvalid Command");
}
THE SWITCH STATEMENT:
The switch statement enables you to select from multiple choices on a set of fixed values for a given expression. The choices are called cases. The selection between a numbers of cases is determined by the value of an integer expression that you specify between brackets following the keyword switch. The switch statement is very similar to the else-if statement but it has more flexibility and it is easy to use. The following simple program demonstrates the use of this statement.
#include
#include
main()
{
int n;
printf("\n enter an interger, N:");
scanf("%d",&n);
switch(n)
{
case1:
printf("N is 1");
break;
case2:
printf("N is 2");
break;
default:
printf("N is not 1 or 2");
}
}
THE CONDITIONAL OPERATOR:
The conditional operator is sometimes called the ternary operator because it involves three operands and is the only operator to do so. it has similarities to the if-else statement because it selects one of the two choices depending on the value of a condition. However, where the if-else statement provides a way of selecting one of the two statements to be executed, the conditional operator is a way to choose between two values.
A conditional operator is a decision-making operator. It has the following form:
condition ? expression1 : expression2;
#include
#include
main()
{
int n;
printf("\n enter an interger, N:");
scanf("%d",&n);
switch(n)
{
case1:
printf("N is 1");
break;
case2:
printf("N is 2");
break;
default:
printf("N is not 1 or 2");
}
}
THE CONDITIONAL OPERATOR:
The conditional operator is sometimes called the ternary operator because it involves three operands and is the only operator to do so. it has similarities to the if-else statement because it selects one of the two choices depending on the value of a condition. However, where the if-else statement provides a way of selecting one of the two statements to be executed, the conditional operator is a way to choose between two values.
A conditional operator is a decision-making operator. It has the following form:
condition ? expression1 : expression2;
Post a Comment