ホームページ  >  記事  >  バックエンド開発  >  フローチャートと手順を使用して、C で意思決定の概念を説明します。

フローチャートと手順を使用して、C で意思決定の概念を説明します。

WBOY
WBOY転載
2023-09-15 11:05:041326ブラウズ

以下は決定ステートメントです-

  • 単純な if ステートメント
  • if-else ステートメント
  • ネストされた if else ステートメント
  • else – ifladder
  • switch ステートメント

単純 – if ステートメント

「if」キーワードは、論理条件が true の場合に一連のステートメントを実行するために使用されます。

構文

if (condition){
   Statement (s)
}

フローチャートと手順を使用して、C で意思決定の概念を説明します。

次の例では、数値が 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);
}

出力

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

if else ステートメント

if else ステートメントは True または False の条件を受け入れます。

構文

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

フローチャート

フローチャートと手順を使用して、C で意思決定の概念を説明します。

次は、奇数と偶数をチェックするプログラムです-

#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);
}

出力

1) enter any number: 10
10 is even number

ネストされた if - else ステートメント

ここでは、「if」が別の if (または) else-

構文の中に配置されています

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

フローチャート

フローチャートと手順を使用して、C で意思決定の概念を説明します。

次の例は、指定された数値の最大 3 桁を出力します。

#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);
   }
}

出力

enter 3 numbers = 10 20 30
30 is largest

Else – ifladder

これは多方向の決定条件です。

構文

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

フローチャート

フローチャートと手順を使用して、C で意思決定の概念を説明します。

次の例では、二次方程式の根を求めます。

#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;);
}

出力

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

Switchステートメント

複数の決定から1つを選択するのに役立ちます。

文法

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

文法

フローチャートと手順を使用して、C で意思決定の概念を説明します。

#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;);
   }
}

出力

enter a number
1
One

以上がフローチャートと手順を使用して、C で意思決定の概念を説明します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。