You are on page 1of 11

Unit-III 2012

Decision making structures


Decision making structures require that the programmer specify one or more conditions to be evaluated or
tested by the program, along with a statement or statements to be executed if the condition is determined
to be true, and optionally, other statements to be executed if the condition is determined to be false.

if Statements
A simple if structure is called a single-selection structure because it either selects or ignores a single action.
It takes an expression in parenthesis and an statement or block of statements. if the expression is true then
the statement or block of statements gets
executed otherwise these statements are
skipped.

Syntax:

if(Expression/Condition)

statement;

if (Expression/Condition)

statement1;

statement2; /* additional code

statements; goes here */

Find largest among two numbers


#include <stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("enter the values of a and b");
scanf("%d %d",&a,&b);
if(a>b)
printf("a is greater");
if(a<b)
printf("b is greater");

Ankur Rastogi Page 1


Unit-III 2012

getch();
}

If-else statement
If-else provides a way to instruct the computer to execute a block of code only if certain conditions have
been met. The syntax of an If-else construct is:

if (/* condition goes here */)


{
/* if the condition is nonzero
(true), this code will execute */
}
else
{
/* if the condition is 0 (false), this code will execute */
}

The first block of code executes if the condition in parentheses directly after the if evaluates to nonzero
(true); otherwise, the second block executes.
The else and following block of code are completely optional. If there is no need to execute code if a
condition is not true, leave it out.

#include <stdio.h>
#include<conio.h>

Ankur Rastogi Page 2


Unit-III 2012

main()
{
int a,b;
printf("enter the values of a and b");
scanf("%d %d",&a,&b);
if(a>b)
printf("a is greater");
else
printf("b is greater");
getch();
}

if..else..if Statement
syntax:
if( expression1 )
statement1;
else if( expression2 )
statement2;
else
statement3;
next_statement;

 If the first expression, expression1, is true, statement1 is executed before the program continues
with the next_statement.
 If the first expression is not true, the second expression, expression2, is checked. If the first
expression is not true, and the second is true, statement2 is executed.
 If both expressions are false, statement3 is executed.
 Only one of the three statements is executed.

Ankur Rastogi Page 3


Unit-III 2012

#include <stdio.h>
#include<math.h>
main()
{
int a,b,c,d,x1,x2;
printf("enter the values of a,b and c");
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("Imaginary %d",d);
}
else if(d==0)
{
x1=x2=-b/2*a;
printf("x1=%d, x2=%d, d=%d",x1,x2,d);
}
else
{
x1=(-b+sqrt(d))/2*a;
x2=(-b-sqrt(d))/2*a;
printf("x1=%d, x2=%d, d=%d",x1,x2,d);
}
}

Nested if Statements
It is a conditional statement which is used when we want to check more than 1 conditions at a time in a
same program. The conditions are executed from top to bottom checking each condition whether it meets
the conditional criteria or not. If it found the condition is true then it executes the block of associated
statements of true part else it goes to next condition to execute.
Syntax:

if(condition)
{
if(condition)
{
statements;
}
else
{
statements;
}
}
else
{

Ankur Rastogi Page 4


Unit-III 2012

statements;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the
braces and again checks the next condition. If it is true then it executes the block of statements associated
with it else executes else part.

#include <stdio.h>
main()
{
int a,b,c;
printf("enter the values of a,b and c");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is greater");
}
else
{
printf("c is greater");
}
}
else
{
if(b>c)
{
printf("b is greater");

Ankur Rastogi Page 5


Unit-III 2012

}
else
{
printf("c is greater");
}
}
}

Switch
Switch case statements are a substitute for long if statements that compare a variable to several "integral"
values ("integral" values are simply values that can be expressed as an integer, such as the value of a char).
The basic format for using switch case is outlined below. The value of the variable given into switch is
compared to the value following each of the cases, and when one value matches the value of the variable,
the computer continues executing the program from that point.

switch ( <variable> ) {
case this-value:
Code to execute if <variable> == this-value
break;
case that-value:
Code to execute if <variable> == that-value
break;
.
.
.
default:
Code to execute if <variable> does not equal the value following any of the cases
break;
}

Ankur Rastogi Page 6


Unit-III 2012

#include <stdio.h>
main()
{
int code;
printf("Enter the code");
scanf("%d",&code);
switch(code)
{
case 1: printf("Red");
break;
case 2: printf("Green");
break;
case 3: printf("Blue");
break;
default: printf("not defined");
}
}

Conditional Operator
(Exp) ? exp1 : exp2

The while Statement


The while statement, also called the while loop, executes a block of statements as long as a specified
condition is true. The while statement has the following form:

