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

Unit-3

The document discusses decision-making in C programming, explaining various conditional statements such as if, if-else, and switch-case. It covers the use of relational operators and provides examples of programs that demonstrate how to implement these decision-making structures. Additionally, it introduces looping constructs like while and for loops, emphasizing their role in executing code repeatedly based on specified conditions.

Uploaded by

akshaysangani89
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit-3

The document discusses decision-making in C programming, explaining various conditional statements such as if, if-else, and switch-case. It covers the use of relational operators and provides examples of programs that demonstrate how to implement these decision-making structures. Additionally, it introduces looping constructs like while and for loops, emphasizing their role in executing code repeatedly based on specified conditions.

Uploaded by

akshaysangani89
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Decision

making in C
Need of decision making

if number is odd
{
/* code */
}

else number is ev
en
{
/* code */
}

2
Decision Making or Conditional Statement
 C program statements are executed sequentially.
 Decision Making statements are used to control the flow of program.
 It allows us to control whether a program segment is executed or not.
 It evaluates condition or logical expression first and based on its result
(either true or false), the control is transferred to particular statement.
 If result is true then it takes one path else it takes another path.

3
Decision Making Statements in C

Decision Making Statements


are
One way Decision: if (Also known as simple if)
Two way Decision: if…else
Multi way Decision: if…else if…else if…else
Two way Decision: ?: (Conditional Operator)
n-way Decision: switch…case

4
Relational Operators
 Relational Operator is used to compare two expressions.
 It gives result either true or false based on relationship of two
expressions.
Math C Meaning Example Result
> > is greater than 5 > 4 true
≥ >= is greater than or equal 5 >= 4 true
to
< < is less than 5 < 4 false
≤ <= is less than or equal to 5 <= 4 false
≠ != is not equal to 5 != 4 true
= == is equal to 5 == 4 false

5

If statement
if
 if is single branch decision making statement.
 If condition is true then only body will be executed.
 if is a keyword. Flowchart of if
Syntax
if(condition)
{ False
// Body of the if
conditio
// true part n
}
True

7
WAP to print Zero if given number is 0

Outpu
Program
t
1 #include<stdio.h> Enter Number:0
2 void main() Zero
3 {
4 int a;
5 printf("Enter Number:");
6 scanf("%d",&a);
7 if(a == 0)
8 {
9 printf("Zero");
10 }
11 }

8
WAP to print Positive or Negative Number

Outpu
Program
t
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
Outpu
4 int a; t
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a >= 0)
8 {
9 printf("Positive Number")
10 ;
11 }
12 if(a < 0)
13 {
14 printf("Negative Number")
15 ;
}
}
9
Modulus Operator
 % is modulus operator in C
 It divides the value of one expression (number) by the value of another expression
(number), and returns the remainder.
 Syntax: express1 % express2
 E.g.
 7%2 Answer: 1
 6%2 Answer: 0
 25%10 Answer: 5
 37%28 Answer: 9

10
WAP to print Odd or Even Number

Outpu
Program
t
1 #include<stdio.h> Enter Number:12
2 void main() Even Number
3 {
Outpu
4 int a; t
5 printf("Enter Number:"); Enter Number:11
6 scanf("%d",&a); Odd Number
7 if(a%2 == 0)
8 {
9 printf("Even Number");
10 }
11 if(a%2 != 0)
12 {
13 printf("Odd Number");
14 }
15 }

11

If..else statement
if...else
 if…else is two branch decision making statement
 If condition is true then true part will be executed else false part will be
executed
 else is keyword Flowchart of if…
Syntax else
if(condition)
{ True conditio False
// true part
} n
else
{
// false part … …
}

13
WAP to print Positive or Negative Number using if…
else
Outpu
Program
t
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
Outpu
4 int a; t
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a >= 0)
8 {
9 printf("Positive Number")
10 ;
11 }
12 else
13 {
14 printf("Negative Number")
15 ;
}
}
14
WAP to print Odd or Even Number using if…else

