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

c Programming-chapter 05

This document provides an overview of decision-making, branching, and looping in C programming. It explains various types of decision-making statements such as 'if', 'if-else', 'switch', and different looping constructs like 'while', 'do-while', and 'for'. Additionally, it discusses jump statements including 'break', 'continue', and 'goto', along with examples and syntax for each concept.

Uploaded by

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

c Programming-chapter 05

This document provides an overview of decision-making, branching, and looping in C programming. It explains various types of decision-making statements such as 'if', 'if-else', 'switch', and different looping constructs like 'while', 'do-while', and 'for'. Additionally, it discusses jump statements including 'break', 'continue', and 'goto', along with examples and syntax for each concept.

Uploaded by

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

WELCOME

PRESENTED BY
SHEYONA G
CHAPTER-05
Decision Making, Branching
and Looping
Decision making with IF statement,
Simple IF statement
 Decision-making statements are used to control the flow of a program by evaluating condition
 Decision-making statements are also known as conditional statements

There are 2 decision making statements in C. They are:

a. If statements – (bidirectional control statement)


b. Switch Case Statements. – (multi directional control statement)
“if” statement and its types. “if “statement can be of the following types:

a. if

b. if…else

c. if…. else if….else

d. Nested if
if statement
Syntax

if (condition)
{
statements ;
}
Next statement

if the condition returns true, then the


statements inside if block will be executed and
then Next statement. If false then the block is
skipped and the Next statement is executed.
void main()
{
int a;
printf(“enter a value”);
scanf(“%d”,&a);
if(a<=10)
{
printf(“Less than 11”);
}
getchar();
}
The IF…else statement

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

if the condition returns true, then the


