What is a program control structure? if else statement

Control Structures are the structures that govern the order in which program statements are performed. There are 3 types of control structures. They are:

Sequence

It is a sequence of program instructions that must be executed in the order they are given (not dependent on any program conditions). Instructions are placed in a predetermined order (like to a queue in a movie theater), and the CPU only executes the next instruction after the previous one has been completed(C never comes before B).

Sequential Statement in protramming
Fig: Sequential Statement

Selection

It is a sequence of instructions that are conditionally executed, that is, they are performed dependent on a condition that can be true or false. If conditions, if else conditions, if else if conditions, nested if else conditions, and switch case conditions are all common selection logic.

If condition:

If condition is employed when there is only one condition and one action in a particular problem. The statement will be executed if the supplied condition is true, regardless of whether it is true or false. Otherwise, the condition is exited by the control.

if condition flowchart
Fig: if condition

Example: Check the number is positive.

Step 1: Start
Step 2: Input N
Step 3: If (n>0), output is positive
Step 4: End
if condition algorithm flowchart image

If else condition:

If the problem has only one condition but two possible solutions, this condition is employed. Here, if the condition is true, statement 1 will be executed; otherwise, statement 2 will be executed.

if else condition flowchart
Fig: if else condition

Example: Find the greatest between 2 numbers.

Step 1: Start
Step 2: Input X, Y
Step 3: If (X>Y)
                 output X is greatest
             else
                 output Y is greatest
Step 4: End
if else condition algorithm flowchart image

If else if condition:

If the presented problem has more than one interrelated condition with their corresponding actions, we can use this condition, also known as ladder type if else. If condition 1 is true, statement 1 is executed based on a check. If condition 2 is true, statement 2 is executed, and so on for the remaining conditions. The last statement will be executed if all criteria are false.

if else if condition  flowchart
Fig: if else if condition

Example: Find the greatest among 3 numbers.

Step 1: Start
Step 2: Input P, Q, R
Step 3: If (P>Q && P>R)
                 output P is greatest
             else if (Q>R)
                 output Q is greatest
             else
                 output R is greatest
Step 4: End
if else if condition algorithm flowchart image 

Nested if else condition:

A nested if else condition is an entire if-else statement put within the body of another if else statement's if part or else part. This condition is used to check a condition that is inside another condition in the same program at the same time in order to make a decision.

Nested if condition flowchart
Fig: Nested if condition

Example: Find the greatest 2 positive numbers.

Step 1: Start
Step 2: Input X, Y
Step 3: If (X>0 && Y>0)
             {
               if (X>Y)
                  output X is greatest
               else
                  output Y is greatest
             }
             output X or Y may be negative
Step 4: End
Nested if condition algorithm flowchart image 

Switch case condition:

In this type of case situation, we can apply the Switch case condition if the presented problem contains one condition and more than two actions. It is a set of numerous branching statements that compare the variable's value to the case value and then execute the instructions associated with it. The default statement will be executed if any expression does not match any of the case values.

switch case condition flowchart
Fig: switc condition

Example: Write an algorithm and a flowchart that prints the day for each integer number from 1 to 7.

Step 1: Start
Step 2: switch (day)
             {
               case 1: output Sunday
                  break
               case 2: output Monday
                  break
               case 3: output Tuesday
                  break
               case 4: output Wednesday
                  break
               case 5: output Thursday
                  break
               case 6: output Friday
                  break
               case 7: output Saturday
                  break
               default: output out of range
             }
Step 3: end
switch case condition algorithm flowchart image

Post a Comment

Please do not enter any spam link in the comment.

Previous Post Next Post