Table of Contents
Simple – if statement
Syntax
Example
Output
if else statement
Flowchart
Nested if - else statement
Flow chart
Else – if ladder
Switch statement
Grammar
Home Backend Development C++ Use flowcharts and procedures to describe decision-making concepts in C

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

Sep 15, 2023 am 11:05 AM
Conditional statements control flow Judge sentences

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C program to print 'even' or 'odd' without using conditional statements C program to print 'even' or 'odd' without using conditional statements Sep 15, 2023 pm 09:21 PM

In this section, we will see how to check whether a number is odd or even without using any conditional statements like <, <=, !=, >, >=, ==. We can easily check if the number is odd or even by using conditional statement. We can divide the number by 2 and check if the remainder is 0. If 0, it is an even number. Otherwise, we can AND the number with 1. If the answer is 0, it is an even number, otherwise it is an odd number. Conditional statements cannot be used here. We'll see two different ways to check whether an odd or even number is present. Method 1 Here we will create an array of strings. The index 0 position will hold "even" and the index 1 position will hold "odd". We can divide numbers

5 essential Python entry example codes 5 essential Python entry example codes Jan 13, 2024 am 08:39 AM

Getting Started with Python Code: 5 Essential Examples for Learning Python is a simple and easy-to-learn high-level programming language that is widely used in data analysis, machine learning, web crawlers and other fields. For beginners, it is important to master some basic Python code. This article will introduce 5 simple example codes to help beginners quickly get started with Python programming. Print Hello,World!print("Hello,World!") This is Python

Conditional statement usage and examples in C++ Conditional statement usage and examples in C++ Aug 22, 2023 am 08:25 AM

As a high-level programming language, C++ has a variety of flow control statements to implement the decision-making structure and loop structure of the program. Among them, the conditional statement is one of the most commonly used statements in C++ programming. It determines the execution path of the program by judging whether the condition is met. This article will introduce the usage and examples of conditional statements in C++ in detail to help readers better understand and apply this syntax. 1. Basic syntax of conditional statements Conditional statements in C++ mainly include three types: if statement, ifelse statement and switch statement. their basic language

How to use conditional statements in Java to make logical judgments How to use conditional statements in Java to make logical judgments Oct 26, 2023 am 09:18 AM

How to use conditional statements in Java to make logical judgments requires specific code examples. Conditional statements are a commonly used tool in programming, which enable the program to perform different branch executions according to requirements. In Java programs, conditional statements can be used to determine the next action of the program based on the truth or falsehood of a certain condition. This article will introduce the use of conditional statements in Java and give specific code examples. In Java, there are two main forms of conditional statements: if statements and switch statements. if statement if statement is the most commonly used conditional statement

What are the three forms of conditional statements? What are the three forms of conditional statements? Jan 11, 2024 pm 01:37 PM

Three forms of conditional statements: 1. if statement: the syntax is "if (condition) {execute statement}", if the condition is true, the statement is executed; 2. if-else statement: the syntax is "if (condition) {execute Statement 1 } else {Execute statement 2 }", if the condition is true, execute statement 1; otherwise, execute statement 2; 3, switch statement, etc.

How to use conditional statements in Go? How to use conditional statements in Go? May 11, 2023 pm 04:03 PM

In Go, conditional statements are one of the keys to controlling program flow. When writing code, we often need to use conditional statements to implement specific logic control. In this article, we will discuss how to use conditional statements in Go language. If Statement The if statement is one of the most common conditional statements in Go. It determines whether to execute a block of code based on the value of a Boolean expression. The following is the basic syntax structure of the if statement: ifcondition{//ifblockofcode} which

How to use conditional statements in Python? How to use conditional statements in Python? Jun 04, 2023 pm 03:10 PM

Conditional statements in the Python language are an important programming concept that are often used to control the flow of the program and determine whether to perform different operations under different circumstances. In Python, commonly used conditional statements include if statements and if-else statements. This article will introduce how to use conditional statements in Python. 1. Basic usage of if statement The if statement is one of the most commonly used conditional statements in Python. It is used to execute a block of code under specific conditions. Its basic syntax is as follows: ifcondition

What are the common conditional statements in PHP programming? What are the common conditional statements in PHP programming? Jun 12, 2023 am 08:25 AM

PHP is an open source, general-purpose scripting language that is widely used in the field of web development. In PHP programming, conditional statements are one of the indispensable basic syntaxes, used to implement various logical judgments and flow control in the program. This article will introduce common conditional statements in PHP programming. 1. If statement The most commonly used conditional statement in PHP is the if statement. The syntax of the if statement is as follows: if (conditional expression) {//statement to be executed when the condition is true} where the conditional expression can be anything

See all articles