Showing posts with label c if else. Show all posts
Showing posts with label c if else. Show all posts

Sunday, 6 October 2013

Control Structures ( if else c)

Control Structures (If else statement):

If-else and else-if c are very important conditions in C++. In this c tutorial for beginners  we are going to focus on control structures of if and if…else.

Control structures are really useful because they allow us to decide between different conditions. 
In C++, there are two types of structures:
  •  Control structure
  •  switch structure.

This tutorial discusses how if and if…else statement can be used to create one-way, two-way and multiple selections. The switch structure will be discussed in coming tutorials.

One-way selection:

If we want to decide between a single condition like “It will rain today”. There are two possibilities that it will rain today or it will not rain today. To decide between these two conditions, we use one way selection (if statement).

Syntax:

If (expression)
    Statement;

If is a reserved keyword. This syntax starts with if and futher is an expression in a parenthesis. So, if the expression value is true then statement will execute otherwise, it won’t. The expression in the parenthesis is also called decision maker. The expression is usually a logical expression. The statement following the expression is also called action statement.

If else (one way selection)


Example:

If (score >= 90)
Grade = ‘A’;    //if the expression is true then grade  is   assigned to value A otherwise not.

Two-way selection:

There are situations where we have two choose between to alternatives. For example, if a student’s attendance in his/her semester is greater or equal to 80 % then he/she will pass the course else he/she will fail. For this, we use two way selection. For this selection, C++ provides use if…else statement.

Syntax:

If (expression)
    Statement1;
else
    Statement2;

Let’s see how this structure works. If expression is true then statement1 will execute else statement2 will execute.

If else (two way selection)


Final words:

 Control structures are important in decision making among different statements.They are above stated with their syntax and examples.They can be studied from this c tutorial for beginners of shapes in c.