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

C Programming Decision Statements

Uploaded by

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

C Programming Decision Statements

Uploaded by

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

C Programming if, if..

else and Nested


if...else Statement
The if, if...else and nested if...else statement are used to make one-time decisions in C
Programming, that is, to execute some code/s and ignore some code/s depending upon the test
expression.

C if Statement
if (test expression) {
statement/s to be executed if test expression is true;
}
The if statement checks whether the text expression inside parenthesis ( ) is true or not. If the test
expression is true, statement/s inside the body of if statement is executed but if test is false,
statement/s inside body of if is ignored.

Flowchart of if statement
Example 1: C if statement
Write a C program to print the number entered by user only if the number entered is negative.

#include <stdio.h>
int main(){
int num;
printf("Enter a number to check.\n");
scanf("%d",&num);
if(num<0) { /* checking whether number is less than 0 or not. */
printf("Number = %d\n",num);
}
/*If test condition is true, statement above will be executed, otherwise it will not be executed */
printf("The if statement in C programming is easy.");
return 0;
}
Output 1
Enter a number to check.
-2
Number = -2
The if statement in C programming is easy.
When user enters -2 then, the test expression (num<0) becomes true. Hence, Number = -2 is
displayed in the screen.
Output 2
Enter a number to check.
5
The if statement in C programming is easy.
When the user enters 5 then, the test expression (num<0) becomes false. So, the statement/s
inside body of if is skipped and only the statement below it is executed.

C if...else statement
The if...else statement is used if the programmer wants to execute some statement/s when the test
expression is true and execute some other statement/s if the test expression is false.

Syntax of if...else
if (test expression) {
statements to be executed if test expression is true;
}
else {
statements to be executed if test expression is false;
}
Flowchart of if...else statement

Example 2: C if...else statement


Write a C program to check whether a number entered by user is even or odd
#include <stdio.h>
int main(){
int num;
printf("Enter a number you want to check.\n");
scanf("%d",&num);
if((num%2)==0) //checking whether remainder is 0 or not.
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
Output 1
Enter a number you want to check.
25
25 is odd.
Output 2
Enter a number you want to check.
2
2 is even.

Nested if...else statement (if...elseif....else Statement)


The nested if...else statement is used when program requires more than one test expression.

Syntax of nested if...else statement


if (test expression1){
statement/s to be executed if test expression1 is true;
}
else if(test expression2) {
statement/s to be executed if test expression1 is false and 2 is true;
}
else if (test expression 3) {
statement/s to be executed if text expression1 and 2 are false and 3 is true;
}
.
.
.
else {
statements to be executed if all test expressions are false;
}
How nested if...else works?
The nested if...else statement has more than one test expression. If the first test expression is true, it
executes the code inside the braces{ } just below it. But if the first test expression is false, it checks
the second test expression. If the second test expression is true, it executes the statement/s inside
the braces{ } just below it. This process continues. If all the test expression are false, code/s
inside else is executed and the control of program jumps below the nestedif...else
The ANSI standard specifies that 15 levels of nesting may be continued.

Example 3: C nested if else statement


Write a C program to relate two integers entered by user using = or > or < sign.
#include <stdio.h>
int main(){
int numb1, numb2;
printf("Enter two integers to check\n");
scanf("%d %d",&numb1,&numb2);
if(numb1==numb2) //checking whether two integers are equal.
printf("Result: %d = %d",numb1,numb2);
else
if(numb1>numb2) //checking whether numb1 is greater than numb2.
printf("Result: %d > %d",numb1,numb2);
else
printf("Result: %d > %d",numb2,numb1);
return 0;
}
Output 1
Enter two integers to check.
5
3
Result: 5 > 3
Output 2
Enter two integers to check.
-4
-4
Result: -4 = -4

C Programming break and continue


Statement
There are two statements built in C programming, break; and continue; to alter the normal flow of a
program. Loops perform a set of repetitive task until text expression becomes false but it is
sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without
checking the test expression. In such cases, break and continue statements are used.
The break;statement is also used in switch statement to exit switch statement.

break Statement
In C programming, break is used in terminating the loop immediately after it is encountered. The
break statement is used with conditional if statement.

Syntax of break statement


break;
The break statement can be used in terminating all three loops for, while and do...while loops.

Example of break statement


Write a C program to find average of maximum of n positive numbers entered by user. But, if
the input is negative, display the average(excluding the average of negative input) and end
the program.

/* C program to demonstrate the working of break statement by terminating a loop, if user inputs
negative number*/
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
Output
Maximum no. of inputs
4
Enter n1: 1.5
Enter n2: 12.5
Enter n3: 7.2
Enter n4: -1
Average=7.07
In this program, when the user inputs number less than zero, the loop is terminated using break
statement with executing the statement below it i.e., without executing sum=sum+num.
In C, break statements are also used in switch...case statement.

continue Statement
It is sometimes desirable to skip some statements inside the loop. In such cases, continue
statements are used.

Syntax of continue Statement


continue;
Just like break, continue is also used with conditional if statement.

Example of continue statement


Write a C program to find the product of 4 integers entered by a user. If user enters 0 skip it.

//program to demonstrate the working of continue statement in C programming


# include <stdio.h>
int main(){
int i,num,product;
for(i=1,product=1;i<=4;++i){
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)
continue; / *In this program, when num equals to zero, it skips the statement product*=num
and continue the loop. */
product*=num;
}
printf("product=%d",product);
return 0;
}