Outpu
Program
t
1 #include<stdio.h> Enter Number:12
2 void main() Even Number
3 {
Outpu
4 int a; t
5 printf("Enter Number:"); Enter Number:11
6 scanf("%d",&a); Odd Number
7 if(a%2 == 0)
8 {
9 printf("Even Number");
10 }
11 else
12 {
13 printf("Odd Number");
14 }
15 }

15
WAP to find largest number from given 2 numbers using if

Outpu
Program
t
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 if(a > b)
8 {
9 printf("%d is largest", a
10 );
11 }
12 if(a < b)
13 {
14 printf("%d is largest",
15 b);
}
}
16
WAP to find largest number from given 2 numbers using if…else

Outpu
Program
t
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 if(a > b)
8 {
9 printf("%d is largest", a
10 );
11 }
12 else
13 {
14 printf("%d is largest",
15 b);
}
}
17
{ }
 If body of if contains only one statement then { } are not compulsory
 But if body of if contains more than one statements then { } are
compulsory

if(a >= b) Both if(a >= b)


{ printf("%d is largest",
are
printf("%d is largest", a a);
); same
}

18

If…else if…else if…else


Ladder if
If…else if…else if…else
 if…else if…else if…else is multi branch decision making statement.
 If first if condition is true then remaining if conditions will not be
evaluated.
 If first if condition is false then second if condition will be evaluated
and if it is true then remaining if conditions will not be evaluated.
 if…else if…else if…else is also known as if…else if ladder
Syntax
if(condition-1)
statement-1;
else if(condition-2)
statement-2;
else
statement-3;

20
if…else if…else ladder flowchart

conditio False
n1

True conditio False


n2
… True Conditio False
n3
… True

… …

21
WAP to print Zero, Positive or Negative Number

Outpu
Program
t
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
Outpu
4 int a; t
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a > 0)
8 printf("Positive Number")
9 ;
10 else if(a==0)
11 printf("Zero");
12 else
13 printf("Negative Number")
;
}

22

Nested if
Nested if
 If condition-1 is true then condition-2 is evaluated. If it is true then
statement-1 will be executed.
 If condition-1 is false then statement-3 will be executed.
Syntax
if(condition-1)
{
if(condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}

24
Nested if flowchart

False conditio True


n1

Statemen conditio False


t3 n2

True
Statemen Statemen
t1 t2

Next
Statemen
t
25
WAP to print maximum from given three numbers
Program
1 void main(){ Outpu
2 int a, b, c; t
3 printf("Enter Three Numbers:" Enter Three Numbers:7
4 ); 5
5 scanf("%d%d%d",&a,&b,&c); 9
6 if(a>b) 9 is max
7 {
8 if(a>c)
9 printf("%d is max",a);
10 else
11 printf("%d is max",c);
12 }
13 else
14 {
15 if(b>c)
16 printf("%d is max",b);
17 else
18 printf("%d is max",c);
19 }
} 26

Conditional Operator
? : (Conditional Operator)
 The conditional works operator is similar to the if-else.
 It is also known as a ternary operator.
 It returns first value of expression (before colon(:)) if expression is true
and second value of expression if expression is false.

Fals
Tru e
e
variable = Expression1 ? Expression2 : Express
ion3 Result
value Result
value

28
Conditional operator flowchart
 Here, Expression1 is the condition to
be evaluated.
True Expressio False
n1
 If the condition(Expression1) is True
then Expression2 will be executed
and the result will be returned.
Expressio Expressio  Otherwise, if condition(Expression1)
n2 n3
is false then Expression3 will be
executed and the result will be
Variable
returned.

29
WAP to find largest number from given 2 numbers using ? :

Outpu
Program
t
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b, max;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 max = a>b?a:b;
8 printf("%d is largest",max);
9 }

30

switch…case
switch...case
 The switch statement allows to execute one code block among many
alternatives.
 It works similar to if...else..if ladder.
Syntax  The expression is evaluated once
switch (expression) and compared with the values of
{
​ each case.
case constant1:
// statements  If there is a match, the
break; corresponding statements after the
case constant2: matching case are executed.
// statements
break;  If there is no match, the default
. statements are executed.
.
.  If we do not use break, all
default: statements after the matching label
// default statements are executed.
}
 The default clause inside the
