Free C++ Lessons (Part 4)

Lessons and tutorials in the Programming Language C.

Branching Statements (if, else, switch) :

  The if statement :

 The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false.

 An if statement includes the keyword if followed by a logical expression, which is an expression that evaluates to either true or false. follow the parentheses with the statement that you want executed if the logical expression is true.

  • An if statement has the form :

Example :

  • In this example the program tells you if the number you entered is an odd number or an even number.

Result :

  The switch statement

 A switch statement is used in place of many if statements.

  • The switch statement has the form:

Example :

  • The example above represents a calculator.

Result :

 

comments powered by Disqus
Loading