C--Conditional Statements

Conditionals

  Conditional expressions allow your programs to make decisions and take different forks in the road,depending on the value of variables or user input.

  C provides a few different ways to implement conditional expressions (also know as branches ) in your programs,some of which likely look familiar from Scratch.

  if (and if-else,and if-else..else)

    Use Boolean expressions to make descisions.

    if (boolean-expression)

      {

        statements;    

      }

    if the boolean-expression evaluates to true,all lines of code between the curly braces will execute in order from top-to-bottom.

    if the boolean-expression evaluates to false,those lines of code will not execute.

    if (boolean-expression)

    {

      statements;

    }

    else

    {

      statements;

    }

    if the boolean-expression evaluates to true,all lines of code between the first set of curly braces will execute in order from top-to-bottom.

    if the boolean-expression evaluates to false,all lines of code between the second set of curly braces will execute in order from top-to-bottom.

  switch statement

    Use discrete cases to make decisions

    int x = GetInt();

    switch(x)

    {

      case 1;

        printf("One!\n");

        break;

      case 2:

        printf("Two!\n");

        break;

      case 3:

        printf("Three!\n");

        break;

      default:

        printf("Sorry!\n");

    }

    C‘s switch() statement is a conditional statement that permits enumeration of discrete cases,instead of relying on Boolean expressions.

      It‘s important to break;between each case,or you will "fall throug" each case (unless that is desired behavior).

  The ternary operator (?:)

    Use to replace a very simple if-else to make your code look fancy

    cool but not a good idea

    int x;

    if (expr)

    {

      x = 5;

    }

    else

    {

      x = 5;

    }

   Next

    int x = (expr) ? 5 : 6;

      These two snippets of code act identically.  // 这两段代码片段作用相同

      The ternary operator (?:) is mostly a cute trick,but is useful for writing tricially short conditional branches.Be familiar with it,but know that you won‘t need to write it if you don‘t want to.  // 三元操作符(?:)是一个不错的技巧

原文地址:https://www.cnblogs.com/jllin/p/9926599.html

时间: 2024-11-08 22:02:33

C--Conditional Statements的相关文章

Using switch statements and the ternary operator

和JSswitch的语法一样? Switch Statements and the Ternary Operator are alternatives to if-else control structures for making decisions. The basic structure of a Switch Statement consists of the keyword switch followed by a pair of parentheses containing a va

Swift流程控制之循环语句和判断语句详解

Swift提供了所有c类语言的控制流结构.包括for和while循环来执行一个任务多次:if和switch语句来执行确定的条件下不同的分支的代码:break和continue关键字能将运行流程转到你代码的另一个点上. 除了C语言传统的for-condition-increment循环,Swift加入了for-in循环,能更加容易的遍历arrays, dictionaries, ranges, strings等其他序列类型. Swift的switch语句也比C语言的要强大很多. Swift中swi

TF-variable生成方法区别

tensorflow中生成variable有两个函数:tf.Variable和tf.get_variable. tf.Variable定义如下 class Variable(object) def __init__(self, initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None

SQL注入攻击和防御

什么是SQL注入? 简单的例子, 对于一个购物网站,可以允许搜索,price小于某值的商品 这个值用户是可以输入的,比如,100 但是对于用户,如果输入,100' OR '1'='1 结果最终产生的sql, SELECT * FROM ProductsTbl WHERE Price < '100.00' OR '1' = '1' ORDER BY ProductDescription; 这样用户可以获取所有的商品信息 再看个例子, 对于用户身份认证,需要输入用户名和密码 但是如果用户在密码里面加

学习提高你CSS技术的法则

分享一些我这几个月来理解的东西,这并不是一些代码片段或者是css小技巧,而更像是一些通用的规则或者是最佳实践之类的东西,如下: 不要让你的代码脱离你的掌控,尽量简洁 掌握基础,学习CSS技巧 保持代码的可复用性 面向对象的css Css3 了解他能做的以及你可以使用的部分 渐进增强与优雅降级 Css预处理工具 与时俱进 取长补短 熟能生巧 你想说什么呢?你准备好了?那我们继续往下吧. 1.不要让你的代码脱离你的掌控,尽量简洁 别让你的代码脱离你的掌控 这是编程的一种通用建议,不仅仅适合css.当

设计模式(一) 策略模式

 一  入门 摘自<Head First Design Patterns> chapter 1 1  飞翔的鸭子 开发一款模拟鸭子的游戏.首先设计一个鸭子母类,里面有鸭子的叫声.游泳和外形三个成员函数,然后在野鸭和红头鸭两个子类中重写继承的外形函数. 更进一步,要求鸭子会飞,应该如何设计程序呢? 2  两种方法 1)  继承 为 Duck 母类添加 fly() 成员函数,然后各个子类继承 fly() 问题: 并不是所有的鸭子都会飞,比如“煮熟的鸭子”.解决: 不会飞的鸭子自母类继承 fly()

设计模式进阶(一) 策略模式

摘自<Design Paterns_Elements of Reusable Object-Oriented Software> 上一系列偏重于入门,从本篇开启进阶系列,着重于设计模式的适用情景. 回顾入门系列 设计模式入门(一)  策略模式 1  Intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary

Matlab编程基础

平台:Win7 64 bit,Matlab R2014a(8.3) "Matlab"是"Matrix Laboratory" 的缩写,中文"矩阵实验室",是强大的数学工具.本文侧重于Matlab的编程语言侧面,讲述Matlab的基本语法,以及用Matlab语言进行程序设计.值得一提的是,Matlab从R2014a版本开始支持中文语言了! 1.基本概念 Matlab默认启动后界面: Matlab有关的文件后缀: File Extension Des

C++ 之 策略模式

1  会飞的鸭子 Duck 基类,包含两个成员函数 (swim, display):派生类 MallardDuck,RedheadDuck 和 RubberDuck,各自重写继承自基类的 display 成员函数 class Duck { public: void swim(); virtual void display(); }; class MallardDuck : public Duck { public: void display(); // adding virtual is OK

IOS学习路径

iOS Developer Roadmap Start your journey today! Where Do I Start? Becoming an iOS developer is a lot of fun and a valuable skill in today's market. But there's a catch: It isn't as easy as it looks. Even experienced programmers are challenged by deve