


Use flowcharts and procedures to describe decision-making concepts in C
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) }
Example
The following example checks whether a number is greater than 50.
#include<stdio.h> main (){ int a; printf (“enter any number:</p><p>”); scanf (“%d”, &a); if (a>50) printf (“%d is greater than 50”, a); }
Output
1) enter any number: 60 60 is greater than 50 . 2) enter any number 20 no output
if else statement
if else statement accepts True or False conditions.
Syntax
if (condition){ True block statement(s) } else{ False block statement(s) }
Flowchart
Example
The following is a program to check odd and even numbers−
#include<stdio.h> main (){ int n; printf (“enter any number:</p><p>”); scanf (“%d”, &n); if (n%2 ==0) printf (“%d is even number”, n); else printf( “%d is odd number”, n); }
Output
1) enter any number: 10 10 is even number
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; }
Flow chart
Example
The following example is to print the largest 3 digits of the given number.
#include<stdio.h> main (){ int a,b,c; printf (“enter 3 numbers”); scanf (“%d%d%d”, &a, &b, &c); if (a>b){ if (a>c) printf (“%d is largest”, a); else printf (“%d is largest”, c); } else { if (b>c) printf (“%d is largest”, b); else printf (“%d is largest”, c); } }
Output
enter 3 numbers = 10 20 30 30 is largest
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;
Flow chart
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 (“%d%d%d”, &a, &b, &c); d= b*b – 4*a*c ; if (d>0){ r1 = (-b+sqrt(d)) / (2*a); r2 = (-b-sqrt(d)) / (2*a); printf (“root1 ,root2 =%f%f”, r1, r2); } else if (d== 0){ r1 = -b / (2*a); r2 = -b/ (2*a); printf (“root1, root2 = %f%f”, r1, r2); } else printf ("roots are imaginary”); }
Output
1) enter the values of a b c : 1 4 3 Root 1 = -1 Root 2 = -3
Switch statement
It helps to select one from multiple decisions.
Grammar
switch (expression){ case value1 : stmt1; break; case value2 : stmt2; break; - - - - - - default : stmt – x; }
Grammar
Example
#include<stdio.h> main (){ int n; printf (“enter a number”); scanf (“%d”, &n); switch (n){ case 0 : printf (“zero”) break; case 1 : printf (‘one”); break; default : printf (‘wrong choice”); } }
Output
enter a number 1 One
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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.

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

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

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
