NotesVeda: Tutorials Notes and Interview question & Answer
NotesVeda: Tutorials Notes and Interview question & Answer
  • Home
  • Tutorial
  • _Programming
  • __Python
  • __C++
  • __DBMS
  • __C Language
  • __SQL
  • _Computer Basics
  • _Biology
  • _Physics
  • Interview Question
  • Contact Us
  • NPTEL
  • IT JOBS
  • INTERNSHIP
  • NOTES
  • RECIPE
HometutorialControl Statements in C++ - if-else, Loop and Continue/ Break/ goto

Control Statements in C++ - if-else, Loop and Continue/ Break/ goto

Anuranjan January 31, 2023
Here I am provide you Control Statements in C++ so that you can increase your knowledge of C++ Language and learn basic concept in Control Statements in C++ and you can prepare for your exam easily.
Control Statements in C++



C++ control statements

C++ control statements are used to control the flow of execution in a program. Some common control statements include:

  • if statement: used to conditionally execute a block of code based on the value of a Boolean expression
  • for loop: used to repeatedly execute a block of code a specified number of times
  • while loop: used to repeatedly execute a block of code while a Boolean expression is true
  • do-while loop: similar to a while loop, but the block of code is executed at least once before the Boolean expression is evaluated
  • switch statement: used to execute a block of code based on the value of an expression that is matched against a number of constant expressions.

C++ also provides the break, continue, and goto statements for additional control over the flow of execution.

C++ if if-else

The ifstatement in C++ is used to conditionally execute a block of code based on the value of a Boolean expression. The syntax for an if statement is as follows:

if (expression) {

// code to execute if expression is true

}

Example:

Here's an example of an if statement that checks if a variable x is greater than 10:

int x = 15;

if (x > 10) {

std::cout << "x is greater than 10" << std::endl;

}

This code will output "x is greater than 10" to the console because the Boolean expression x > 10 is true.

 

An if-elsestatement allows for two different blocks of code to be executed based on whether an expression is true or false. The syntax for an if-elsestatement is as follows:

if (expression) {

    // code to execute if expression is true

} else {

    // code to execute if expression is false

}

Example:

Here's an example of an if-else statement that checks if a variable x is greater than 10:

int x = 15;

if (x > 10) {

std::cout << "x is greater than 10" << std::endl;

} else {

std::cout << "x is not greater than 10" << std::endl;

}

This code will output "x is greater than 10" to the console because the Boolean expression x > 10 is true.

And if x=5, the output will be "x is not greater than 10"

C++ Loop Types 

C++ provides several types of loops that can be used to repeatedly execute a block of code. Here are some common types of loops in C++:

  • for loop: used to repeatedly execute a block of code a specified number of times. The syntax for a for loop is as follows:

for (initialization; condition; increment) {

 // code to execute

}

Here's an example of a for loop that prints the numbers from 1 to 10:

for (int i = 1; i <= 10; i++) {

std::cout << i << std::endl;

}

This code will output the numbers from 1 to 10 to the console.

 

  • while loop: used to repeatedly execute a block of code while a Boolean expression is true. The syntax for a while loop is as follows:

while (condition) {

// code to execute

}

Here's an example of a while loop that prints the numbers from 1 to 10:

int i = 1;

while (i <= 10) {

  std::cout << i << std::endl;

  i++;

}

This code will also output the numbers from 1 to 10 to the console.

 

  • do-while loop: similar to a while loop, but the block of code is executed at least once before the Boolean expression is evaluated. The syntax for a do-while loop is as follows:

do {

  // code to execute

} while (condition);

Here's an example of a do-while loop that prints the numbers from 1 to 10:

int i = 1;

do {

  std::cout << i << std::endl;

  i++;

} while (i <= 10);

This code will also output the numbers from 1 to 10 to the console.

 

  • range-based for loop : added in C++11, this loop allows for iterating over the elements of a container (such as an array or vector) in a more concise and readable manner. The syntax for a range-based for loop is as follows:

for (type variable : container) {

  // code to execute

}

Here's an example of a range-based for loop that prints the elements of an array:

int arr[] = {1, 2, 3, 4, 5};

for (int x : arr) {

  std::cout << x << std::endl;

}

This code will output the numbers 1 to 5 in the console.

All the above loops are used to control the flow of the program based on different criteria, and they are useful in different situations. The choice of which loop to use depends on the specific requirements of the task at hand.

C++ Continue/ Break/ goto statement

In C++, the break, continue, and goto statements provide additional control over the flow of execution in loops and other control structures.

  • break statement: when encountered inside a loop, the break statement causes the loop to immediately exit, skipping any remaining iterations. It can also be used inside a switch statement, the break statement causes the switch statement to exit and flow of control resumes at the next statement following the switch statement.
  • continue statement: when encountered inside a loop, the continue statement causes the current iteration of the loop to be skipped and the next iteration to begin.
  • goto statement: the goto statement is used to transfer control to a labeled statement. The labeled statement must be within the same function as the goto statement. The syntax for a goto statement is as follows:

goto label;

...

label: statement;

The gotostatement is not recommended to use in modern programming, because it can make the code hard to read and reason about.

 

Here's an example of using break and continue in a for loop that prints the numbers from 1 to 10, but skips the number 5:

for (int i = 1; i <= 10; i++) {

  if (i == 5) {

    continue;

  }

  std::cout << i << std::endl;

}

This code will output the numbers from 1 to 4, 6 to 10 to the console.

 

Here's an example of using break statement in a while loop that prints the numbers from 1 to 10, but stops when it reaches the number 5:

int i = 1;

while (i <= 10) {

  std::cout << i << std::endl;

  if (i == 5) {

    break;

  }

  i++;

}

This code will output the numbers from 1 to 5 to the console and stop the execution of the loop.

It is important to use them judiciously, since overusing break and gotostatements can make code harder to understand and maintain.


Tags
Control Statements in C++ tutorial
  • Facebook
  • Twitter

Post a Comment

0 Comments

If You Have Any Doubts, Please tell me know

Adsterra

Search This Blog

Advertisement

Popular Posts

Ads

  • June 20233
  • March 20239
  • February 202316
  • January 202326

Report Abuse

Contact Form

Name

Email *

Message *

Social Plugin

Recent Posts

3/recent/post-list

Categories

  • FAQ 1
  • computer basics 10
  • interview 12
  • notes 4
  • nptel 6
  • tutorial 23

Recent in Computer Basics

3/computer basics/post-list

Menu Footer Widget

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
Copyright (c) 2023 NotesVeda All Right Reseved
Powered by Blogger