Decision Making
Decision Making
(Decision Making)
Control Statements
Control statements in C are used to write powerful programs by;
Continue statement
Goto statement
SELECTION STATEMENT
True
Test Condition
Executable X - Statement
Selection Statement
Properties of an if statement
void main()
{
int a, b;
clrscr();
if (a > b)
{
printf(A is Big);
}
getch();
}
The if else statement
It is used to execute some statements when the condition is true and execute some other
statements when the condition is false depending on the logical test.
Syntax:
if ( condition )
{
statement 1 ; (if the condition is true this statement will be executed)
}
else
{
statement 2 ; (if the condition is false this statement will be executed)
}
False True
Test Condition
getch(); getch();
} }
// Biggest of Three Numbers
Nested if.. else statement
#include<stdio.h>
when a series of ifelse statements are occurred in void main()
{
a program, we can write an entire ifelse statement int a, b, c;
in another ifelse statement called nesting clrscr();
FALSE TRUE
Test Condition_2
FALSE TRUE
Test Condition_3
Syntax:
if (condition_1)
executed statement_1;
else if (condition_2)
executed statement_2;
else if (condition_3)
executed statement_3;
----------------------
----------------------
else if (condition_n)
executed statement_n;
else
executed statement_x;
FALSE TRUE
Test Condition_1 Exec. Stat_1
FALSE TRUE
Test Condition_2 Exec. Stat_2
FALSE TRUE
Test Condition_3 Exec. Stat_3
FALSE TRUE
Test Condition_n
switch(choice) switch(choice)
{ {
case 1: case 1:
Printf(Sum is %d\n, num1+num2); printf(Sum is %d\n, num1+num2);
break;
case 2: case 2:
Printf(Diif. is %d\n, num1-num2); printf(Diif. is %d\n, num1-num2);
break;
case 3: case 3:
Printf(Product is %d\n, num1*num2); printf(Product is %d\n, num1*num2);
break;
case 4: case 4:
Printf(Division is %d\n, num1/num2); printf(Division is %d\n, num1/num2);
break;
default: default:
printf (Invalid Choice..\n); printf (Invalid Choice..\n);
} }
getch(); getch();
} }
Rules for Switch
The expression in the switch statement must be an integer or character constant.
No real numbers are used in an expression.
The default is optional and can be placed anywhere, but usually placed at end.
The case keyword must be terminated with colon (:);
No two case constant are identical.
The values of switch expression is compared with case constant in the order specified i.e
from top to bottom.
The compound statements are no need to enclose within pair of braces.
Integer Expression used in different case statements can be specified in any order.
A switch may occur within another switch, but it is rarely done. Such statements are called
as nested switch statements.
The switch statement is very useful while writing menu driven programs.
Initialize
False
Test Condition
True Stop
Body of Loop
Increment or Decrement
Example:
// Summation of the series 1 + 2 + 3 + 4 + .
// Print the I Values
#include <stdio.h>
#include <stdio.h>
void main()
void main()
{
{
int i, sum;
int i;
clrscr();
clrscr();
i = 1;
i = 0;
sum = 0;
while(i<=10)
while(i<=10)
{
{
printf(The I Value is :%d\n,i);
sum = sum + i
++I;
printf(The Sum Value is:%d\n,i);
}
++I;
getch();
}
}
getch();
}
Example: //Summation of the series 11 + 22 + 33 + ..
//Summation of the series 12 + 22 + 32 + ..
#include <stdio.h>
#include <stdio.h>
#include<math.h>
#include<math.h>
void main()
void main()
{
{
int i, sum;
int i, sum;
clrscr();
clrscr();
i = 1;
i = 1;
sum = 0;
sum = 0;
while(i<=10)
while(i<=10)
{
{
sum = sum + pow(i,i)
sum = sum + i*i; //or I ^2 or pow(i, 2)
printf(The Sum Value is:%d\n,i);
printf(The Sum Value is:%d\n,i);
++I;
++I;
}
}
getch(); }
getch(); }
Wap to print the summation of digits of any given number.
#include<stdio.h>
void main()
{
int number=0, rem=0, sum=0;
clrscr();
while(number > 0)
{
rem = number % 10;
sum = sum + rem;
number = number / 10;
}
Initialize
Body of Loop
Increment or Decrement
True
Test Condition
False
Stop
Difference Between While Loop and Do While Loop
1. The while loop tests the condition before The do while loop tests the condition after
each iteration. the first iteration.
2. If the condition fails initially the loop is Even if the condition fails initially the loop is
Skipped entirely even in the first iteration. executed once.
Example:
// Print the I Values // Print the I Values
#include <stdio.h> #include <stdio.h>
void main() void main()
{ {
int i; int i;
clrscr(); clrscr();
i = 0; i = 11;
do do
{ {
printf(The I Value is :%d\n,i); printf(The I Value is :%d\n,i);
++I; ++I;
} }
while(i<=10); while(i<=10);
getch(); getch();
} }
Wap to print the Fibonacci series for any given number Using Do.While Loop
#include <stdio.h>
void main()
{
int i, f1,f2,f3;
clrscr();
f1 = 0;
f2 = 1;
printf(The Fibonacci Series is:\n)
printf(%d\n,f1);
printf(%d\n,f2);
do
{
f3 = f1 + f2;
printf(%d\n,f3);
f1 = f2;
f2 = f3;
++i;
}
while(i <= 10);
getch();
}
for Loop
The for loop is another repetitive control structure, and is used to
execute set of instruction repeatedly until the condition becomes
false.
To set up an initial condition and then modify some value to perform
each succeeding loop as long as some condition is true.
Body of Loop
Stop
Example Example
Given example will print Given example of Multiplication Table
the values from 1 to 10. #include<stdio.h>
#include<conio.h>
void main()
#include<stdio.h>
{
void main() int mul,limit,c,i;
{ clrscr();
for (int i = 1; i <= 10; i++)
printf("Enter the Multiplication Number:\n");
printf("i is %d\n", i); scanf("%d",&mul);
} printf("Enter the Limits:\n");
scanf("%d",&limit);
Case 2:
The second feature is that the test condition may have any compound
relation and
the testing need not be limited only to the loop control variable.
sum = 0;
for (i = 1; i < 20 && sum < 100; ++ i)
{
sum = sum + i;
printf(%d %d\n, i, sum);
}
Additional Features of for Loop Conti
Case 3:
It also permissible to use expressions in the assignment statements of initialization
and increments sections.
For Example:
for (x = (m + n) / 2; x > 0; x = x / 2)
Case 4:
Another unique aspect of for loop is that one or more sections can be omitted, if
necessary.
For Example:
m = 5;
for ( ; m ! = 100 ;)
{
printf(%d\n,m);
m = m + 5;
}
Both the initialization and increment sections are omitted in the for statement. The
initialization has been done before the for statement and the control variable is incremented
inside the loop. In such cases, the sections are left blank. However, the semicolons
separating the sections must remain. If the test condition is not present, the for statement
sets up an infinite loop. Such loops can be broken using break or goto statements in the
loop.
Additional Features of for Loop Conti
Case 5:
We can set up time delay loops using the null statement as follows:
for ( j = 1000; j > 0; j = j 1)
1. This is loop is executed 1000 times without producing any output; it simply
causes a
time delay.
2. Notice that the body of the loop contains only a semicolon, known as a null
statement.
Case 6:
for ( j = 1000; j > 0; j = j 1)
This implies that the C compiler will not give an error message if we place a
semicolon by mistake at the end of a for statement. The semicolon will be
considered as a null statement and the program may produce some nonsense.
Nesting of for Loop
The One for statement within another for statement is called Nesting for Loop.
Syntax:
#include<stdio.h> #include<stdio.h>
#include<conio.h>
#include<conio.h> void main()
void main() {
{ int sum = 1,a,b;
int I, j; clrscr();
clrscr();
for (a=1;a<=5;a++)
for (i = 1; I < = 10 ; I ++) {
{ printf ("the multiplication table for %d\n",a);
printf (The I Value is %d \n", i); for (b=1;b<=12;b++)
{
for (j = 1; j < = 10; j ++) sum=a*b;
{ printf("%d*%d=",a,b);
printf (The J Value is %d \n", j); printf("%d\n",sum);
} }
} sum = 0;
getch(); }
getch();
} }
Exercise
1) Write a program that will read in N numbers and print out their average.
2) Wap to print the following series for N given number
1+(1+2)+(1+2+3)+(1+2+3+4)
3) Wap to print the following series for N given number
+
++
+++
++++
+++++
++++++ ..
break;
3. The break statement does not have any embedded expression or arguments.
4. The break statement is usually used at the end of each case and before the start of the next
case statement.
5. The break statement causes the control to transfer out of the entire switch statement.
#include<stdio.h>
#include <stdio.h> void main ()
{
void main() int num1,num2,choice;
printf (Enter the Two Numbers:\n);
{
scanf(%d%d,&num1,&num2);
int i; printf(1 -> Addition\n);
printf(2->Subtraction\n);
clrscr(); printf(3->Multiplication\n);
printf(4->Division\n);
i = 1; printf (Enter your Choice:\n);
while (i < = 10) scanf (%d, &choice);
{ switch (choice)
{
printf (The I Value is: %d \n, i); case 1:
printf (Sum is %d \n, num1+num2);
if (i = = 6)
break;
{ case 2:
printf (Diif. is %d \n, num1-num2);
printf (The I value is Reached 6, break;
case 3:
So break of the programs\n); printf (Product is %d \n, num1*num2);
break; break;
case 4:
} printf (Division is %d \n, num1/num2);
break;
++ i default:
printf (Invalid Choice..\n);
}
}
} getch();
}
The continue Statement
The continue statement is used to transfer the control to the beginning of the loop, there
by terminating the current iteration of the loop and starting again from the next iteration
of the same loop.
The continue statement can be used within a while or a do while or a for loop.
The general form or the syntax of the continue statement is
continue;
The continue statement does not have any expressions or arguments.
Unlike break, the loop does not terminate when a continue statement is encountered, but it
terminates the current iteration of the loop by skipping the remaining part of the loop and
resumes the control tot the start of the loop for the next iteration.
#include <stdio.h>
void main()
{
int i;
clrscr();
i = 1;
while (i < = 10)
{
printf (The I Value is: %d \n, i);
if (i = = 6)
{
printf (The I value is Reached 6, But Continue this Programs\n);
continue;
}
++ i
}
Differences Between Break and Continue Statement
1. Used to terminate the loops or to exit loop Used to transfer the control to the start of
from a switch. loop.
2. The break statement when executed Continue statement when executed causes
causes Immediate termination of the current
immediate termination of loop containing iteration
it. of the loop.
The goto Statement
The goto statement is used to transfer the control in a loop or a function from one point to
any other portion in that program.
If misused the goto statement can make a program impossible to understand.
The general form or the syntax of goto statement is
goto label;
Statement (s);
.
label:
statement (s);