32
WAP that asks day number and prints day name using switch…
case
void main(){ case 7:
int day; printf("Saturday");
printf("Enter day number(1-7):"); break;
scanf("%d",&day); default:
switch(day) printf("Wrong input");
{ break;
case 1: }
printf("Sunday"); }
break;
case 2:
printf("Monday");
break;
case 3: Outpu
printf("Tuesday"); t
break; Enter day number(1-7):5
case 4: Thursday
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;

33
Looping
Life is all about Repetition.
We do same thing everyday
What is loop?
 Loop is used to execute the block of code several times according to the
condition given in the loop. It means it executes the same code multiple
times.
“Hello” 5

Outpu
t
printf("Hello\n"); Hello
printf("Hello\n"); Hello loop(condition)
{
printf("Hello\n"); Hello //
printf("Hello\n"); Hello statements
}
printf("Hello\n"); Hello

36
if v/s while

v/
Flowchart of if s Flowchart of while

conditio False conditio False


n n
True True

… …

37
Looping or Iterative Statements in C
Looping Statements are
Entry Controlled Loop: while,
for
Exit Controlled Loop: do…while
Virtual Loop: goto

38

While loop
While Loop
 while is an entry controlled loop
 Statements inside the body of while are repeatedly executed till the
condition is true
 while is keyword
Synta
x
while(condition)
{
// Body of the while
// true part
}

40
WAP to print 1 to n(while loop)

Outpu
Program
t
1 #include <stdio.h> Enter n:10
2 void main() 1
3 { 2
4 int i,n; 3
5 i=1; 4
6 printf("Enter n:"); 5
7 scanf("%d",&n); 6
8 while(i<=n) 7
9 { 8
10 printf("%d\n",i); 9
11 i=i+1; 10
12 }
13 }

41
WAP to print multiplication table(while loop)

Outpu
Program
t
1 #include<stdio.h> Enter n for multiplication
2 void main() table:5
3 { 5 * 1 = 5
5 * 2 = 10
4 int i=1,n;
5 * 3 = 15
5 printf("Enter n for multiplication table:") 5 * 4 = 20
6 ; 5 * 5 = 25
7 scanf("%d",&n); 5 * 6 = 30
8 while(i<=10) 5 * 7 = 35
9 { 5 * 8 = 40
10 printf("%d * %d = %d\n",n,i,n*i); 5 * 9 = 45
11 i=i+1; 5 * 10 = 50
12 }
}

43
WAP to Sum of 5 numbers entered by user(while
loop)
Outpu
Program
t
1 #include<stdio.h> Enter a number=10
2 void main() Enter a number=20
3 { Enter a number=30
Enter a number=40
4 int sum=0, i=1,n;
Enter a number=50
5 while(i<=5) Sum is=150
6 {
7 printf("Enter a number=");
8 scanf("%d",&n);
9 sum=sum+n;
10 i=i+1;
11 }
12 printf("Sum is=%d",sum);
13 }

44
How to build logic? Step-1
Step 1: Understand the problem statement
 e.g. Write a program to find factors of a number.
 Run following questions through mind
 What is the factor of a number?
 Factor is a number that divides another number evenly with no remainder.
 For example, 1,2,3,4,6,12 are factors of 12.
 How many variables needed? What should be their data
types?(Inputs/Outputs)
 To get number from user we need variable n.
 Now we need to divide n with 1,2,3,...,n. For this we will declare a loop variable i initialized
as 1.
 Both variables should be of integer data type.
 What control structure you require?
 First we need a loop to divide n by 1,2,3,…,n, loop will start from 1 and ends at n.
 Inside loop we need if structure to check n%i==0 (Number n is evenly divisible by i or
not).
46
How to build logic? Step-2
Step 2: Think for 1 or 2 examples
 Consider n=6, now take i=1
 6%1==0, TRUE; So, 1 is factor of 6
 6%2==0, TRUE; So, 2 is factor of 6
 6%3==0, TRUE; So, 3 is factor of 6
 6%4==2, FALSE; S0, 4 is not factor of 6
 6%5==1, FALSE; S0, 5 is not factor of 6
 6%6==0, TRUE; S0, 6 is factor of 6
 From this we can infer that loop variable i starts with 1 and
