In sequential statements, we know that all the statements are executed in the order specified in the program one after one. However, computer can skip execution of some statements those statements will not be execute by the compiler. This skipping feature is provided by the a set of instructions called skip instructions. These skip instructions are further called Branching Statements in technical words.
In other words the statements that alter/change the sequence of execution of the instructions, written in a program, are called branching statements. These statements or skip instructions have its own types. I will describe types of branching statement in brief:
For example, if, if-else, else-if ladder and switch statement.
For example, goto, break, return and continue. We will understand all these statements with example.
If (Condition)
statement;
rest of code;
According to Flowchart, compiler will first check the condition. If the condition is true, the block of statements will execute otherwise rest of code will execute. For example
if(num%2==0)
Write: ‘Even Number’
End if
Here, "Even Number" is displayed only if the condition is true. Now, let us write some simple programs that shows the application where if-statement can be used.
Program to check whether the given number is even or not.
Process: Let num is the given number. After dividing num by 2, if the remainder is equals to zero, then num will be even number. So, the equivalent algorithm and C language code is written here.
Algorithm:
Step 1: [Read the number to check]
Read: NUM
Step 2: [Check the number]
if(NUM%2==0)
Write: ‘Number is Even’
End of if
Step 3: Exit
The C program to perform the algorithm
main()
{
int num;
printf(“Enter an integer number here:”);
scanf(“%d”,&num);
if(num%2==0)
printf(“The number num %d is EVEN”,num);
getch();
}
In other words the statements that alter/change the sequence of execution of the instructions, written in a program, are called branching statements. These statements or skip instructions have its own types. I will describe types of branching statement in brief:
Conditional Statements
The written instructions can be skipped based on a condition specified by the programmer. The branching statements that alter the sequence of execution of the program based on some condition are called conditional branching statements. They can also be called Selection Statements or Decision Statements.For example, if, if-else, else-if ladder and switch statement.
Non Conditional Statements
Sometimes, user wants to transfer the control from one point to another point during execution without any condition. These type of branching statements that transfer the control from one point to another point in a program without any condition are called unconditional branching statement or unconditional control statements.For example, goto, break, return and continue. We will understand all these statements with example.
If Condition
If or If statement is a simple selection or decision statement. When a set of statements have to be executed or skipped according to the given condition, this statement is used. Here's the syntax and flowchart of this statement:If (Condition)
statement;
rest of code;
According to Flowchart, compiler will first check the condition. If the condition is true, the block of statements will execute otherwise rest of code will execute. For example
if(num%2==0)
Write: ‘Even Number’
End if
Here, "Even Number" is displayed only if the condition is true. Now, let us write some simple programs that shows the application where if-statement can be used.
Program to check whether the given number is even or not.
Process: Let num is the given number. After dividing num by 2, if the remainder is equals to zero, then num will be even number. So, the equivalent algorithm and C language code is written here.
Algorithm:
Step 1: [Read the number to check]
Read: NUM
Step 2: [Check the number]
if(NUM%2==0)
Write: ‘Number is Even’
End of if
Step 3: Exit
The C program to perform the algorithm
main()
{
int num;
printf(“Enter an integer number here:”);
scanf(“%d”,&num);
if(num%2==0)
printf(“The number num %d is EVEN”,num);
getch();
}
Tidak ada komentar:
Posting Komentar