while (condition){
statement;

}
false
/*Sum of first n numbers */ Continuation
#include<stdio.h> condition?
#include<conio.h>
void main()
{ true
int n,a=0,i=1;
printf("Enter the number\n"); Statement(s)
scanf("%d",&n);
while(i<=n){
a=a+i;
i++;

Ankur Rastogi Next Page 7


Statement
Unit-III 2012

}
printf("Sum of first %d number is %d",n,a);
getch();
}

The do...while Loop


The do...while loop executes a block of statements as long as a specified condition is true. The do...while
loop tests the condition at the end of the loop rather than at the beginning, as is done by the for loop and
the while loop.

The structure of the do...while loop is as follows:

do{
statement
}while (condition);

When program execution reaches a do...while statement, the following events occur:

1. The statements in statement are executed.


2. condition is evaluated. If it's true, execution returns to step 1. If it's false, the loop terminates.

/*Sum of given n numbers*/


Statement(s)
void main()
{
int n,a=0,sum=0,i=1;
printf("Enter the number\n"); true
Continue
scanf("%d",&n); condition?
do{
printf("enter your number\n");
scanf("%d",&a); false
sum=sum+a;
i++; Next
}while(i<=n); Statement
printf("Sum of given n number is %d",sum);
getch();
}

The for Statement


The for statement is a C programming construct that executes a block of one or more statements a certain
number of times. It is sometimes called the for loop because program execution typically loops through the
statement more than once.

Ankur Rastogi Page 8


Unit-III 2012

Initial-Action
for ( initial; condition; increment ){
statement;
}
false
/* Table generation for n */ Action-After- Continuation
void main() Each-Iteration condition?
{
int n,a=0,i;
true
printf("Enter the number\n");
scanf("%d",&n); Statement(s)
for(i=1;i<=10;i++) (loop-body)
{
a=n*i;
printf("%d\n",a);
} Next
getch(); Statement
}

The break Keyword

false
Continuation
condition?

true

Statement(s)

break

Statement(s)

Next
Statement

 The break statement can be placed only in the body of a for loop, while loop, do...while loop or
switch.
 break is used inside a loop or switch statement. It causes the control of a program to skip past the
end of the current loop (for, while, or do...while) or switch statement. No further iterations of the
loop execute.

Ankur Rastogi Page 9


Unit-III 2012

/*C program for prime number or not*/


#include<stdio.h>
main()
{
int n, c = 2;
printf("Enter a number to check if it is prime\n");
scanf("%d",&n);
for ( c = 2 ; c <= n - 1 ; c++ ) {
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
getch();
}

The continue Keyword

false
Continue
condition?

true

Statement(s)

continue

Statement(s)

Next
Statement

 Like the break statement, the continue statement can be placed only in the body of a for loop, a
while loop, or a do...while loop. When a continue statement executes, the next iteration of the
enclosing loop begins immediately. The statements between the continue statement and the end
of the loop aren't executed.
 continue is used inside a loop. It causes the control of a program to skip the rest of the current
iteration of a loop and start the next iteration.

Ankur Rastogi Page 10


Unit-III 2012

 Example
printf("Printing only the even numbers from 1 to 10\n");
for( x = 1; x <= 10; x++ ) {
if( x % 2 != 0 ) /* See if the number is NOT even */
continue; /* Get next instance x */
printf( "\n%d", x );
}

The goto Statement


 The goto statement is one of C's unconditional jump, or branching, statements. When program
execution reaches a goto statement, execution immediately jumps, or branches, to the location
specified by the goto statement. This statement is unconditional because execution always
branches when a goto statement is encountered; the branch doesn't depend on any program
conditions.
 A goto statement and its target must be in the same function, but they can be in different blocks.

 Syntax-
goto Label ;
Label:
/*Some Code*/

 Syntax-
Label:
/*Some Code*/
goto Label;

The exit() Function


 The exit() function terminates program execution and returns control to the operating system.
 This function takes a single type int argument that is passed back to the operating system to
indicate the program's success or failure.
 The syntax of the exit() function is
exit(status);
 If status has a value of 0, it indicates that the program terminated normally.
 A value of 1 indicates that the program terminated with some sort of error.
 To use the exit() function, a program must include the header file STDLIB.H.
 This header file also defines two symbolic constants for use as arguments to the exit() function:
 #define EXIT_SUCCESS 0
 #define EXIT_FAILURE 1
 Thus, to exit with a return value of 0, call exit(EXIT_SUCCESS).
 for a return value of 1, call exit(EXIT_FAILURE).

Ankur Rastogi Page 11

You might also like