C Programming | Decision making and Branching

Qus. 01: What is decision making? What are the statements used in C for decision making purpose?

Ans. : Decision making : In practice, users may have a number of situations where they may have to change the order of statements based on certain conditions. This is decision making.
            The decision making statements in C are as follows,
i)            If statement
ii)          switch statement
iii)        Conditional operator statement
iv)         goto statement


Ans. : The if statement : The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two way decision-statement. It takes the following form,
                        if(test expression)
            This point of program has two paths to follow, one for the true condition and the other for the false condition. That is shown in the following figure,


The if statement may be used in different forms depending on the complexity of conditions to be tested. The different forms are,
i)            Simple if statement
ii)          if....else statement
iii)        Nested if......else statement
iv)         else if ladder

Qus. 03: Write down about the simple if statement.

Ans. : Simple if statement : The general form of a simple if statement is as follows,
if (test expression)
{
         statement-block;
}
statement – x;
          Here, the statement block may contain a single statement or a group of statements. If the test expression is true then the statement block will be executed; otherwise it will be skipped to the statement – x. This can be shown by the following flowchart,


Qus. 04: Write down about the if.....else statement.

Ans. : The if....else statement : The general form of the if....else statement is as follows,
if (test expression)
            {
              true-block statements;
            }
else
            {
               False-block statements;
            }
statement-x
          Here, if the test expression is true, then the true-block statements will be executed, otherwise the false-block statements will be executed. In either case, either true block or false block statements will be executed, not both. The general form can be shown by the following flowchart. 



Qus. 05 : Write down about the nested if.....else statement.

Ans. : Nested if...else statement : When a series of decisions are involved, we may have to use more than one if....else statement in nested form as shown below,


Here, if the condition 1 is false then it skipped to statement 3. But if the condition 1 is true, then it tests condition 2. If condition 2 is true then it executes statement 1 and if false then it executes statement 2. Then the control is transferred to the statement x. This can also be shown by the following flowchart,




Qus. 06 : Write down about the else if ladder.

Ans. : The else if ladder : When a multipath decision is involved then we use else if ladder. A multipath decision is a chain of ifs in which the statement associated with each else is an if. It takes the following general form,



This construct is known as the else if ladder. The conditions are evaluated from the top, downwards. This can be shown by the following flowchart,








ক্রেডিট: তন্ময় চক্রবর্তী

1 comments:

Tanmay Chakrabarty বলেছেন...

Well, you have mentioned the credit. Thats ok, but you could have told once that you are about to copy all my notes here as a book.

Its not a book, its my notes on C programming based on the book of C programming by E. Balaguruswamy.

Ok. Bye.

01. Overview of C

02. Constants, Variables & Data Types

03. Operators & Expressions

04. Managing I/O Operations

05. Decision Making & Branching

06. Decision Making & Looping

07. Arrays

08. User Defined Functions

Comment Please

মন্তব্য..

সবিস্তার সূচীপত্র
টেম্পলেট কাষ্টমাইজেশন - তরঙ্গ ইসলাম | তরঙ্গ ইসলাম