Decision Making in C

Similar documents
Following is the general form of a typical decision making structure found in most of the programming languages:

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Flow Control. CSC215 Lecture

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture 5 Tao Wang 1

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Chapter 4: Making Decisions

Software Design & Programming I

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

SELECTION. (Chapter 2)

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

LECTURE 04 MAKING DECISIONS

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

5. Selection: If and Switch Controls

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

In this lab, you will learn more about selection statements. You will get familiar to

Boolean Expressions (Conditions)

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Introduction to Programming Using Java (98-388)

INTRODUCTION TO COMPUTER SCIENCE - LAB

CS302: Self Check Quiz 2

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Making Decisions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

03 Features of C#, Part 2. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

CGS 3066: Spring 2015 JavaScript Reference

Programming and Data Structures

bool bool - either true or false

Control Structures in Java if-else and switch

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

(Not Quite) Minijava

Chapter 2: Functions and Control Structures

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Decision Making -Branching. Class Incharge: S. Sasirekha

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

Java Bytecode (binary file)

Control Structures in Java if-else and switch

LAB 5: SELECTION STATEMENTS

CP215 Application Design

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Decision Structures. Chapter 4

Object Oriented Software Design

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter 4: Control structures. Repetition

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CSC Java Programming, Fall Java Data Types and Control Constructs

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

5. Control Statements

C-LANGUAGE CURRICULAM

Chapter 4: Control structures

Pace University. Fundamental Concepts of CS121 1

Functions. Lecture 6 COP 3014 Spring February 11, 2018

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Introduction to Bioinformatics

Announcements. HW 0 due tonight (check that it compiles on CSE labs) (make sure you submit the code part)

IEEE LANGUAGE REFERENCE MANUAL Std P1076a /D3

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Fundamentals of Programming Session 13

Decisions. Arizona State University 1

CSCI 1061U Programming Workshop 2. C++ Basics

Working with JavaScript

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Computer Programming: C++

204111: Computer and Programming

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Quiz 1: Functions and Procedures

COMS 469: Interactive Media II

CSC 1300 Exam 4 Comprehensive-ish and Structs

egrapher Language Reference Manual

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

CHAPTER : 9 FLOW OF CONTROL

The following expression causes a divide by zero error:

[301] JSON. Tyler Caraza-Harter

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

Programming Lecture 4

Computational Expression

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Programming Lecture 4

COMP 401 Spring 2014 Midterm 1

Chapter 2 Control in Programming. lecture 2 1

Transcription:

Decision Making in C Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Following is the general form of a typical decision making structure found in most of the programming languages: C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value. C programming language provides following types of decision making statements. if statement An if statement consists of a boolean expression followed by one or more statements. The syntax of an if statement in C programming language is: If the boolean expression evaluates to true, then the block of code inside the if statement will be executed. If boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed. 1 P a g e

Example When the above code is compiled and executed, it produces the following result: if...else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. The syntax of an if...else statement in C programming language is: If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed. C programming language assumes any non-zero and non-null values as true and if it is either zero or null then it is assumed as false value. 2 P a g e

3 P a g e

The if...else if...else Statement An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement. When using if, else if, else statements there are few points to keep in mind: 's or else's will be tested. The syntax of an if...else if...else statement in C programming language is: Example: Below is a code as a example. When the following code is compiled and executed, it produces the following result: 4 P a g e

Nested if statements It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s). The syntax for a nested if statement is as follows: 5 P a g e

If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed. You can nest else if...else in the similar way as you have nested if statement. When the above code is compiled and executed, it produces the following result: You can nest else if...else in the similar way as you have nested if statement. 6 P a g e

switch statement A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. The syntax for a switch statement in C programming language is as follows: The following rules apply to a switch statement: n a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. value to be compared to and a colon. by the -expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal. will execute until a break statement is reached. lowing that case the next line following the switch statement. eak appears, the flow of control will fall through to subsequent cases until a break is reached. switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. 7 P a g e

Example 8 P a g e

When the above code is compiled and executed, it produces the following result: Nested switch statements It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise. The syntax for a nested switch statement is as follows: Example: 9 P a g e

When the above code is compiled and executed, it produces the following result: The? : Operator We have covered conditional operator? : in previous chapter which can be used to replace if...else statements. It has the following general form: Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The value of a? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression. Example: The output will be: ================================================================== 10 P a g e