Thanks to visit codestin.com
Credit goes to www.packtpub.com

Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
The C++ Workshop
The C++ Workshop

The C++ Workshop: Learn to write clean, maintainable code in C++ and advance your career in software engineering

Arrow left icon
Profile Icon Dale Green Profile Icon Kurt Guntheroth Profile Icon Shaun Ross Mitchell
Arrow right icon
₹799.99 ₹2621.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (7 Ratings)
eBook Feb 2020 606 pages 1st Edition
eBook
₹799.99 ₹2621.99
Paperback
₹3276.99
Subscription
Free Trial
Renews at ₹800p/m
Arrow left icon
Profile Icon Dale Green Profile Icon Kurt Guntheroth Profile Icon Shaun Ross Mitchell
Arrow right icon
₹799.99 ₹2621.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (7 Ratings)
eBook Feb 2020 606 pages 1st Edition
eBook
₹799.99 ₹2621.99
Paperback
₹3276.99
Subscription
Free Trial
Renews at ₹800p/m
eBook
₹799.99 ₹2621.99
Paperback
₹3276.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

The C++ Workshop

2. Control Flow

Overview

This chapter presents various tools and techniques that are used to control the flow of execution throughout applications. This includes, but is not limited to: if statements, switch statements, and various loops. We will also look at how we control the lifetime of our applications using these techniques, and how they are to be used efficiently. The chapter will end with the creation of a number guessing that that will implement various loops and conditional statements.

Introduction

In the first chapter, we covered the absolute essentials of C++ and looked at the key components of a C++ application. We looked at how applications run, how they're built, and how we can get information in and out of them with some basic I/O. Up until this point, the applications we've built have mainly run sequentially; that is, the code we've written has been executed line by line, sequentially. While that's great for demonstration purposes, this generally isn't how real-world applications work.

In order to represent logical systems correctly, we need to be flexible in what we do and when. For example, we may only want to perform a certain action if a given statement is true or to return to an earlier piece of code again. Manipulating execution in this manner is known as control flow (or program flow), and is the topic of this chapter.

To begin with, we are going to look at the humble if statement, one of the most fundamental logic statements...

if/else

One of the most basic, yet most important, control flow statements is if. This simple keyword is at the heart of all logic, allowing us to perform a given action only if a specified condition is true. By chaining these if statements together in creative ways, we can model any logical system.

