Top 10 Control Flow Statements Definitions
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
Normally, statements in a program are executed one after the other in the order in which they are written. This process is called sequential execution. Various Java statements, which we will soon discuss, enable the programmer to specify that the next statement to execute is not necessarily the next one in sequence. This is called transfer of control and is done using control statements.
Control statements include if-else statements, switch statements ,while loops, do-while loops, and for loops.. in this tutorial we will be looking at if-else statements. sometimes when we want to solve a problem, decision making is needed, lets say we want to write a program to notify the user whether or not to wear a jacket depending whether its a rainy day or not.. this will be done using if-else statement:
pseudo code:
if it is raining
then wear a jacket
else
then a jacket is not needed
As you may have guessed, the compiler first looks at the first statement and checks the condition, if the condition is fulfilled then the next block of statements will be executed and the user will get a notification to wear a jacket, else if the condition of the first statement is not fulfilled, then the compiler will jump on to the block of the next statement which is in this case the else statement and the user will get a notification that a jacket is not needed...so as you see here, our algorithm makes a decision whether to execute a statement or not by using if-else statements..
if-else structure looks like this:
if( condition to be checked )
statement1
else
statement2
A programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program.
The conditional-expression will equate to either true or false. If the conditional-expression is true, then the statement(s) will be executed; if the conditional expression is false, then the statement(s) will not be executed. Afterward, the computer will continue with the execution of the next statement after statement(s).
When programming you often want to control the order in which the statements will be executed. This is done by using Control Flow Statements, and some of those are the if-else statements. The if-else statements enable your program to selectively execute other statements, based on some criteria.
This is used to decide whether to do something at a special point, or to decide between two courses of action. The following test decides whether a student has passed an exam with a pass mark of 45:
if (result >= 45)
printf("Pass\n");
else
printf("Fail\n");
