lec2-flow of control

lec2 Notes: Flow of Control

1 Motivation

  • Normally, a program executes statements from first to last. The first statement is executed, then the second, then the third, and so on, until the program reaches its end and terminates.A computer program likely wouldn't very useful if it ran the same sequence of statements every time it was run.

2 Control Structures

  • Control structures are portions of program code that contain statements within them and, depending on the circumstances, execute these statements in a certain way. There are typically two kinds: conditions and loops
  • Conditionals
    In order for a program to change its behavior depending on the input, there must a way to test that input. Conditionals allow the program to check the values of variables and to execute (or not execute) certain statements. C++ has if and switch-case conditional structures
  • Operators

, >=, <, <=, ==, !=, &&, ||, !

  • if, if-else and else if
#include <iostream>
using namespace std;

int main() {
    int x = 6;
    int y = 2;
    
    if (x > y)
        cout << "x is greater than y\n";
    else if (y > x)
        cout << "y is greater than x\n";
    else
        cout << "x and y are equal\n";
        
    return 0;
}
  • switch-case
#include <iostream>
using namespace std;

int main() {
    int x = 6;
    
    switch(x) {
        case 1:
            cout << "x is 1\n";
            break;
        case 2:
        case 3:
            cout << "x is 2 or 3";
            break;
        default:
            cout << "x is not 1, 2, or 3";
    }
    
    return 0;
}
  • loops
#include <iostream>
using namespace std;

int main() {
    int x = 0;
    
    while (x < 10)
        x = x + 1;
    cout << "x is " << x << "\n";
    
    return 0;
}
do {
    statement1
    statement2
} while(condition);
#include <iostream>
using namespace std;

int main() {
    for (int x = 0; x < 10; x = x + 1)
        cout << x << "\n";
        
    return 0;
}
  • Nested Control Structures
#include <iostream>
using namespace std;

int main() {
    int x = 6;
    int y = 0;
    
    if (x > y) {
        cout << "x is greater than y\n";
        if (x == 6)
            cout << "x is equal to 6\n";
        else
            cout << "x is not equal to 6\n";
    } else 
        cout << "x is not greater than y\n";
        
    return 0;
}
#include <iostream>
using namespace std;

int main() {
    for (int x = 0; x < 4; x = x + 1) {
        for (int y = 0; y < 4; y = y + 1)
            cout << y
        cout << "\n";
    }
    
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,285评论 3 20
  • 从小以来,我就对钱没有太多的渴求,不是说家庭是多么的富裕,而是作为普通家庭的孩子,没有太多的概念,反正不愁吃穿,还...
    杨艺瀚阅读 121评论 0 0
  • 默默地说话,默默地写字,默默地看书,2016的下半年悄悄地来了,忙着搬家,忙着培训,忙着似乎永远忙不完的事。很多认...
    陈小护的日常阅读 257评论 0 0
  • 最近的家庭电话太多,多得没有耐心一一接听回电再接听,无非都是鸡毛蒜皮的小事情完全都是可以解决好的,为什么...
    南山默少阅读 105评论 0 0