The syntax for an if statement is as follows:

    if (condition) { // do stuff. }

If the statement we use as our condition resolves to true, then the code within the curly braces will be executed. If the statement is false, then it will be skipped. Our condition can be anything that can be either true or false. This can be something simple, such as checking the value of a Boolean, or something more complex, such as the result of another operation or function.

We also have the else statement. This allows code to be executed if, and only if, a preceding if statement's condition evaluates to false. If the condition evaluates to true, however, and the if...

switch/case

As we've seen, we can use if/else to perform certain actions based on which conditions are true. This is great when you're evaluating multiple conditional statements to determine flow, such as the following:

    if (checkThisCondition)
    {
        // Do something ...
    }
    else if (checkAnotherCondition)
    {
        // Do something else ...
    }

When we're evaluating the different possibilities of a single variable, however, we have a different statement available to us: the switch statement. This allows us to branch in a similar way to an if/else statement, but each branch is based on a different possible value of a single variable that we're switching on.

A good example of where this would be suitable is the menu application we created...

Loops

Alongside if statements, loops are among the most fundamental of programming concepts. Without loops, our code would execute by running through our logic statements one by one and then ending. That's how our applications have worked so far; however, in reality, this really isn't practical. Systems tend to consist of many moving parts, and code execution will jump all around the code base to where it's needed.

We've seen how this can be achieved by creating branches in our code where statements can be evaluated, and we do different things based on the outcome. Another way we do this is via loops. Loops allow us to rerun sections of code, either a set or indefinite number of times depending on which one we choose. We're going to be looking at three: while, do while, and for loops.

while

A while loop is one of the most basic loops in your arsenal and is usually the outermost loop in an application. When execution enters a while loop it typically...

break/continue

Having the ability to loop sections of code is very important, but it has to be used carefully. We've seen that it's possible to create loops that never end, and another concern is ensuring that they're used efficiently. So far, the loops we've looked at have been small, and we've been happy to see them run through in their entirety. But what if we needed more control over our loops, perhaps to end one early? Thankfully, we have two important keywords to help us with that—break and continue.

break

break is a C++ keyword that will exit the current loop, with execution jumping to the next section of code if there is any. This keyword works with the different types of loop that we've covered so far, and we can demonstrate it nicely using a simple counting application, as shown in the following snippet:

// Break example.
#include <iostream>
#include <string>
int main()
{
    std::cout <<...

Summary

In this chapter, we've learned about program flow and how we can manipulate the flow of execution through our applications. This is fundamental for representing logical systems.

We started by looking at basic if/else statements. These allow us to branch our code based on conditions and are one of the most fundamental ideas in programming. With this branching ability, we're able to replicate logical systems and behaviors by controlling the flow of execution through our application. We then looked at some alternatives to the basic if/else statement, such as switch and ternary statements.

Next, we looked at a number of different loops. We started with while and do while loops; loops that run indefinitely so long as the condition they're checking is true. We then looked at for loops, which run for a set number of iterations. Finally, we looked at range-based loops, which are useful for iterating over collections. We ended by looking at how we can...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Gain a solid understanding of the syntax and anatomy of C++
  • Implement best practices when building high-performance C++ programs
  • Prepare for real-world development tasks by tackling engaging activities

Description

C++ is the backbone of many games, GUI-based applications, and operating systems. Learning C++ effectively is more than a matter of simply reading through theory, as the real challenge is understanding the fundamentals in depth and being able to use them in the real world. If you're looking to learn C++ programming efficiently, this Workshop is a comprehensive guide that covers all the core features of C++ and how to apply them. It will help you take the next big step toward writing efficient, reliable C++ programs. The C++ Workshop begins by explaining the basic structure of a C++ application, showing you how to write and run your first program to understand data types, operators, variables and the flow of control structures. You'll also see how to make smarter decisions when it comes to using storage space by declaring dynamic variables during program runtime. Moving ahead, you'll use object-oriented programming (OOP) techniques such as inheritance, polymorphism, and class hierarchies to make your code structure organized and efficient. Finally, you'll use the C++ standard library?s built-in functions and templates to speed up different programming tasks. By the end of this C++ book, you will have the knowledge and skills to confidently tackle your own ambitious projects and advance your career as a C++ developer.

Who is this book for?

This Workshop is for anyone who is new to C++ who wants to build a strong foundation for C++ game programming or application development. Basic prior knowledge of data structures and OOP concepts, as well as experience in any other programming language, will help you grasp the concepts covered in this book more easily.

What you will learn

  • Understand how a C++ program is written, executed, and compiled
  • Efficiently work with the essential C++ data types and variables
  • Build your own C++ applications by writing clear and error-free code
  • Grasp the core principles behind object-oriented programming
  • Simplify your code by using templates and the standard library
  • Debug logical errors and handle exceptions in your program

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 07, 2020
Length: 606 pages
Edition : 1st
Language : English
ISBN-13 : 9781838988364
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 07, 2020
Length: 606 pages
Edition : 1st
Language : English
ISBN-13 : 9781838988364
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
₹4500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts
₹5000 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 14,821.97
The C++ Workshop
₹3276.99
C++ High Performance
₹4468.99
Modern C++ Programming Cookbook
₹7075.99
Total 14,821.97 Stars icon

Table of Contents

13 Chapters
1. Your First C++ Application Chevron down icon Chevron up icon
2. Control Flow Chevron down icon Chevron up icon
3. Built-In Data Types Chevron down icon Chevron up icon
4. Operators Chevron down icon Chevron up icon
5. Pointers and References Chevron down icon Chevron up icon
6. Dynamic Variables Chevron down icon Chevron up icon
7. Ownership and Lifetime of Dynamic Variables Chevron down icon Chevron up icon
8. Classes and Structs Chevron down icon Chevron up icon
9. Object-Oriented Principles Chevron down icon Chevron up icon
10. Advanced Object-Oriented Principles Chevron down icon Chevron up icon
11. Templates Chevron down icon Chevron up icon
12. Containers and Iterators Chevron down icon Chevron up icon
13. Exception Handling in C++ Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(7 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




RAJA MOHAN REDDY May 16, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Awesome book. It is really recommendated for the starters. They have explained about SOLID principle and important data structures topis like queue, and stack vector.
Amazon Verified review Amazon
Grant A. Sep 03, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
C++ and unreal engine 4 = choose this book if you wanna learn the basics
Amazon Verified review Amazon
Martin Sinnaeve Sep 26, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Clear and simple start in the C++ language, a steady built up in knowledge, and not focusing on any IDE.
Amazon Verified review Amazon
Luis Carlos Absalon Rojas Torres Dec 10, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Ja seguia o autor no QUORA onde sempre dava aquelas respostas sobre c++, ai quando fiquei sabendo que lançou um livro de c++ geral comprei. Olha conteudo muito bom hein!
Amazon Verified review Amazon
absk198 Jun 13, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good read. Easy to follow. Quite useful for beginner and intermediate level developers
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.

Modal Close icon
Modal Close icon