LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.

Slides:



Advertisements
Similar presentations
Integer Arithmetic. Operator Priority Real Number Arithmetic.
Advertisements

Type Conversion. C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
True or false A variable of type char can hold the value 301. ( F )
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
CH Programming An introduction to programming concepts.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 5.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
C BASICS QUIZ (DATA TYPES & OPERATORS). C language has been developed by (1) Ken Thompson (2) Dennis Ritchie (3) Peter Norton (4) Martin Richards.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
While ( number
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Arithmetic Instructions. Integer and Float Conversions.
Chapter 4 – C Program Control
Chapter 7: Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Programming Fundamentals
Intro to Programming Week # 5 Repetition Structure Lecture # 9
Arithmetic Operator Operation Example + addition x + y
Arrays, For loop While loop Do while loop
Looping and Repetition
Lecture 3 Expressions Richard Gesick.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
With Assignment Operator
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Chapter 2 Programming Basics.
CS 101 First Exam Review.
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

LOOP & Type Conversion

do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false when the loop is entered, the loop body won’t be executed at all. Sometime we want that loop body should execute at least once, no matter what the initial state of the test expression. In this case, you should use the do-while loop, which places the test expression at the end of the loop.

do – while Loop do { statement 1; statement 2;. statement n; }while ( test expression );

do – while Loop Example void main ( ) { longdividend, divisor; charch; do { cout > dividend; cout > divisor; cout<<“Qoutient is”<<dividend / divisor; cout<<“Remainder = ”<<dividend % divisor; cout >ch; }while ( ch != ‘n’ ) ; }

TYPE CONVERSION

Integer and Float Conversion In order to effectively develop C++ Program, it is necessary to understand the rules that are used for implicit conversion of floating point and integer values in C++. These three rules are – An arithmetic operation between an integer and integer always yields an integer result. – Operation between a real and real always yields a real result – Operation between an integer and real always yields a real result.

Integer and Float Conversion ResultOperation 25 / / / / / / / / 5.0

Type Conversion In Assignment In some Cases, it may happen that the type of the expression on the right hand side and the type of the variable on the left hand side of the assignment operator may not be same. In that case the value of the expression is promoted or demoted depending on the type of the variable on left hand side of assignment operator.

Type Conversion in Assignments Example int i; float y; i = 35.9; y = 10; As 35.9 is of float type it cannot be stored in i of int type. In this case float is demoted to an int and then value is stored in i. So the value of i will be 35. Same will happen in y i.e. 10 will be promoted to and then will be stored in y.

Type Conversion in Assignment float a=1.0, b = 2.0, c = 10.0; int s; s = a * b * c / / 4 – 3 * 1.1; In the above example, some of the operands are of type int and some of them are of type float. As we know during evaluation of the expression int will be promoted to float and result would be of type float. But when this float value is assigned to s, it is again demoted to an int and then stored in s.