.

C Programming Decision Making Assignment Help

Decision Making

Conditional statements concept is the one way to change the sequential execution of program. Conditions are constraints which are used to make decision based on the output of the given condition. 

C supports the following the decision making statements:

  • If statement
  • if statement
  • if-else statement
  • Nested if-else statement
  • else if-statement
  • goto statement
  • switch statement

C Programming Decision Making Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com


If statement: simple If statement

If statement is used to perform a specific task only a given constraint is satisfied. In If statement the program allow to execute a set of statements when the condition is true. Otherwise the program control is come out from the if block.

Syntax:

if (condition)

{'//group of statements /*This block will be executed only if the condition value '}

become ‘1’

Example:

#include <stdio.h>

int main ()

{'/* local variable declaration */'}

int age ;

printf(“Enter Ur age:”);

scanf(“%d”,&age);

{'/* check the boolean condition using if statement */'}

if( age>=18)

{'/* if condition is true then print the following */'}

printf("You are eligible for Voting…\n" );

printf(“Good Day”);

return 0;

Output:

Enter Ur age:20

You are eligible for Voting…

Good Day

Output:

Enter Ur age:16

Good Day

If-else statement

It is the extended version of simple If statement. If the given condition is satisfied the block of statements followed by the if.. condition will be executed otherwise the control is transferred to the other block of statements.

Syntax:

if(condition)

{'/* statement(s) will execute if the condtion is true */'}

else

{'/* statement(s) will execute if the condition expression is false */'}

Example:

#include <stdio.h>

 int main ()

{'/* local variable declaration */'}

int a;

printf(“Enter Number which is less than 10:”);

scanf(“%d”,&a);

{'/* check the boolean condition */'}

if( a < 10 )

{'/* if condition is true then print the following */'}

printf("You entered correct number\n" );

else

{'/* if condition is false then print the following */'}

printf("You entered wrong number\n" );

return 0;

Output:

Enter number which is less than 10

5

You entered correct number

Enter number which is less than 10

30

You entered wrong number

Nested if-else statement

An if – else statement exists within if or else block, it is called as nested if-else statement. Sub condition can be verified with the use of nested if-else statement.

Syntax:

if(outer-condition)

if(inner-condition)

{'// inner block of Statements'}

else

{'//outer else block of statements'}

Example:

#include <stdio.h>

int main()

int a, b;

printf("Enter the first value:");

scanf("%d", &a);

printf("Enter the Second value:");

scanf("%d",&b);

if (a>= b)

{'//Nested if else'}

if (a!=b)

c=a-b;

printf("Difference between two value is %d",c);

else

printf("first value is lower than second value\n");

return 0;

Output:

Enter the first value:20

Enter the second value:10

Difference between two value is 10

Output:

Enter the first value:10

Enter the second value:20

first value is lower than second value

else if-statement

To change the flow of execution into multiple paths based on multiple conditions, then the program adopts else if-statement.

Syntax

if (condition1)

{'//block1- statements'}

else if(condition2)

{'// block2- statements'}

else if (condition3)

{'// block3- statements'}

.

.

else

{'//block-statements'}

Example:

#include <stdio.h>

int main()

int m1, m2;

printf("Enter Mark1:");

scanf("%d", &m1);

printf("Enter Mark2:");

scanf("%d",&m2);

if (m1 !=m2)

printf("Mark1 is not equal to Mark2\n");

else if (m1 > m2)

printf("Mark1 is greater than mark2\n");

else if (m2 > m1)

printf("Mark2 is greater than Mark1\n");

else

printf("Mark1 is equal to Mark2\n");

return 0;

Output:

Enter Mark1:50

Enter Mark2:40

Mark1 is not equal to Mark2

Goto statement

The goto statement is used to change the usual sequential execution of a program. It forces the OS to jump the program control to the specified location within the program. It is also called as jumping statement.

Syntax

goto label(identifier);

... .. ...

... .. ...

... .. ...

Label(identifier):

statement;

Example

# include <stdio.h>

int main()

int i;

float m1,m2,m3,tot,per;

printf("Enter a mark1:");

scanf(“%f”,&m1);

printf("Enter a mark2:");

scanf(“%f”,&m2);

printf("Enter a mark3:");

scanf(“%f”,&m3);

{'// If user enters negative number, flow of program moves to label jump'}

if ((m1<0.0)|| (m2<=0.0) || (m3<=0.0))

goto jump;

tot=m1+m2+m3;

per=tot/3;

printf(“Total is:%f”,tot);

printf(“percentage is:%f”,per);

jump:

return 0;

Output:

Enter a mark1:40

Enter a mark2:40

Enter a mark3:40

Total is :120

percentage is:40

output:

Enter a mark1:50

Enter a mark2:-6

Enter a mark3:40

Program terminates

Switch-case statement

It switches the flow of program execution to n-number of cases based on the value which exists in switch statement.

switch( expression )

case value1:

Block-1 statements;

Break;

case value2:

Block-2 statements;

Break;

case valuen:

Block-n statements;

Break;

default:

Block-1 statements;

Break;

The following rules apply to a switch statement −

  • The value of the expression may be integer or a character.
  • Each case is identified using value1, 2, n. No two case values are same.
  • Case labels end with a colon ( : ). Each of these case have block of executable statements.
  • Whenever the switch is executed, it returns the value from the expression and the value is compared with all the cases which reside within the switch-case block.
  • The case block will be executed if the expression value is matched with case value.
  • The break statement terminates the program execution and switch over the control out of the switch block.
  • The optional blocks of statements are written in the section default case. When the expression value is not matched with any case value, the default block will be executed.

Example:

#include <stdio.h>

int main()

int a,b,c,opt;

printf(“Enter first Number:”);

scanf(“%d”,&a);

printf(“Enter second Number:”);

scanf(“%d”,&b);

printf(“Pls enter your option? 1-add,2-sub,3-mul”);

scanf(“%d”,&opt);

switch (opt)

case 1:

c=a+b;

printf("Addition:%d",c);

break;

case 2:

c=a-b;

printf("subtraction:%d",c);

break;

case 3:

c=a*b;

printf("Multiplication:%d",c);

break;

default:

printf("Choice other than 1, 2 and 3");

break;

return 0;

 

Output:

Enter first Number:6

Enter second Number: 4

Pls enter your option? 1-add,2-sub,3-mul

3

Multiplication:24

.