Output
Enter num1:3
Enter num2:0
Enter num3:-5
Enter num4:2
product=-30

C Programming switch Statement


Decision making are needed when, the program encounters the situation to choose a particular
statement among many statements. If a programmer has to choose one block of statement among
many alternatives, nested if...else can be used but, this makes programming logic complex. This
type of problem can be handled in C programming using switch statement.

Syntax of switch...case
switch (n) {
case constant1:
code/s to be executed if n equals to constant1;
break;
case constant2:
code/s to be executed if n equals to constant2;
break;
.
.
.
default:
code/s to be executed if n doesn't match to any cases;
}
The value of n is either an integer or a character in above syntax. If the value of n matches constant
in case, the relevant codes are executed and control moves out of the switch statement. If
the n doesn't matches any of the constant in case, then the default codes are executed and control
moves out of switch statement.

Example of switch...case statement


Write a program that asks user an arithmetic operator('+','-','*' or '/') and two operands and
perform the corresponding calculation on the operands.

/* C program to demonstrate the working of switch...case statement */


/* C Program to create a simple calculator for addition, subtraction,
multiplication and division */

# include <stdio.h>
int main() {
char o;
float num1,num2;
printf("Select an operator either + or - or * or / \n");
scanf("%c",&o);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(o) {
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
Output
Enter operator either + or - or * or /
*
Enter two operands: 2.3
4.5
2.3 * 4.5 = 10.3
The break statement at the end of each case cause switch statement to exit. If break statement is not
used, all statements below that case statement are also executed.

Source Code to Make Simple Calculator in C


programming
/* Source code to create a simple calculator for addition, subtraction, multiplication and division using
switch...case statement in C programming. */

# include <stdio.h>
int main()
{
char o;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&o);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(o) {
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
Output
Enter operator either + or - or * or divide : -
Enter two operands: 3.4
8.4
3.4 - 8.4 = -5.0
Explanation
This program takes an operator and two operands from user. The operator is stored in variable
operator and two operands are stored in num1 and num2 respectively. Then, switch...case statement
is used for checking the operator entered by user. If user enters + then, statements for case: '+'is
executed and program is terminated. If user enters - then, statements for case: '-' is executed and
program is terminated. This program works similarly for * and / operator. But, if the operator doesn't
matches any of the four character [ +, -, * and / ], default statement is executed which displays error
message.

C Programming goto Statement


n C programming, goto statement is used for altering the normal sequence of program execution by
transferring control to some other part of the program.

Syntax of goto statement


goto label;
.............
.............
.............
label:
statement;
In this syntax, label is an identifier. When, the control of program reaches to goto statement, the
control of the program will jump to the label: and executes the code below it.
Example of goto statement
/* C program to demonstrate the working of goto statement. */
/* This program calculates the average of numbers entered by user. */
/* If user enters negative number, it ignores that number and
calculates the average of number entered before it.*/

# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs: ");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
goto jump; /* control of the program moves to label jump */
sum=sum+num;
}
jump:
average=sum/(i-1);
printf("Average: %.2f",average);
return 0;
}
Output
Maximum no. of inputs: 4
Enter n1: 1.5
Enter n2: 12.5
Enter n3: 7.2
Enter n4: -1
Average: 7.07
Though goto statement is included in ANSI standard of C, use of goto statement should be reduced
as much as possible in a program.

Reasons to avoid goto statement


Though, using goto statement give power to jump to any part of program, using goto statement
makes the logic of the program complex and tangled. In modern programming, goto statement is
considered a harmful construct and a bad programming practice.

The goto statement can be replaced in most of C program with the use of break and continue
statements. In fact, any program in C programming can be perfectly written without the use of goto
statement. All programmer should try to avoid goto statement as possible as they can.

You might also like