0% found this document useful (0 votes)
8 views95 pages

C Programming: Conditional & Iterative Statements

Uploaded by

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

C Programming: Conditional & Iterative Statements

Uploaded by

Sumeet Gill
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPUTER

PROGRAMMING

Conditional and Iterative Statements


COMPUTER
PROGRAMMING

Conditional
Branching
Statements
Branching

❖ C program may require that a logical test be carried out at


some particular point within the program.

❖ One of several possible actions will then be carried out,


depending on the output of the logical test.

This is known as Branching.


Branching

❖ There is also special kind of branching, called Selection,


in which one group of statements is selected from several
available groups.
DECISION MAKING

❖ Decision-making structures require that the programmer


specifies 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.
Branching Statements

All of these operations can be carried out using various control statements
included in C
C programming language provides the following types of decision-making
statements.
COMPUTER
PROGRAMMING

Conditional
if Statement
Statements
if Statements
if Statements
If..else Statements
….example

main()
{
int a=10;
if(a<20)
{
printf("a<20\n");
}
if(a>20)
{
printf("a>20\n");
}
}
….example

main()
{
int a=10;
if(a<20)
{
printf("a<20\n");
}
else
{
printf("a>20\n");
}
}
….example
COMPUTER
PROGRAMMING

Conditional
switch Statement
Statements
switch Statement
switch Statement
switch Statement
switch Statement
switch Statement
switch Statement
switch Statement
switch Statement
switch Statement
….examples

main()
{
int a=10;
switch(a)
{
case 20: printf("a=20\n"); break;
case 10: printf("a=10\n"); break;
default: printf("unknown");
}
}
switch Statement
….examples
switch Statement
….examples
switch Statement
….examples
switch vs if
COMPUTER
PROGRAMMING

Iterative
Looping
Statements
Looping

❖ A program may require that a group of instructions be


executed repeatedly, until the logical condition becomes
true.
This is known as Looping.

❖ Sometimes the required number of repetitions is know in


advance; and sometimes the computation continues
indefinitely until the logical condition become true.
Loops
Looping
COMPUTER
PROGRAMMING

Iterative
while loop
Statements
While Loop
While Loop
While Loop
While Loop
While Loop
While Loop
(….examples)
While Loop
(….examples)
While Loop
(….example)
While Loop
(….example)
While Loop
Factorial using while
#include<stdio.h>
int main()
{
int i=1,n,f=1;
printf("\n enter value of n=");
scanf("%d", &n);

while(i<=n)
{
f = f*i;
i++;
}
printf("Factorial of %d = %d",n, f);
}
COMPUTER
PROGRAMMING

Iterative
for loop
Statements
for Loop
for Loop
for Loop
for Loop
for Loop
for Loop
for Loop
Factorial using for loop
main()
{
int i,n=5,f=1;
for(i=1;i<=n;i++)
{
f = f*i;
}
printf(“Factorial is=%d", f);
}
COMPUTER
PROGRAMMING

Iterative
do-while looping
Statements
do-while Loop
do-while Loop
While vs do-while
do-while Loop
do-while Loop
do-while Loop
Factorial using do-while

main()
{
int i=1,n=5,f=1;
do{
f = f*i;
i++;
} while(i<=n);
printf(“Factorial is =%d",f);
}
Nested Loops
COMPUTER
PROGRAMMING

Iterative
Nested Loops
Statements
Nested Loops
Nested Loops
Nested Loops
Example
#include <stdio.h>
int main()
{
int i=1,j;
while (i <= 5)
{
j=1;
while (j <= i )
{
printf("%d ",j);
j++;
}
printf("\n");
i++;
}
return 0;
}
Nested Loops
#include <stdio.h>
Example
int main()
{
int i=1,j;
do
{
j=1;
do
{
printf("*");
j++;
}while(j <= i);
i++;
printf("\n");
}while(i <= 5);
return 0;
}
Nested Loops

main(){
int i,j,n=6;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
Nested Loops
Practice

Write Programs for printing bellow output


COMPUTER
PROGRAMMING

Loop Control break and continue


Statements Statements
Loop Control Statements
break Statement
break Statement

❖ In any loop break is used to jump out of loop skipping the


code below it without caring about the test condition.

❖ It interrupts the flow of the program by breaking the loop and


continues the execution of code which is outside the loop.

❖ The common use of break statement is in switch case where it


is used to skip remaining part of the code.
break Statement
break Statement
break Statement
Example: C program to take input from the user until user enters zero.

#include <stdio.h>
int main ()
{
int a;
while (1)
{
printf("enter the number:");
scanf("%d", &a);
if ( a == 0 )
break;
}
return 0;
}
break Statement
break Statement
break Statement
Example: C program to print the day of the week according to the
number of days entered
continue Statement
continue Statement

❖ Like a break statement, continue statement is also used with


if condition inside the loop to alter the flow of control.

❖ When used in while, for or do...while loop, it skips the


remaining statements in the body of that loop and performs
the next iteration of the loop.

❖ Unlike break statement, continue statement when


encountered doesn’t terminate the loop, rather interrupts a
particular iteration.
continue Statement
continue Statement
continue Statement

#include <stdio.h>
int main ()
{
int a, sum = 0;
for (a = 0; a < 10; a++)
{
if ( a % 2 == 0 )
continue;
sum = sum + a;
}
printf("sum = %d",sum);
return 0; }
continue Statement
continue Statement
COMPUTER
PROGRAMMING

Loop Control goto


Statements statements
goto Statement
goto Statement
….examples

You might also like