Skip to content

Latest commit

 

History

History
90 lines (63 loc) · 1.94 KB

File metadata and controls

90 lines (63 loc) · 1.94 KB

Chapter 3: Decision Structure

The if Statement

The if statement is used to create a decision structure, which allows a program to have one path of execution. The if statement causes one or more statement to execute only when a boolean expression is true.

The below image explain the if-else statement.

if-else statement

IMG source

Type of if Statements:

Sequence:

A sequence structure is one that comprises instructions that are to be executed sequentially one after the other, with branching off in another direction.

Selection:

A Selection(sometime it's also called decision too) structure allows for exactly one of a set of sequence to be executed. In selection, a question is asked, and depending on the answer, the program take the second action, after which program move to the next event.

Iteration:

A Iteration or Loop structure comprises a sequence that is to be executed iteratively. Program repeatedly executes the loops.

Relational Operators:

Precedence Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not Equal

if Statement writing style

Single statement example:

  if (experssion)
    statement; --> semicolon goes here

More than one statement example:

  if (experssion) {
    statement; --> semicolon goes here
  } else {
    statement; --> semicolon goes here
  }
     

Multiple statement example:

  if (experssion)
     statement;
     else if (experssion)
     statement;
     else
     statement;
     

Or (by using brackets)

  if (experssion){ --> open bracket
     statement; --> semicolon 
     } --> closing bracket 
     else if (experssion){
     statement;
     }
     else {
     statement;
     }