1 of 33

Decision making in �C

{C}

Programming

Programming for Problem Solving (PPS)

GTU # 3110003

USING

Prof. Nilesh Gambhava

Computer Engineering Department,�Darshan Institute of Engineering & Technology, Rajkot

2 of 33

Need of decision making

if number is odd

{

    /* code */

}

else number is even

{

    /* code */

}

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

2

3 of 33

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.

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

3

4 of 33

If statement

5 of 33

if

  • if is single branch decision making statement.
  • If condition is true then only body will be executed.
  • if is a keyword.

if(condition)

{

    // Body of the if

    // true part

}

condition

True

False

Syntax

Flowchart of if

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

5

6 of 33

WAP to print Zero if given number is 0

#include<stdio.h>

void main()

{

    int a;

    printf("Enter Number:");

    scanf("%d",&a);

    if(a == 0)

    {

        printf("Zero");

    }

}

1

2

3

4

5

6

7

8

9

10

11

Enter Number:0

Zero

Program

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

6

7 of 33

WAP to print Positive or Negative Number

#include<stdio.h>

void main()

{

    int a;

    printf("Enter Number:");

    scanf("%d",&a);

    if(a >= 0)

    {

        printf("Positive Number");

    }

    if(a < 0)

    {

        printf("Negative Number");

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Enter Number:5

Positive Number

Program

Output

Enter Number:-5

Negative Number

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

7

8 of 33

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

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

8

9 of 33

If..else statement

10 of 33

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

if(condition)

{

    // true part

}

else

{

    // false part

}

Syntax

Flowchart of if…else

condition

True

False

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

10

11 of 33

WAP to print Positive or Negative Number using if…else

#include<stdio.h>

void main()

{

    int a;

    printf("Enter Number:");

    scanf("%d",&a);

    if(a >= 0)

    {

        printf("Positive Number");

    }

    else

    {

        printf("Negative Number");

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Program

Enter Number:5

Positive Number

Output

Enter Number:-5

Negative Number

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

11

12 of 33

WAP to print Odd or Even Number using if…else

#include<stdio.h>

void main()

{

    int a;

    printf("Enter Number:");

    scanf("%d",&a);

    if(a%2 == 0)

    {

        printf("Even Number");

    }

    else

    {

        printf("Odd Number");

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Enter Number:12

Even Number

Program

Output

Enter Number:11

Odd Number

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

12

13 of 33

WAP to find largest number from given 2 numbers using if…else

#include<stdio.h>

void main()

{

    int a, b;

    printf("Enter Two Numbers:");

    scanf("%d%d",&a,&b);

    if(a > b)

    {

        printf("%d is largest", a);

    }

    else

    {

         printf("%d is largest", b);

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Enter Two Numbers:4

5

5 is largest

Program

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

13

14 of 33

{ }

  • 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)

    printf("%d is largest", a);

if(a >= b)

{

    printf("%d is largest", a);

}

Both are same

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

14

15 of 33

If…else if…else if…else �Ladder if

16 of 33

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

if(condition-1)

  statement-1;

else if(condition-2)

  statement-2;

else

  statement-3;

Syntax

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

16

17 of 33

if…else if…else ladder flowchart

condition1

True

False

condition2

True

False

Condition 3

True

False

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

17

18 of 33

WAP to print Zero, Positive or Negative Number

#include<stdio.h>

void main()

{

    int a;

    printf("Enter Number:");

    scanf("%d",&a);

    if(a > 0)

        printf("Positive Number");

    else if(a==0)

        printf("Zero");

    else

        printf("Negative Number");

}

1

2

3

4

5

6

7

8

9

10

11

12

13

Program

Enter Number:5

Positive Number

Output

Enter Number:-5

Negative Number

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

18

19 of 33

WAP for Below condition:

Marks Grade

90-100 : A

70-89 : B

50-69: C

Fail

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

19

20 of 33

#include<stdio.h>

void main()�{

int marks;

printf("Enter your marks between 0-100\n");

scanf("%d", &marks);

if(marks >= 90)

{

printf("YOUR GRADE : A\n");

}

else if (marks >= 70 && marks < 90)

{

printf("YOUR GRADE : B\n");

}

else if (marks >= 50 && marks < 70)

{

printf("YOUR GRADE : C\n");

}

else

{

printf("YOUR GRADE : Failed\n");

}

getch();

}

21 of 33

Nested if

22 of 33

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.

if(condition-1)

{

    if(condition-2)

    {

      statement-1;

    }

    else

    {

      statement-2;

    }    

}

else

{

  statement-3;

}

Syntax

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

22

23 of 33

Nested if flowchart

condition1

True

False

Statement3

condition2

True

False

Statement 1

Statement 2

Next

Statement

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

23

24 of 33

WAP to print maximum from given three numbers

void main(){

    int a, b, c;

    printf("Enter Three Numbers:");

    scanf("%d%d%d",&a,&b,&c);

    if(a>b)

    {

      if(a>c)

        printf("%d is max",a);

      else

        printf("%d is max",c);

  }

  else

  {

    if(b>c)

      printf("%d is max",b);

    else

      printf("%d is max",c);

  }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Program

Enter Three Numbers:7

5

9

9 is max

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

24

25 of 33

Conditional Operator

26 of 33

? : (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.

variable = Expression1 ? Expression2 : Expression3

False

True

Result value

Result value

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

26

27 of 33

Conditional operator flowchart

  • Here, Expression1 is the condition to be evaluated.
  • If the condition(Expression1) is True then Expression2 will be executed and the result will be returned.
  • Otherwise, if condition(Expression1) is false then Expression3 will be executed and the result will be returned.

Expression1

True

False

Expression 3

Expression 2

Variable

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

27

28 of 33

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

#include<stdio.h>

void main()

{

    int a, b, max;

    printf("Enter Two Numbers:");

    scanf("%d%d",&a,&b);

    max = a>b?a:b;

    printf("%d is largest",max);

}

1

2

3

4

5

6

7

8

9

Enter Two Numbers:4

5

5 is largest

Program

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

28

29 of 33

switch…case

30 of 33

switch...case

  • The switch statement allows to execute one code block among many alternatives.
  • It works similar to if...else..if ladder.

switch (expression)

​{

    case constant1:

      // statements

      break;

    case constant2:

      // statements

      break;

    .

    .

    .

    default:

      // default statements

}

Syntax

  • The expression is evaluated once and compared with the values of each case.
  • If there is a match, the corresponding statements after the matching case are executed.
  • If there is no match, the default statements are executed.
  • If we do not use break, all statements after the matching label are executed.
  • The default clause inside the switch statement is optional.

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

30

31 of 33

WAP that asks day number and prints day name using switch…case

void main(){

    int day;

    printf("Enter day number(1-7):");

    scanf("%d",&day);

    switch(day)

    {

        case 1:

                printf("Sunday");

                break;

        case 2:

                printf("Monday");

                break;

        case 3:

                printf("Tuesday");

                break;

        case 4:

                printf("Wednesday");

                break;

        case 5:

                printf("Thursday");

                break;

        case 6:

                printf("Friday");

                break;

Enter day number(1-7):5

Thursday

Output

        case 7:

                printf("Saturday");

                break;

        default:

                printf("Wrong input");

               

    } 

}

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

31

32 of 33

Practice programs

  1. Write a program to check whether entered character is vowel or not?
  2. Write a program to perform Addition, Subtraction, Multiplication and Division of 2 numbers as per user’s choice (using if…else/Nested if/Ladder if).
  3. Write a program to read marks of five subjects. Calculate percentage and print class accordingly. Fail below 35, Pass Class between 35 to 45, Second Class between 45 to 60, First Class between 60 to 70, Distinction if more than 70.
  4. Write a program to find out largest number from given 3 numbers (Conditional operator).
  5. Write a program to print number of days in the given month.

Prof. Nilesh Gambhava

#3110003 (PPS) – Decision Making

32

33 of 33

Thank you