statements inside if block will be executed,
otherwise the statements inside else block will
be executed
void main()
{
int a;
printf("Enter the number\n");
scanf("%d",&a);
if(a%2==0)
printf("Number is Even\n");
else
printf("Number is Odd\n");
}
if… else if …. Else
if(condition)
{  The first ‘if’ condition will be checked, if
statements ; the condition returns true, the
} statements inside ‘if’ block will be
else if (condition) executed and the rest of the ‘else if’
{ conditions will be skipped from
statements ; checking.
}  if false, the control moves to the next
else if (condition) ‘else if’ condition and follows the same
{ rule.
statements ;  if all the if and else if conditions return
} false, then else block statement will be
…..(any number of else if conditional statements are allowed) executed.
else
{
statements ;
}
void main()
{
int a;
printf(“enter a value”);
scanf(“%d”,&a);
if(a<=5)
{
printf(“Less than 6”);
}
else if (a>5 && a<=10)
{
printf(“In 6 to 10”);
}
else
{
printf(“more than 10”);
}
}
Nested if
void main()
{
printf(“Good time is 2clk”);
int a; }
printf(“enter either 1 or 2”); else
scanf(“%d”,&a); {
if(a==1) printf(“Good Night”);
{ }
printf(“time is 1clk”);
}
}
else }
{
if(a==2)
{
Switch Statement
 switch statement is multi- directional control statement, mainly used for making a particular
choice from the many choices.
 Based on the expression’s output, case will be chosen and the statement inside the
corresponding case will be executed.
 Switch block has three main elements.
They are:
a. Expression
b. Case
c. Break
 Break statement is necessary at the end of each case, in order to move out of the switch
block, as soon as the case is executed.
Syntax:
switch (expression)
{
case 1:
statements;
break;
case 2:
statements;
break;
.
.
.
case n:
statements;
break;
default:
statements;
break;
}
#include<stdio.h>
void main()
{
int choice;
printf("Welcome to NTTF customer support");
printf("\n Our Customer Care Person will contact you soon");
printf("\n Please choose your choice of language 1- English, 2- Tamil, 3- Kannada, 4- Hindi");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Good Day Sir!");
break;
case 2:
printf("Vanakkam Sir!");
break;
case 3:
printf("Namaskara Sir!");
break;
case 4:
printf("Namaste Sir!");
break;
default:
printf("How can we help you Sir!, please make a correct choice!");
break;
}
printf("\n. Visit our website www.nttftrg.com for more details\n");
getch();
}
LOOPS

The WHILE statement, The DO… statement, The FOR statement

 In looping a set of statements are executed until some conditions for termination of the loop
is encountered.
 A program loop therefore consists of two segments one known as body of the loop and other
is the control statement.
 The control statement tests certain conditions and then directs the repeated execution of the
statements contained in the body of the loop
They are two types of loop:

 Entry-controlled loop or pre-test loop


 Exit-controlled loop or post-test loop
Entry-controlled loop:
In entry-controlled loop, the control conditions are tested before the start of the loop execution. If
the conditions are not satisfied, then the body of the loop will not be executed. Entry-controlled
loops are
 while loop
 for loop

Exit-controlled loop:
In exit-controlled loop, the test is performed at end of the body of the loop and therefore the body
of the loop is executed unconditionally for first time. Exit- controlled loop is
 Do...while loop
Entry-controlled loop: Exit-controlled loop:
While Statement:
The simplest of all looping structure in C is the while statement.
The general format of the while statement is:

Initialization;
while (test condition)
{
body of the loop
}
Next statement;
 The given test condition is evaluated and if the condition is true then the body of the loop is
executed.
 After the execution of the body, the test condition is once again evaluated and if it is true, the
body of the loop is executed once again.
 This process of repeated execution of the body continues until the test condition finally
becomes false and the control is transferred out of the loop.
#include<stdio.h> OUTPUT:
void main() 1,2,3,4,5,6,7,8,9,10,…………………………..,100
{
int i=1;
while(i<=100)
{
printf("%d ",i);
i++;
}
}
The Do while statement:
The body of the loop is executed first and then the loop condition is checked , so the body of the loop is
executed at least once.
The syntax of the do while loop is:

Initialization;
do
{
body of the loop;
}
while(test condition);
Next statement;

The body of the loop is executed, and then test condition is evaluated. If the condition expression is true
then the body is executed again and this process continues till the conditional expression becomes false.
When the expression becomes false the loop terminates.
Example:

void main()
{
int i=10 ;
do
{
printf(“%d \t”,i);
i--;
}
while(i>=1);
}

Output:
10 9 8 7 6 5 4 3 2 1
The for Statement:

Syntax:

for (initialization; test condition; increment/decrement)


{
Body of loop
}
Next statement;
 Initialization of the control variable is done first, using assignment operator i=1. The variable ‘i’ is
known as loop-control variable.
 If the test condition is true the body of the loop is executed, otherwise the loop is terminated
 After executing the body of loop the control is transferred back to the increment or decrement part
such as i++ or i-- and the new value of the control variable is again tested.
 If the condition is satisfied the body of the loop is again executed.
void main()
{
int i; Output
for(i=0; i<=5; i++) 012345
{
printf(“\t%d”,i);
}
getchar();
}
JUMP STATEMENTS
break, Continue, return and go to

Break Statement

Break statement is used to jump out of a loop instantly, without waiting to get back to the
conditional test.
The keyword used is break .
When break is encountered inside any loop, control automatically passes to the first statement
after the loop. Break is usually associated with an ‘if’.
Syntax:
break;
void main()
{ Output
int i; 12
for(i=1; i<=5; i++) exiting the loop
{ Thank you
if(i==3)
{
printf(“exiting the loop\t”);
break;
}
}
printf(“\nThank you”);
getchar();
}
Continue Statement
 'continue' statement is used to skip the current iteration of a loop &
move to the next one
 A continue is usually associated with an if.

Syntax:
continue;
void main()
{
int i; Output:
for(i=1; i<=5; i++)
{ 1 2 skipping this part of the loop 4 5
if(i==3)
{
printf(“skipping this part of the loop\t”);
}
continue;
}
printf(“%d\t”,i);
getch();
}
Difference between break and continue
Goto statement:

Goto statement to branch unconditionally from one statement to another in a program.


The goto requires a label in order to identify the place where the branch is to be made.
A label is any valid variable name, and must be followed by colon(:).
The label is placed immediately before the statement where the control is to be transferred.
The keyword for goto statement is “goto”

Syntax:

goto label-name;
There are two types of goto statements. They are,
 Forward jump
 Backward jump
#include<stdio.h>
void main()
{
int a;
printf(“Enter a number\n”);
scanf(“%d”,&a);
if(a>0)
goto aa;
else
goto bb;
aa: printf(“Positive”);
goto cc;
bb:
printf(“Neagtive”);
cc: getch();
}
Assignments

1. What is the use of decision making statements in C language?


2. Write a C program to check whether the number is an even number using if statement.
3. Write the syntax of if-else statement
4. Write a C program to check whether an entered number is divisible by 5.
5. What is loop? What are the different types of loop in C language?
6. Write the syntax of while loop in C language
7. Write the syntax of do-while loop in C language
8. Write a C program to display 1-100 using for loop.
9. Write a C program to display 1-100 using while loop.
10. Write notes on break and continue
The keyword else cannot be used without
if default switch for
___________ keyword
Multiway selection can be accomplished using an else
switch if default for
if statement or the _______statement.
In switch case ____________ keyword is used to exit
break default switch case
from each block.
In switch case statement all the case labels are end
semicolon dot colon }
with a ______.
In switch case the case labels should be always either
integer float double void
character type or ___________ type
Which among the following is not a loop in C
for while do-while switch
language?
Which among the following loop will execute atleast
for do-while while break
one time?
Which among the following is an entry controlled
do-while switch break while
loop?
In ‘for loop’ which of the following statement is executed only
decrement increment testing initialization
one time?

Which amongst the following is used in for loop to separate closing square
commas semicolon
the expressions brace bracket

Which among the following is an exit controlled loop? do-while switch break while

The ___________ statement can be used to skip a part of the


goto break continue else
loop and to continue with the next iteration.

Which amongst the following, keyword is used for transfer the


goto break continue for
control to any place in a program.

You might also like