美文网首页
lec2-flow of control

lec2-flow of control

作者: leea_a009 | 来源:发表于2018-11-14 23:42 被阅读0次

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;
}

相关文章

  • lec2-flow of control

    lec2 Notes: Flow of Control 1 Motivation Normally, a prog...

  • 2022-11-01 Release The Past

    “To control your life, control your mind. To control your...

  • control

    我要控制我自己,不然这世界将进入地狱

  • control

    //屏幕宽度的宏 #define SCR_W [UIScreen mainScreen].bounds.size....

  • control

    - (void)viewDidLoad { [selfuidata]; dataSource = [[NS...

  • Control

    我察觉到执着的来源是这样:在任何一个通道上倾泻过多的能量。 这让我明了长久以来自己对爱情和命运的执着。它们让我伤痕...

  • control

    1.生成木马软件 msfvenom -p windows/meterpreter/reverse_tcp LHOS...

  • CONTROL

    是不是不该再往前随心而走再走 我怕我的理智与意念都会脱手 我怕一个人走两个人的距离 线不够拉扯我怕你往别的方向出发...

  • In Control

    原创 (简书名:天行鹤)|007-12372|7月21日|第6篇|心情随笔 本文960字,大概需要阅读2分钟 I ...

  • Control

    今天看了篇文章说富豪为什么那么热爱工作? 比尔·盖茨今年64岁,在1995年,他40岁的时候就成为了世界首富。目前...

网友评论

      本文标题:lec2-flow of control

      本文链接:https://www.haomeiwen.com/subject/lntefqtx.html