设为首页 加入收藏

TOP

1.4 Flow of Control (6)
2013-10-07 16:14:58 来源: 作者: 【 】 浏览:78
Tags:1.4 Flow Control

1.4.4 The if Statement

Like most languages, C++(www.cppentry.com) provides an if statement that supports conditional execution. We can use an if to write a programto count how many consecutive times each distinct value appears in the input:

  1. #include <iostream>   
  2. int main()   
  3. {   
  4. // currVal is the number we’re counting; we’ll read new values into val   
  5. int currVal = 0, val = 0;   
  6. // read first number and ensure that we have data to process   
  7. if (std::cin >> currVal) {   
  8. int cnt = 1; // store the count for the current value we’re processing   
  9. while (std::cin >> val) { // read the remaining numbers   
  10. if (val == currVal) // if the values are the same   
  11. ++cnt; // add 1 to cnt   
  12. else { // otherwise, print the count for the previous value   
  13. std::cout << currVal << " occurs "   
  14. << cnt << " times" << std::endl;   
  15. currVal = val; // remember the new value   
  16. cnt = 1; // reset the counter   
  17. }   
  18. // while loop ends here   
  19. // remember to print the count for the last value in the file   
  20. std::cout << currVal << " occurs "   
  21. << cnt << " times" << std::endl;   
  22. // outermost if statement ends here   
  23. return 0;   
  24. }  

If we give this program the following input:

  1. 42 42 42 42 42 55 55 62 100 100 100  

then the output should be

  1. 42 occurs 5 times   
  2. 55 occurs 2 times   
  3. 62 occurs 1 times   
  4. 100 occurs 3 times  

Much of the code in this programshould be familiar fromour earlier programs. We start by defining val and currVal: currVal will keep track of which number we are counting; val will hold each number as we read it from the input. What’s new are the two if statements. The first if

  1. if (std::cin >> currVal) {   
  2. // . . .   
  3. // outermost if statement ends here  

ensures that the input is not empty. Like a while, an if eva luates a condition. The condition in the first if reads a value into currVal. If the read succeeds, then the condition is true and we execute the block that starts with the open curly following the condition. That block ends with the close curly just before the return statement.

Once we know there are numbers to count, we define cnt, which will count how often each distinct number occurs. We use a while loop similar to the one in the previous section to (repeatedly) read numbers from the standard input.

The body of the while is a block that contains the second if statement:

  1. if (val == currVal) // if the values are the same   
  2. ++cnt; // add 1 to cnt   
  3. else { // otherwise, print the count for the previous value   
  4. std::cout << currVal << " occurs "   
  5. << cnt << " times" << std::endl;   
  6. currVal = val; // remember the new value   
  7. cnt = 1; // reset the counter   
  8. }  

The condition in this if uses the equality operator (the == operator) to test whether val is equal to currVal. If so, we execute the statement that immediately follows the condition. That statement increments cnt, indicating that we have seen currVal once more.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.4 Flow of Control (3) 下一篇1.4 Flow of Control (5)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)