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

Similar documents
8. Control statements

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

CHAPTER 4 CONTROL STRUCTURES

Chapter 8 Statement-Level Control Structure

Manual. Subject Code: CS593. Computer Science and Engineering

Decision Making -Branching. Class Incharge: S. Sasirekha

Programming with C++ Language

CHAPTER : 9 FLOW OF CONTROL

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

Flow Control. CSC215 Lecture

Lab Instructor : Jean Lai

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

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

Computer Programming: C++

Introduction. C provides two styles of flow control:

Biyani's Think Tank. Concept based notes. C Language M.C.A. Neha Jain M.C.A Lecturer Deptt. of Information Technology Biyani Girls College, Jaipur

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

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Advanced Computer Programming

Iosif Ignat, Marius Joldoș Laboratory Guide 4. Statements. STATEMENTS in C

Functions. Lecture 6 COP 3014 Spring February 11, 2018

C Functions. 5.2 Program Modules in C

CHAPTER 9 FLOW OF CONTROL

LESSON 6 FLOW OF CONTROL

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

C++ Programming Lecture 11 Functions Part I

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

5. Selection: If and Switch Controls

C: How to Program. Week /Mar/05

INTRODUCTION TO PROGRAMMING

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

from Appendix B: Some C Essentials

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

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

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Chapter 3 - Functions

REPETITION CONTROL STRUCTURE LOGO

Chapter 5: Control Structures

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

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

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

DELHI PUBLIC SCHOOL TAPI

CSE 130 Introduction to Programming in C Control Flow Revisited

CFG (Control flow graph)

Lab 3 (Sections 300, 301 and 302) Prelab: MIPS-Control Instructions

CT 229 Java Syntax Continued

Chapter 7 - Notes User-Defined Functions II

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Theory of control structures

Why Is Repetition Needed?

Control Statements. If Statement if statement tests a particular condition

6.096 Introduction to C++

ANSI C Programming Simple Programs

Chapter 2 - Introduction to C Programming

SELECTION STATEMENTS:

DECISION CONTROL AND LOOPING STATEMENTS

Quiz Determine the output of the following program:

M1-R4: Programing and Problem Solving using C (JAN 2019)

Exceptions, Case Study-Exception handling in C++.

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Lab 3 (All Sections) Prelab: MIPS-Control Instructions

Programming Fundamentals

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Basics of C++ // my first program in C++ Hello World! #include <iostream> using namespace std; int main () { cout << "Hello World!

What did we talk about last time? Examples switch statements

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

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

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

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

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Chapter 6 - Notes User-Defined Functions I

Programming Fundamentals

Object-Oriented Modeling. Sequence Diagram. Slides accompanying Version 1.0

ECE 202 LAB 3 ADVANCED MATLAB

Score score < score < score < 65 Score < 50

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

LECTURE 5 Control Structures Part 2

Methods CSC 121 Fall 2016 Howard Rosenthal

CS64 Week 5 Lecture 1. Kyle Dewey

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

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

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

HW/SW Codesign. WCET Analysis

Fundamentals of Computer Programming Using C

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

Computer Science & Engineering 150A Problem Solving Using Computers

Transcription:

University of Technology Laser & Optoelectronics Engineering Department C++ Lab Sixth week Jump statements The break statement Using break we can leave a loop even if the condition for its end is not fulfilled It can be used to end an infinite loop, or to force it to end before its natural end For example, we are going to stop the count down before its natural end (maybe because of an engine check failure?): // break loop example 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted! int n; for (n=10; n>0; n--) if (n==3) cout << "countdown aborted!"; The continue statement The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration For example, we are going to skip the number 5 in our countdown: // continue loop example 10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE! for (int n=10; n>0; n--) if (n==5) continue;

cout << "FIRE!\n"; Functions Sin(x)=sinx Cos(x)=cosx Tan(x)=tanx Asin(x)=sin -1 Acos(x)=cos -1 x exp(x)=e x sqrt(x)= pow(x,y)=x y Q1write program to find the square root of the numbers from (2-10) Q2 write program to count down from 10 to 1 and skip number 5 from our count down Q3 write a program to find the sin of the angles 0 30 60 90 120 150 180 in radian Q4 write program to find the root of the second order equation Ax2+bx+c=0 Q5 write program in C++ to count down from 10-1 but abort the countdown at number 3 The goto statement goto allows to make an absolute jump to another point in the program You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations The destination point is identified by a label, which is then used as an argument for the goto statement A label is made of a valid identifier followed by a colon (:) Generally speaking, this instruction has no concrete use in structured or object oriented programming aside from those that low-level programming fans may find for it For example, here is our countdown loop using goto:

// goto loop example 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE! int n=10; loop: n--; if (n>0) goto loop; cout << "FIRE!\n"; The exit function exit is a function defined in the cstdlib library The purpose of exit is to terminate the current program with a specific exit code Its prototype is: void exit (int exitcode); The exitcode is used by some operating systems and may be used by calling programs By convention, an exit code of 0 means that the program finished normally and any other value means that some error or unexpected results happened The selective structure: switch The syntax of the switch statement is a bit peculiar Its objective is to check several possible constant values for an expression Something similar to what we did at the beginning of this section with the concatenation of several if and else if instructions Its form is the following: switch (expression) case constant1: group of statements 1; case constant2: group of statements 2; default group of statements It works in the following way: switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes group of statements 1 until it finds the break statement When it finds this break statement the program jumps to the end of the switch selective structure

If expression was not equal to constant1 it will be checked against constant2 If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure Finally, if the value of expression did not match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the label, if it exists (since it is optional) Both of the following code fragments have the same behavior: switch example if-else equivalent switch (x) case 1: cout << "x is 1"; case 2: cout << "x is 2"; cout << "value of x unknown"; if (x == 1) cout << "x is 1"; else if (x == 2) cout << "x is 2"; else cout << "value of x unknown"; The switch statement is a bit peculiar within the C++ language because it uses labels instead of blocks This forces us to put break statements after the group of statements that we want to be executed for a specific condition Otherwise the remainder statements - including those corresponding to other labels- will also be executed until the end of the switch selective block or a break statement is reached For example, if we did not include a break statement after the first group for case one, the program will not automatically jump to the end of the switch selective block and it would continue executing the rest of statements until it reaches either a break instruction or the end of the switch selective block This makes unnecessary to include braces surrounding the statements for each of the cases, and it can also be useful to execute the same block of instructions for different possible values for the expression being evaluated For example: switch (x) case 1: case 2: case 3: cout << "x is 1, 2 or 3"; cout << "x is not 1, 2 nor 3"; Notice that switch can only be used to compare an expression against constants Therefore we cannot put variables as labels (for example case n: where n is a variable) or ranges (case (13):) because they are not valid C++ constants If you need to check ranges or values that are not constants, use a concatenation of if and else if statements Q1 Write program in C++ to print the numbers from (1-100) except the numbers can divided on 2,4,6 without remaining

Q2 Write a program to print numbers from (1-10) but if the numbers more than 10 program will print message (the number is more than 10) Q3 Write program in C++ to print numbers from (1-10) in loop from (1-100)