Home > Backend Development > C++ > body text

Use flowcharts and procedures to describe decision-making concepts in C

WBOY
Release: 2023-09-15 11:05:04
forward
1364 people have browsed it

The following is the decision statement-

  • Simple- if statement
  • if-else statement
  • Nested-if else statement
  • else – ifladder
  • switch statement

Simple – if statement

The “if” keyword is used to execute a set of statements when a logical condition is true.

Syntax

if (condition){
   Statement (s)
}
Copy after login

Use flowcharts and procedures to describe decision-making concepts in C

Example

The following example checks whether a number is greater than 50.

#include<stdio.h>
main (){
   int a;
   printf (&ldquo;enter any number:</p><p>&rdquo;);
   scanf (&ldquo;%d&rdquo;, &a);
   if (a>50)
      printf (&ldquo;%d is greater than 50&rdquo;, a);
}
Copy after login

Output

1) enter any number: 60
60 is greater than 50 .
2) enter any number 20
no output
Copy after login

if else statement

if else statement accepts True or False conditions.

Syntax

if (condition){
   True block statement(s)
}
else{
   False block statement(s)
}
Copy after login

Flowchart

Use flowcharts and procedures to describe decision-making concepts in C

Example

The following is a program to check odd and even numbers−

#include<stdio.h>
main (){
   int n;
   printf (&ldquo;enter any number:</p><p>&rdquo;);
   scanf (&ldquo;%d&rdquo;, &n);
   if (n%2 ==0)
      printf (&ldquo;%d is even number&rdquo;, n);
   else
      printf( &ldquo;%d is odd number&rdquo;, n);
}
Copy after login

Output

1) enter any number: 10
10 is even number
Copy after login

Nested if - else statement

Here the "if" is placed inside another if (or) else-

Syntax

if (condition1){
   if (condition2)
      stmt1;
   else
      stmt2;
   }
   else{
      if (condition3)
         stmt3;
      else
         stmt4;
   }
Copy after login

Flow chart

Use flowcharts and procedures to describe decision-making concepts in C

Example

The following example is to print the largest 3 digits of the given number.

#include<stdio.h>
main (){
   int a,b,c;
   printf (&ldquo;enter 3 numbers&rdquo;);
   scanf (&ldquo;%d%d%d&rdquo;, &a, &b, &c);
   if (a>b){
      if (a>c)
         printf (&ldquo;%d is largest&rdquo;, a);
      else
         printf (&ldquo;%d is largest&rdquo;, c);
   } else {
      if (b>c)
         printf (&ldquo;%d is largest&rdquo;, b);
      else
         printf (&ldquo;%d is largest&rdquo;, c);
   }
}
Copy after login

Output

enter 3 numbers = 10 20 30
30 is largest
Copy after login

Else – if ladder

It is a multi-way decision condition.

Syntax

if (condition1)
   stmt1;
else if (condition2)
   stmt2;
   - - - - -
   - - - - -
else if (condition n)
   stmt n;
else
   stmt x;
Copy after login

Flow chart

Use flowcharts and procedures to describe decision-making concepts in C

Example

The following example finds the roots of a quadratic equation-

#include <math.h>
main (){
   int a,b,c,d;
   float r1, r2
   printf ("enter the values a b c");
   scanf (&ldquo;%d%d%d&rdquo;, &a, &b, &c);
   d= b*b &ndash; 4*a*c ;
   if (d>0){
      r1 = (-b+sqrt(d)) / (2*a);
      r2 = (-b-sqrt(d)) / (2*a);
      printf (&ldquo;root1 ,root2 =%f%f&rdquo;, r1, r2);
   }
   else if (d== 0){
      r1 = -b / (2*a);
      r2 = -b/ (2*a);
   printf (&ldquo;root1, root2 = %f%f&rdquo;, r1, r2);
   }
   else
      printf ("roots are imaginary&rdquo;);
}
Copy after login

Output

1) enter the values of a b c : 1 4 3
Root 1 = -1
Root 2 = -3
Copy after login

Switch statement

It helps to select one from multiple decisions.

Grammar

switch (expression){
   case value1 : stmt1;
      break;
   case value2 : stmt2;
      break;
   - - - - - -
   default : stmt &ndash; x;
}
Copy after login

Grammar

Use flowcharts and procedures to describe decision-making concepts in C

Example

#include<stdio.h>
main (){
   int n;
   printf (&ldquo;enter a number&rdquo;);
   scanf (&ldquo;%d&rdquo;, &n);
   switch (n){
      case 0 : printf (&ldquo;zero&rdquo;)
         break;
      case 1 : printf (&lsquo;one&rdquo;);
         break;
      default : printf (&lsquo;wrong choice&rdquo;);
   }
}
Copy after login

Output

enter a number
1
One
Copy after login

The above is the detailed content of Use flowcharts and procedures to describe decision-making concepts in C. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template