Thursday 17 October 2013

Break And Continue Statements

Break And Continue Statements:

In this C tutorial for beginners, we are going to discuss a very useful facility of C++ which are break and continue statements.

Break statement in C:

Previously, we have seen that break statement was used in a switch structure. Similarly, you can use break statement in while, for and do…while loops. When a break statement executes in a repetition structure, it immediately exits from the repetition structure. There are times when our requirement is met but the loop still doesn’t finish so, we use this break statement condition. This make our code efficient and less time consuming.

It is used for two purposes:

  • To exit early from the loop.
  • To skip the remainder of the switch structure.
The use of break in c can eliminate the use of many (flags) variables. However, the break statements in c should be used very carefully as the excessive use of it will make your code an ugly code. Use it just when you’re sure that what you wanted from the code has been achieved.

Example of break in c:

 int sum=0, num=0;
cin >> num;
while(cin)
{
    if (num<0)
    {
        cout << “Negative number found\n”;
        cin>>num;
        break;
}
    sum+=num;
    cin>>num;
}

Continue statement in c:

Continue statement in c is used in while, for and do…while loop. When continue statement executes, it skips all remaining statements in loop for that certain iteration and proceeds with the next iteration of the loop. 

Let’s take an example of continue in c:


int sum=0, num=0;
cin >> num;
while(cin)
{
    if (num<0)
    {
        cout << “Negative number found\n”;
        cin>>num;
        continue;
}
    sum+=num;
    cin>>num;
}

In this program, if the num is negative then it doesn’t execute the further statements and proceeds with the further iteration else it does.

Final words:

 In this c tutorial for beginners of shapes in c we discussed about break and continue statements in detail.
These are very important and commonly used conditions in C++. You can understand and learn it here.

Switch In C++

Switch In C++:

There are two branches or structures in C++ and we studied the first structure in our previous tutorial which was implemented with if and if…else statements. The second selection structure that we are going to study in this C tutorial for beginners is called the switch in c structure. This structure doesn’t require a logical evaluation.

Syntax:

         Switch (expression)
           {
                   case value1:
                       Statements1;
                       break;
                   case value2:
                       Statements2;
                       break;
                       .
                       .
                       .
                   case valuen:
                       Statementsn;
                       break;
                   default:
                       statements
              }





In C++, switch, case, break and default are reserved keywords. In this structure first the expression is evaluated and then that value gained by expression is used to perform action specified in the statements of the switch case in c that satisfies.
 The expression can be a constant or identifier. The expression is also called selector. Only one switch case c is executed and if none of the case satisfies then default case is executed. It is not important that break statement must appear after every each statement in the case.

Rules:

Below are some rules that the c switch statements follows:
When the value of the expression satisfies some case then the statements in that case execute until a break statement is reached or the end of the switch structure is reached.
If the value doesn’t match any case then the statements in default label executes. But if value of the expression doesn’t match any case and there is no default value then the entire switch structure is skipped.
A break statement causes an immediate exit from the switch structure.

Comparison between if…else and switch statement c:

Switch structure is more classified and easy to handle structure than if…else structure. In switch structure, user can easily track his/her cases.

Final Words:

C switch statement has been discussed in this c tutorial for beginners.switch statement in c is important  in C as it used to select between different expressions , and execute the one most appropriate under certain conditions.

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.

Logical Operators

 Logical Operators:

C++ provides us the facility of combining logical expression and evaluate their behavior. So, in this C tutorial for beginner, we are going to discuss  logical operators in c. We are going to discuss how to form and evaluate logical expressions and see how they work.

Below are three logical operators in C++ that we normally use:

Operator        Description

  1. !                    not
  2. &&                and
  3. ||                    or

Logical operators in c take two logical values, combine them and finally gives one logical value.

Expression            !expression

True (nonzero)            false (0)
False (0)                     true (1)

When you put not “ ! ” operator with expression it changes
  1. True to false
  2. False to true
  3. Nonzero to zero
  4. Zero to one

Let’s see some examples of “not” operator:

  1. ! (‘B’ > ‘A’)          !(true)             false
  2. !( 1 <= 10 )        !(true)             false
  3. !(1 != 1)            !(false)             true

Let’s see some examples of “and” operator:

  1. ( 100 >= 101 && 100 <= 101)                          false
  2. ( “shapes” == “shapes” && “me” == “me”)        true
  3. (!false && true)                                                true
  4. (1>0 && 1<0)                                                   false

Let’s see some examples of “or” operator:

  1. ( 100 >= 101|| 100 == 101)                             false
  2. ( “shapes” > “shapes” || “me” == “me”)            true
  3. (!false || !true)                                                true
  4. (1==0 || 1<0)                                                  false


Final words:

Logical operators are important in C++ programming. They have been discussed in detail in this c tutorial for beginners.
We hope that this post will be help full for our viewers.

Relational Operators in C++

Relational Operators In C++:

To make decisions in a C++ program, you must be able to compare conditions. So, in this C tutorial for beginners we’re going to discuss relational c operators. For example, if we want to see who has the highest, average and lowest marks in some class then we need to develop such algorithm which would require comparing marks of students so, we are going to need relational c operators for that.

In C++, a condition is represented by a logical expression. It would be either true or false. And true and false are Boolean values.

Consider:

            i>j        // i and j have some int values stored in them
It is a logical expression so, it would either have a true value or a false. “ > ” is a relational c operator. And relational operators allow you to make comparisons in a C++ program.
C++ includes six relational operators.

Operator                Description

  1. ==                      equal to
  2. !=                       not equal to
  3. <                        less than
  4. >                        greater than
  5. <=                      less than or equal to
  6. >=                      greater than or equal to

Note:

 “ ==  “ and “ =  “ are different signs. “ ==  “ is a double equal sign known as equality operator ad used to check equality of operands whereas “ = “ is known as assignment operator.

Expression            Result

  1. 1 < 10                true
  2. 2 == 2                true
  3. 4 > 11                 false
  4. 6.1 <= 7.9           true
  5. 5.8 >= 7.3           false

Relational operators in c can also be used to compare char values.

For example:

‘R’ > ‘T’            False
‘+’ < ‘*’             False
‘A’ <= ‘a’           False

Relational operators in c can also be used to compare strings.
  • string str1 = “Hello”
  • string str2 = “Hi”
  • string str3 = “Hello”
  • string str4 = “Air”

  • str1 < str2         true
  • str1 == str3       true
  • str1 > str 2        false
  • str < “An”          true

Final words:

 Relational operators are very important in C++ programming. They are the fundamental tools for comparing different things. In this C tutorial for beginners they have been discussed in detail with examples.