Upload
vinay-arora
View
1.819
Download
4
Embed Size (px)
Citation preview
C Programming - Decision & Loop
Control
Organized By: Vinay Arora
Assistant Professor, CSED
Thapar University, Patiala
Vinay Arora
CSED
Decision Control
� The if statement
� The if-else statement
� The conditional operators
Vinay Arora
CSED
Forms of if
Vinay Arora
CSED
Forms of if (contd.)
Vinay Arora
CSED
Relational Operator
Vinay Arora
CSED
Demonstrating - If
Vinay Arora
CSED
Flowchart
Vinay Arora
CSED
Demonstrating - If
Vinay Arora
CSED
Flowchart
Vinay Arora
CSED
Expression in Conditional Part
Vinay Arora
CSED
Multiple statements within if
Vinay Arora
CSED
Demonstrating If - Else
Vinay Arora
CSED
Flowchart If-Else
Vinay Arora
CSED
Logical Operators with If-Else
Vinay Arora
CSED
Logical Operators with If-Else
Vinay Arora
CSED
Else-if ladder
Vinay Arora
CSED
Logical Operators with If-Else
Vinay Arora
CSED
Smallest amongst 3 nos.
Vinay Arora
CSED
Smallest amongst 3 nos.
Vinay Arora
CSED
Program using Logical OR, elseif
Vinay Arora
CSED
Calculate Salary as per following table
Vinay Arora
CSED
&& - Logical AND
� C allows usage of three logical operators, namely, &&, || and !
� These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively.
� Don’t use the single symbol | and &. These single symbols also have a
meaning.
� The first two operators, && and ||, allow two or more conditions to be
combined in an if statement.
Vinay Arora
CSED
&& - Logical AND (C Program)
Vinay Arora
CSED
Vinay Arora
CSED
Vinay Arora
CSED
| | - Logical OR (C Program)
Vinay Arora
CSED
Vinay Arora
CSED
Vinay Arora
CSED
! - Logical NOT
� This operator reverses the result of the expression it operates on.
� For example, if the expression evaluates to a non-zero value, then
applying ! operator to it results into a 0.
� Vice versa, if the expression evaluates to zero then on applying !
operator to it makes it 1, a non-zero value.
Vinay Arora
CSED
! - Logical NOT (C Program)
Vinay Arora
CSED
Vinay Arora
CSED
Vinay Arora
CSED
&&, ||, ! Operator
Vinay Arora
CSED
? : - Conditional Operator
� The conditional operators ? and : are sometimes called Ternary
Operators since they take three arguments.
� They form a kind of foreshortened if-then-else.
� Their general form is, expression 1 ? expression 2 : expression 3
� If expression 1 is true (that is, if its value is non-zero), then the value
returned will be expression 2, otherwise the value returned will be
expression 3.
Vinay Arora
CSED
Conditional Operator (C Program)
Vinay Arora
CSED
Vinay Arora
CSED
Vinay Arora
CSED
goto Statement
� goto is used to switch the control flow.
� In a difficult programming situation it seems so easy to use a goto to
take the control.
� In most of the scenarios use of goto is depreciated.
� goto can be replaced by if-else, switch, for.
Vinay Arora
CSED
goto Statement - Program
Vinay Arora
CSED
Vinay Arora
CSED
Vinay Arora
CSED
switch Statement
� The control statement that allows us to make a decision from the number
of choices is called a switch.
The integer expression following the keyword switch is any C expression that
will yield an integer value.
Vinay Arora
CSED
switch Statement - Flowchart
Vinay Arora
CSED
switch Statement - Program
Vinay Arora
CSED
Vinay Arora
CSED
Vinay Arora
CSED
Loops
� Repetitive instructions is done through a Loop control instruction.
� This involves repeating some portion of the program either a
specified number of times or until a particular condition is being
satisfied.
� There are three methods by way of which we can repeat a part of a
program. They are:
(a) Using a while statement
(b) Using a do-while statement
(c) Using a for statement
Vinay Arora
CSED
while Loop - Flowchart
Vinay Arora
CSED
while Loop – general form
Vinay Arora
CSED
while Loop – forms of conditions
Vinay Arora
CSED
while Loop – C Program
Vinay Arora
CSED
Vinay Arora
CSED
While Loop – Program 1
//Program to demonstrate simple while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=10)
{
printf("%d\n",i);
i=i+1;
}
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
While Loop – Program 2
//Program to demonstrate simple while loop with decrement operator
#include<stdio.h>
#include<conio.h>
void main()
{
int i=5;
clrscr();
while (i>=1)
{
printf("%d\n",i);
i=i-1;
}
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
While Loop – Program 3
/* Program to demonstrate simple while loop taking incremental value
as float */
#include<stdio.h>
#include<conio.h>
void main()
{
float i=10.0;
clrscr();
while (i<=10.5)
{
printf("\nCivil Engineering at Thapar");
i=i+.1;
}
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
While Loop – Program 4
//Demonstrating simple while loop with integer value out of range
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=32767)
{
printf("%d\n",i);
i=i+1;
}
getch();
}
Vinay Arora
CSED
Output – Infinite Loop
Vinay Arora
CSED
While Loop – Program 5
//Program to demonstrate simple while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=10);
{
printf("%d\n",i);
i=i+1;
}
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
While Loop – Program 6
//Program to demonstrate post increment operator in while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=10)
{
printf("%d\n",i);
i=i++;
}
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
While Loop – Program 7
//Program to demonstrate compound assignment operator within while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=5)
{
printf("%d\n",i);
i+=1;
}
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
While Loop – Program 8
//Program to demonstrate post increment operator with while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
while (i++ < 5)
{
printf("%d\n",i);
}
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
While Loop – Program 9
//Program to find out even numbers between 1-10
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=10)
{
if (i%2==0)
printf("%d\n",i);
i=i+1;
}
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
Do-while Loop
WhileWhile
Do while
Vinay Arora
CSED
Do-while Loop – Program 10
//Program to demonstrate DO-WHILE loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
/*
while(i<1)
{
printf("hello i am at Thapar");
}
*/
do
{printf("hello i am at Thapar");
}
while(i<1);
getch();
}
Vinay Arora
CSED
Output
Vinay Arora
CSED
Program Transformation
Unary Post Increment
Operator
Vinay Arora
CSED
Program Transformation
Compound
Assignment Operator
Vinay Arora
CSED
For Loop
� The for allows us to specify three things about a loop in a single line:
Vinay Arora
CSED
For Loop (Program-1)
//Program to demonstrate simple For loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for (i=1; i<=10; i=i+1)
printf("%d\n",i);
getch();
}
Vinay Arora
CSED
For Loop (Program-1 Output)
Vinay Arora
CSED
For Loop (Program-2)
//Program to demonstrate simple For loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for (i=1; i<=10;)
{
printf("%d\n",i);
i=i+1;
}
getch();
}
Vinay Arora
CSED
For Loop (Program-2 Output)
Vinay Arora
CSED
For Loop (Program-3)
//Program to demonstrate simple For loop
//Print numbers from 1 to 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
for (;i<=5;i=i+1)
{
printf("%d\n",i);
}
getch();
}
Vinay Arora
CSED
For Loop (Program-3 Output)
Vinay Arora
CSED
For Loop (Program-4)
//Program to demonstrate simple For loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
for (;i<=5;)
{
printf("%d\n",i);
i=i+1;
}
getch();
}
Vinay Arora
CSED
For Loop (Program-4 Output)
Vinay Arora
CSED
For Loop (Program-5)
//Program to demonstrate simple For loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for (i=0;i++<5;)
{
printf("%d\n",i);
}
getch();
}
Vinay Arora
CSED
For Loop (Program-5 Output)
Vinay Arora
CSED
For Loop (Program-6)
//Program to demonstrate simple For loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for (i=0;++i<5;)
{
printf("%d\n",i);
}
getch();
}
Vinay Arora
CSED
For Loop (Program-6 Output)
Vinay Arora
CSED
For Loop (Program-7)
//Program to demonstrate NESTED For loop#include<stdio.h>
#include<conio.h>
void main(){int i,j;
clrscr();
for (i=1;i<=5;i=i+1){printf("\n");
for (j=1;j<=i;j=j+1){printf(" *");}
}
getch();}
Vinay Arora
CSED
For Loop (Program-7 Output)
Vinay Arora
CSED
Thnx…