incremented by one for next iteration then ends at value n.
 Consider n=10, factors are 1,2,5,10
 Consider n=11, factor is 1,11
 From this we can infer that 1 and number itself are always factors of any
number n.

47
How to build logic? Step-3
Step 3: Draw flowchart/steps on paper or in mind
Start
Steps
i=1 Step 1: Start
Step 2: Declare variables n,i
read n Step 3: Initialize variable

i ← 1
i<=n Step 4: Read value of n
True ? False Step 5: Repeat the steps until i
= n
n
5.1: if n%i == 0
True %i==0? False Display i
print i 5.2: i=i+1
Step 7: Stop

i=i+1
Stop
48
How to build logic? Step-4
Step 4: Writing Pseudo-code
 Pseudo-code is an informal way to express the design of a computer
program or an algorithm.
 It does not require any strict programming language syntax.

Pseudo-
code
Initialize i=1 integer
Declare n as integer
Input n
while i<n
if n%i
print i
end if
increment i=i+1
end while

49
WAP to find factors of a number(while loop)

Outpu
Program
t
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i=1,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 while(i<=n)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 i=i+1;
12 }
13 }

50
WAP to print reverse a number(while loop)

Outpu
Program
t
1 #include <stdio.h> Enter a number=1234
2 void main() 4321
3 {
4 int n;
5 printf("Enter a number=");
6 scanf("%d",&n);
7 while(n!=0)
8 {
9 printf("%d",n%10);
10 n=n/10;
11 }
12 }

51
WAP to check given number is prime or not(while
loop)
1 void main()
2 { Outpu
3 int n, i=2,flag=0; t
4 printf("Enter a number:"); Enter a number:7
5 scanf("%d",&n); 7 is a prime number
6 while(i<=n/2)
7 { Outpu
8 if(n%i==0) t
9 { Enter a number:9
9 is not a prime number
10 flag=1;
11 break;
12 }
13 i++;
14 }
15 if (flag==0)
16 printf("%d is a prime number",n);
17 else
18 printf("%d is not a prime number",n);
19 }

52

for loop
for Loop
 for is an entry controlled loop
 Statements inside the body of for are repeatedly executed till the
condition is true
Synta
 for is keyword x
for (initialization; condition; updateStatement)
{
// statements
}

 The initialization statement is executed only once.


 Then, the condition is evaluated. If the condition is false, the for loop is
terminated.
 If the condition is true, statements inside the body of for loop are
executed, and the update statement is updated.
 Again the condition is evaluated.
54
WAP to print numbers 1 to n (for loop)

Outpu
Program
t
1 #include<stdio.h> Enter a number:5
2 void main() 1
3 { 2
4 int i,n; 3
5 printf("Enter a number:"); 4
6 scanf("%d",&n); 5
7 for(i=1;i<=n;i++)
8 {
9 printf("%d\n",i);
10 }
11 }

55
WAP to find factors of a number (for loop)

Outpu
Program
t
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 for(i=1;i<=n;i++)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 }
12 }

56

do while loop
do while Loop
 do while is an exit controlled loop.
 Statements inside the body of do while are repeatedly executed till the
condition is true.
 Do and while are keywords. Synta
x
do
{
// statement
}
while (condition);

 Loop body will be executed first, and then condition is checked.


 If the condition is true, the body of the loop is executed again and the
condition is evaluated.
 This process goes on until the condition becomes false.
 If the condition is false, the loop ends.
58
WAP to print Odd numbers between 1 to n(do while loop)

