About Me

Hello there! Before talking about qualifications and professional abilities, I would like to introduce myself. I am a Yuvraj Basnet and professional software developer and have been working in this field for 3 long years. I have finished my graduation from The University of Delhi on Computer Science Engineering I considered myself a reasonably experienced programmer with a particular interest in the human side of software development, computers are fascinating machine, but they're mostly reflection of the people using them. In the art of software development, studying code isn't enough; you have to study the people behind the software, too.

Part 1 Control Structures in C++

Part 1 Control Structures in C++

Control Structures in C++

Hi this is Yuvraj from welearncode and welcome to the another C++ programming tutorial in this tutorial we will be discussing about the concepts of control structures in C++ programs. In the previous tutorial you saw the concept of variables, datatypes, and keywords.


What are control structures ?

A control structure is a block of programming that analysis variables and chose the direction in which to go based on given parameters. Any program or algorithm can be more clear and understood by programmer if they use self-contained modules called as logic or control structures. Control structures is also known as flow of control it is used to control the flow of execution in a program. 

There are three basic types of control structures available in C++:

1. Sequence logic, or sequential flow
2. Selection logic, or conditional flow
3. Iteration logic, or repetitive flow

1. Sequence logic, or sequential flow: 

Sequential logic as the name suggests it follows a arrangement or sequential flow in which the flow depends on the series of instructions or commands which is given to the computer. Unless new instructions are given, the statements are executed in the defined sequence. The sequences may be given, by means of numbered steps explicitly. Also, implicitly follows the order in which modules are written. Most of the processing, even some complex task, we will generally follow this elementary flow pattern.


2. Selection Logic (Conditional Flow):
Selection Logic simply imply a number of parameters or conditions which decides one out of several written modules. The structures which is use these type of logic are known as Conditional Structures.
In C++ we have three types of conditional structures:

A. if statements:
The if statements either selects and performs a block of statements only if the condition is true or skips the statements if the condition is false. This is a single selection statements.

Syntax:
if (condition) { statements block; }

Example:

In this example since the boolean variable print_x is true the block gets executed therefore the value of x 20 is printed on the screen conversely in this case print_x is false therefore the block is not executed. The boolean value of false corresponds to an integer value zero therefore the conditions that have non zero value access true and the conditions that have zero x is false. Here if (0) x as if false therefore the block below it is not executed x is not printed out on the hand here if (-1) x as if true therefore the block below it is executed the value of x is printed out. It is worthwhile to mention that the if statements are one of the most practical places that the comparison our logical operators are used. In this example we print out x only if it is equal to 20 we are using the equality operator.

B. if else statement:
The if else statement is a double selection statement because it selects between two different blocks. If the condition is true it selects the first block and if the condition is false it selects the second block.

Syntax: 
if (condition) { statements block 1; }
else               { statements block 2; }

Example:

In this example if the value of x is less than 20 it selects the first block and prints x is less than 20 otherwise it prints out x is greater than 20. It is very important to note that if x is greater than or equal to 20 the first block will not be executed at all conversely if x is smaller than 20 the second block will not be executed at all.

C. if...else if...else statement:
We can also have multi selection statement using the if-else if-else statements if the condition 1 in the if statement is true statement block 1 is executed otherwise if the condition 2 in the else if statement is true then the statement block 2 is executed you could also have other conditions with other else if statements. If none of these conditions are true the code just go ahead and execute the statements reside in the else block. Note that if one of these conditions are true it will not execute any of the other conditions and block suffer it.

Syntax:
if (condition 1)  { statements block 1; }
else if (condition 2)    { statements block 2; }
else                  { statements block 3; }

Example:

In this example if x is 10 which is less than 20 the first if block prints out x is less than 20 but it will not execute any else if conditions therefore it will not print x is less than 50 or x is less than 100 even if they are terrifically true thus the if-else if-else statements make the code execute only one block among the multiple blocks.







Post a Comment

0 Comments