'Tips/C++ Eng'에 해당하는 글 26건

The for loop executes a block of code repeatedly until the specified expression evaluates to false. You can define a for loop as follows.

for ([initializer(s)]; [condition]; [iterator]) 
{
   // code to repeat goes here
}

The [initializer(s)] portion is used to initialize a value, or values, as a counter for the loop. On each iteration, the loop checks that the value of the counter is within the range to execute the for loop, specified in the [condition] portion., and if so, execute the body of the loop.   At then end of each loop iteration, the[iterator] section is responsible for incrementing the loop counter.

The following code example shows how to use a for loop to execute a code block 10 times.

for Loop
for (int i = 0 ; i < 10; i++) 
{
    // Code to execute.
}
In this example, i = 0; is the initializer, i < 10; is the condition, and i++ is the iterator. 


This contents come from edx : Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Introducing Classes  (0) 2015.11.28
Storage classes and Scope  (0) 2015.11.23
Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04
Introducing Operators  (0) 2015.11.01

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

The C++ conditional operator is also known as a ternary operator because it takes three operands.  How this operator functions is somewhat similar to an if statement or a switch statement, but in a more compact form and for one single Boolean value with one of two possible outputs.   That is to say, the first operand is evaluated as a Boolean result.  If the result is true, then the second operand will be the one evaluated.  Otherwise, the third operand will be evaluated.   A sample helps amplify this.

#include <iostream> 
using namespace std; 
int main() 

     int i = 1, j = 2; 
     cout << ( i > j ? i : j ) << " is greater." << endl; 
}

In this example, we have two integer variables, i and j which are initialized to 1 and 2 respectively.  The ternary operator is embedded inside the cout statement and essentially follows this pattern:

  1. it checks whether i is greater than j
  2. it outputs the proper numeric value along with is greater.

In the code example here, j is greater than i so the condition evaluates to false and the value for j (2), is output to the console along with the text is greater.  In other words, the output is "2 is greater."  If i was 5 and j was 2, the output would be, "5 is greater." 

i > j ? i : j where i is greater than j then the bold value is selected

i > j ? i : j where j is greater than i, then the bold value is selected

This contents come from : Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Storage classes and Scope  (0) 2015.11.23
Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04
Introducing Operators  (0) 2015.11.01
Unions Quiz  (0) 2015.10.27

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

switch Statments

Tips/C++ Eng 2015.11.04 00:13

If there are too many else if statements, code can become messy and difficult to follow. In this scenario, a better solution is to use a switch statement. The switch statement simply replaces multiple else ifstatements. The following sample shows how you can use a switch statement to replace a collection of else if clauses.

switch Statement

char response = 'y';
switch (response)
{
   case 'y':
      // Block of code executes if the value of response is y.
      break;
   case 'Y':
      // Block of code executes if the value of response is Y.
      break;
   case 'n':
      // Block of code executes if the value of response is n.
      break;
   default:
      // Block executes if none of the above conditions are met.
      break;
}

Notice that there is a block labeled default:. This block of code will execute when none of the other blocks match.  The default block is optional.

In each case statement, notice the break keyword. This causes control to jump to the end of the switchafter processing the block of code. If you omit the break keyword, the application may not perform as you anticipate.  In other languages, such as C#, omitting the break; keyword will cause the code to no longer compile. 

Without the break statement, the code will "fall through" to the remaining cases until it encounters a break statement.   Be very careful in using fall through logic in your switch statements.  The most common use for a fall through scenario is when you want to handle multiple cases with a single statement or set of statements.

If you are coming from another programming language, such as C#, that also uses the switch statement, you might notice that in the C# language, you can use string values in your switch statements and don't have to use integers or enumerated types.  C++ switch statements support the following data types as expressions:

    • intrinsic data types such as int or char
    • enumerations

This contents come from Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04
Introducing Operators  (0) 2015.11.01
Unions Quiz  (0) 2015.10.27
Enumerations  (0) 2015.10.27

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret