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

Similar documents
Programming in C++ PART 2

Programming in OOP/C++

Flow Control. CSC215 Lecture

Decision Making in C

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

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

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

LECTURE 5 Control Structures Part 2

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

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

Introduction. C provides two styles of flow control:

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

SELECTION. (Chapter 2)

CT 229 Java Syntax Continued

Text Input and Conditionals

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

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

Control Structures in Java if-else and switch

Computer Programming: C++

5. Control Statements

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Accelerating Information Technology Innovation

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

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

Decision Making -Branching. Class Incharge: S. Sasirekha

Information Science 1

Control Structures in Java if-else and switch

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Boolean evaluation and if statements. Making decisions in programs

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

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

CS 106 Introduction to Computer Science I

LECTURE 04 MAKING DECISIONS

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Chapter 5: Control Structures

CS313D: ADVANCED PROGRAMMING LANGUAGE

Unit 7. 'while' Loops

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Information Science 1

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

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.

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Software Design & Programming I

Theory of control structures

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

Object Oriented Software Design

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Computational Expression

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

ECE 122. Engineering Problem Solving with Java

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

n Group of statements that are executed repeatedly while some condition remains true

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Chapter 2. C++ Basics

The following expression causes a divide by zero error:

5. Selection: If and Switch Controls

C Syntax Out: 15 September, 1995

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Chapter 4: Control Structures I (Selection)

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

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

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Chapter 2: Functions and Control Structures

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

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

Object Oriented Programming with Java

REPETITION CONTROL STRUCTURE LOGO

COMP 208 Computers in Engineering

CpSc 1111 Lab 4 Formatting and Flow Control

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Boolean Expressions. Is Equal and Is Not Equal

M.CS201 Programming language

Boolean Expressions. Is Equal and Is Not Equal

Chapter 4: Conditionals and Loops

Lecture 6. Statements

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

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

Add Subtract Multiply Divide

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

Lecture 5 Tao Wang 1

C++ Programming: From Problem Analysis to Program Design, Third Edition

COMP Introduction to Programming If-Else Statement, Switch Statement and Loops

Object-Oriented Programming

Decision making with if Statement : - Control Statements. Introduction: -

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Transcription:

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

The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful only if your code is testing something simple and does not require multiple statements. if (y!= 0) z = x/y; else z = 0; SAME z = (y!=0)? x/y : 0; The two code segments above have the same effect exactly. However, the one on right is shorter. Pay attention to conditional operator format: Since it has three operands it is classified as a ternary operator. 2

The Conditional Operator z = (y!=0)? x/y : 0; condition is a normal Boolean expression that might appear in an if statement. Parentheses around the condition are not required but should be used to improve the readability. expression 1 is the overall value of the conditional expression if condition is true. expression 2 is the overall value of the conditional expression if condition is false. 3

The Conditional Operator Example: Write a program to enter an integer number and print Big if the number is greater than 10 otherwise print Small. Notice that a single statement for conditional operator is enough to do the required code. 4

if..else ladder and switch statement We studied if..else before and below is an example of nested or ladder if..else code (because it is similar to a ladder) 5

if..else ladder and switch statement The if..else ladder is like having multiple execution paths based on condition check: There is another way of writing this code by using the switch statement instead of the if..else ladder. 6

if..else ladder and switch statement Below is the same code by using switch statement: 7

if..else ladder and switch statement Notice the following important notes: The integral expression MUST be one of the integer types such as int, long, char ONLY. None-integer types are not acceptable. The body of the switch is enclosed by required curly braces { }. case key word is followed by an integral constant and a colon (:). Any number of statements can follow (:) and the last statement for each case must be break (otherwise it will continue executing next case). ONLY on of the case statements will be executed and if no one found the default will be executed and it s optional (if you want). 8

switch statement mistakes Some common mistakes (WRONG code) Not integer type Only constant values allowed of type integer This example can be solved by using if..else ladder ONLY. 9

switch statement mistakes Some common mistakes (WRONG code) What is wrong with this code? Can you spot the mistakes? 10

Using continue and break with loops In any of the loops we studied before we can make use of two statements: continue : will cause the loop to take the next cycle and ignore (skip) any statements that are in the loop after continue. break: will cause the loop to terminate and exit. Both statements can be used with for, while and do..while loops. These statements are usually used with if to test a condition and are executed if the condition is true. 11

Using continue with loops Compare the output of the following programs. The left is without using continue, the right is with continue. skip 12

Using break with loops We studied break with switch statement. The same statement can be used with loop to terminate the loop (stop it). Infinite loop 13

Homework (for LAB) Problem 1: Re-write the program given in Lecture 6-2, slide 10 to use the switch statement instead of the if..else ladder. Problem 2: Write a program to find the sum of some numbers (float), positive or negative, such that the program will print the total sum only if the user enters 0. Use infinite loop with break. Problem 3: Repeat the program in problem 2 above such that it sums only the positive numbers entered and skips the negative ones. Make use of continue. 14

Remember: If you don t practice C++ programming on the computer, YOU WILL NOT LEARN anything. To become a professional programmer you must try all the examples by yourself. 15

Thank You My Office, 3 rd floor, 303 Email: ali.kattan@nobleinst.com Website: www.alikattan.org 16