C++学习系列三:Statements and Flow Control||Functions||Overloads and Templates||Name Visibility

  • Statements and flow control

    A simple C++ statement is each of the individual instructions of a program, always end with a semicolon(😉, and are executed in the same order in which they appear in a program.

    1. Selection Statements: if and else

      The if keyword is used to execute a statement or block, if, and only if, a condition is fulfilled. Its syntax is :

      if (x == 100)
          cout << "x is 100";
      if (x == 100)
      {
          cout << "x is  ";
          cout << x;
      }
      

      Selection statements with if can also specify what happens when the condition is not fulfilled, by using the else keyword to introduce an alternative statement. Its syntax is :

      if (x == 100)
          cout << "x is 100";
      else 
          cout << "x is not 100";
      

      Several if + else structures can be concatenated with the intention of checking a range of values.

      if (x > 0)
          cout << "x is positive";
      else if (x < 0)
          cout << "x is negative";
      else
          cout << "x is 0";
      
    2. Iteration Statements (loops)

      Loops repeat a statement a certain number of times, or while a condition is fulfilled. They are introduced by the keywords while, do, and for.

      • The while loop

        The simplest kind of loop is the while-loop. Its syntax is:

        while (expression) statement

        The while-loop simply repeats statement while expression is true. If, after any execution of statement, expression is no longer true, the loop ends, and the program continues right after the loop.

        A thing to consider with while-loops is that the loop should end at some point, and thus the statement shall alter values checked in the condition in some way, so as to force it to become false at some point.

      • The do-while loop

        A very similar loop is the do-while loop, whose syntax is :

        do statement while (condition);

        It behaves like a while-loop, except that condition is evaluated after the execution of statement instead of before, guaranteeing at least one execution of statement, even if condition is never fulfilled.

      • The for loop

        The for loop is designed to iterate a number of times. Its syntax is:

        for (initialization; condition; increase) statement;

        Like the while-loop , this loop repeats statement while condition is true. But, in addition, the for loop provides specific locations to contain an initialization and an increase expression, executed before the loop begins the first time, and after each iteration, respectively.

        Therefore, it is eapecially useful to use counter variables as condition.

        It may be useful to execute more than a single expression as any of initialization, condition, or statement.

        Range-based for loop

        The for-loop has another syntax, which is used exclusively with ranges:

        for (declaration : range) statement;

        This kind of for loop iterates over all the elements in range, where declaration declares some variable able to take the value of an element in this range.

        Ranges are sequences of elements, including arrays, containers, and any other type supporting the functions begin and end.

      • Jump statements
        1. The break statement

          break leaves a loop, even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end.

        2. The continue statement

          The continue statement causes the program to skip the rest of the loop in the current iteration, as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.

        3. The goto statement

          goto allows to make an absolute jump to another point in the program. This unconditional jump ignores nesting levels, and does not cause any automatic stack unwinding(堆栈解退).

          The destination point is identified by a label, which is then used as an argument for the goto statement.

          A

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值