Outpu
Program
t
1 void main() Enter a number:5
2 { 1,3,5
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(i%2!=0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 }

59
WAP to find factors of a number(do while loop)

Outpu
Program
t
1 void main() Enter a number:6
2 { 1,2,3,6,
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(n%i==0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 }

60
WAP to print reverse a number(do while loop)

Outpu
Program
t
1 void main() Enter a number=1234
2 { 4321
3 int n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 printf("%d",n%10);
9 n=n/10;
10 }
11 while(n!=0);
12 }

61

goto statement
goto Statement
 goto is an virtual loop
 The goto statement allows us to transfer control of the program to the
specified label.
 goto is keyword
Synta Synta
x x
goto label label:
; .
. .
. .
. goto label
label: ;
 The label is an identifier. When the goto statement is encountered, the
control of the program jumps to label: and starts executing the code.

63
WAP to print Odd numbers between 1 to n(goto)

Outpu
Program
t
1 void main() Enter a number:5
2 { 1,3,5
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(i%2!=0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 }
16 }

64
WAP to find factors of a number(goto)

Outpu
Program
t
1 void main() Enter a number:6
2 { 1,2,3,6,
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(n%i==0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 }
16 }

65
Types of loops
Entry Control Entry Control Virtual
Exit Control Loop
Loop Loop Loop
int i=1; int i; int i=1; int i=1;
while(i<=10) for(i=1;i<=10;i++) do labelprint:
{ { { printf("%d",i+
printf("%d",i+ printf("%d",i); printf("%d",i+ +);
+); } +); if(i<=10)
} } goto labelprin
while(i<=10); t;

conditio False Loop Statemen


Label
n Body t
True True
Loop conditio conditio
n n
Body False True False
goto

66

Pattern
Always detect pattern in pattern
Pattern
There are important points to note in pattern
1. Determine, how many rows?
2. Determine, how many numbers/characters/columns in a row?
3. Determine, Increment/Decrement among the number of rows.
4. Determine, starting in each row

1 1 1 *
11 12 23 * *
111 123 456 * * *
1111 1234 78910 * * * *
11111 12345 * * *
* *
*

68
WAP to print given pattern (nested loop)
* Program
** 1 void main()
*** 2 {
**** 3 int i,j;
***** 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5
6 for(j=1; j<=i; j++)
No. of characters 7 {
Row-1: * 8 printf("*");
Row-2: ** 9 }
Row-3: *** 10 printf("\n");
Row-4: **** 11 }
Row-5: ***** 12 }

Inner loop:
Increment
Outer loop:
Starting:
Increment*

69
WAP to print given pattern (nested loop)
1 Program
12 1 void main()
123 2 {
1234 3 int i,j;
12345 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5
6 for(j=1; j<=i; j++)
No. of values 7 {
Row-1: 1 8 printf("%d",j);
Row-2: 12 9 }
Row-3: 123 10 printf("\n");
Row-4: 1234 11 }
Row-5: 12345 12 }

Inner loop:
Increment
Outer loop:
Starting:
Increment1

70
WAP to print given pattern (nested loop)
5 Program
54 1 void main()
543 2 {
5432 3 int i,j;
54321 4 for(i=5;i>0;i--)
5 {
No. of rows: 5
6 for(j=5; j>=i ; j-
No. of values 7 -)
Row-1: 5 8 {
Row-2: 54 9 printf("%d",j)
Row-3: 543 10 ;
Row-4: 5432 11 }
Row-5: 54321 12 printf("\n");
}
Inner loop: }
Decrement
Outer loop:
Decrement/Incremen
Starting:
t 5
71
WAP to print given pattern (nested loop)
Program
* 1 void main() First we need to print
2 { 4 spaces before
** 3 int i,j,k; printing *
4 for(i=1;i<=5;i++)
*** 5 {
No. of rows: 5 *
6 for(k=5;k>i;k--)
**** 7 { **
No. of values
**** 8 printf(" "); ***
Row-1: ----*
* 9 }
Row-2: ---** ****
Row-3: --*** 10 for(j=1;j<=i;j++) ****
11 { *
Row-4: -**** After printing spaces
Row-5: ***** 12 printf("*");
this inner loop prints
13 }
*
Inner loop: Decrement 14 printf("\n");
Outer loop: 15 }
Decrement/Increment 16 }
Starting: -(space)
Ending: *
72

Thank you

You might also like