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

Decision Making Statements

Uploaded by

pethu.tndalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Decision Making Statements

Uploaded by

pethu.tndalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Decision Making statements

 C conditional statements allows to make a decision,


based upon the result of a condition. These statements
are called Decision Making
Statements or Conditional Statements.
 Decision making statements contain conditions that
are evaluated by the program. If the condition is true,
then a set of statements are executed and if the
condition is false then another set of statements is
executed.
Decision Making statements
If statements
If else statements
If Else if Statements
else if Ladder
Simple If
 Syntax;  The statement inside the if block
if(condition) are executed only when
{ condition is true, otherwise not.
statement;  If we want to execute only one
}
statement when condition is
true, then braces ({}) can be
removed.
FLOWCHART SYMBOLS
Flowchart Definition: A flow
chart is a graphical
representation of algorithms,
workflow or process.
Program using Simple if
#include<stdio.h>
#include<conio.h>
Void main()
{
int a;
printf(“\n enter a number”);
scanf(“%d”,&a);
clrscr();
if(a%2!=0)
{
printf(“\n the number is odd”);
}
getch();
}
if else statement
 If the test expression is
 Syntax; evaluated to true
if(condition)  statements inside the if are
{
executed.
 if block statements;  statements inside the else are
}
skipped from execution.
else
{
Else block statemnts  If the test expression is
} evaluated to false,
 statements inside the
else block are executed
 statements inside the if are
skipped from execution.
Example program- if -else
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Nested if -else
if-else statement if(test condition)
within either if block {
Statement-X;
or the else block .
Syntax }
if(test condition) else
{ {
if (test condition) if (test condition)
{ {
Statements-x; Statements-x;
} }
else else
{ {
Statment-y; Statment-y;
} }
} }
Example program : To find the greatest number
among
three numbers
#include <stdio.h>
void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
Else if ladder
Here, a user can decide among multiple options. The C
if 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 are true, then the final else statement will
be executed.
Syntax
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
}
Example : Program to print the grade of a student.
#include<stdio.h>
void main( )
{
int mark=70;
if (mark >= 90) /* condi-exp1 */
printf("Passed: Grade A\n"); /* statement1
*/
else if (mark < 90 && mark >= 80) /* condi-exp2 */
printf("Passed: Grade B\n"); /* statement2 */
else if (mark < 80 && mark >= 45)/* condi-exp3 */
printf("Passed: Grade C\n");/* statement3 */
else
printf("Failed\n"); /* statement4 */
}
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 possibles
values of a single variable called switch variable.
switch(expression){
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......
default:
Rules for switch Statement
Rules for switch statement in C language
1) The switch expression must be of an integer
or character type.
2) The case value must be an integer or
character constant.
3) The case value can be used only inside the
switch statement.
4) 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.
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
} return 0; }
Looping Statements
Looping Statements in C execute the sequence of
statements many times until the stated condition
becomes false.
Types of Loops in C
Depending upon the position of a control statement
in a program, looping statement in C is classified
into two types:
1. Entry controlled loop
2. Exit controlled loop
In an entry control loop in C, a condition is checked
before executing the body of a loop. It is also called
as a pre-checking loop.
In an exit controlled loop, a condition is checked
after executing the body of a loop. It is also called as
while loop
It is an entry-controlled loop. In while loop, a
condition is evaluated before processing a body of
the loop. If a condition is true then and only then
the body of a loop is executed. After the body of a
loop is executed then control again goes back at
the beginning, and the condition is checked if it is
true, the same process is executed until the
condition becomes false. Once the condition
becomes false, the control goes out of the loop.
syntax
while (test condition)
{
statements;
}
Example program to print the first ten positive
integers
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with
condition
{
printf("%d\n",num);
num++; //incrementing
operation
}
Do-While loop in C
A do…while loop in C is similar to the while loop
except that the condition is always executed after
the body of a loop. It is also called an exit-
controlled loop.
do {
statements
} while (expression);
In the do-while loop, the body of a loop is always
executed at least once. After the body is executed,
then it checks the condition. If the condition is
true, then it will again execute the body of a loop
otherwise control is transferred out of the loop.
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",num);
num++; //incrementing
operation
}while(num<=10);
return 0;
}
for loop
A for loop is a repetition control structure
that allows you to efficiently write a loop that
needs to execute a specific number of times.
The syntax of a for loop in C programming
language is −

for ( initialization; testcondition; increment


/decrement) {
statement(s);
}
for loop
The initialization step is executed first, and only
once. This step allows to declare and initialize
any loop control variables.
Next, the condition is evaluated. If it is true, the
body of the loop is executed. If it is false, the body
of the loop does not execute and the flow of
control jumps to the next statement just after the
'for' loop.
After the body of the 'for' loop executes, the flow
of control jumps back up to the
increment/decrement statement. This statement
allows to update any loop control variables.
#include <stdio.h>
int main () {
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}
return 0;
}
Jumping Statements
Jump Statement makes the control jump to
another section of the program
unconditionally when encountered. It is
usually used to terminate the loop or switch-
case instantly. It is also used to escape the
execution of a section of the program.
Break
Continue
Goto
Return
break statement
A break statement is used to terminate the
execution of the rest of the block where it is
present and takes the control out of the block
to the next statement.
It is mostly used in loops and switch-case to
bypass the rest of the statement and take the
control to the end of the loop.
The break statement when used in nested
loops only terminates the inner loop where it
is used and not any of the outer loops.
#include <stdio.h>

int main() {
int i;
for (i = 1; i <= 15; i++) {
printf("%d\n", i);
if (i == 10)
break;
}
return 0;
}
continue
The continue jump statement like any other
jump statement interrupts or changes the
flow of control during the execution of a
program. Continue is mostly used in loops.
Rather than terminating the loop it stops the
execution of the statements underneath and
takes control to the next iteration.
Similar to a break statement, in the case of a
nested loop, the continue passes the control
to the next iteration of the inner loop where it
is present and not to any of the outer loops.
#include <stdio.h>
int main()
{
int i, j;
for (j = 1; j < 5; j++) {
if (j == 2)
break;
printf("%d\n", j);
}
return 0;
}
goto statements
goto jump statement is used to transfer the
flow of control to any part of the program
desired. The programmer needs to specify a
label or identifier with the goto statement in
the following manner:

goto label;

This label indicates the location in the


program where the control jumps to.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i < 5; i++) {
if (i == 10)
goto there;
printf("%d\n", i);
}
there:
printf("three");
return 0;
}
return
Return jump statement is usually used at the
end of a function to end or terminate it with or
without a value. It takes the control from the
calling function back to the main function(main
function itself can also have a return).
The return can only be used in functions that is
declared with a return type such as int, float,
double, char, etc.
#include <stdio.h>
int main()
{
int ascii;
char ch;
printf("Enter any ascii value in decimal: \n");
scanf("%d", & ascii);
printf("The character is : %c", ascii);
return 0;
}

You might also like