The document provides information on various flow control statements in C including comments, if/else statements, for loops, while loops, do-while loops, break, continue, switch statements, and goto. It emphasizes using comments to explain why code is written a certain way rather than just what it does. It also provides examples of each statement type and best practices for formatting and structuring code blocks.
The document discusses C programming concepts including control statements, loops, relational operators, data types, arrays, and functions. It provides code examples to demonstrate while, for, do-while loops, if/else statements, functions, arrays, and more. Various outputs are shown for the example code snippets.
The document discusses different types of control constructs in programming. There are two main types: decision making statements and looping statements. Decision making statements include if, if-else, and nested if-else statements which allow for conditional execution of code. Looping statements like for, while, and do-while loops allow code to repeat execution for a set number of iterations or as long as a condition remains true. Examples are provided for each type of control construct.
The document discusses different control structures in the C programming language including if-else statements, nested if statements, switch statements, break statements, and continue statements. It provides syntax examples and explanations for how each statement directs program flow. The key control structures covered are if-else for conditional execution, switch for multi-way decisions, break to exit loops, and continue to skip to the next loop iteration.
The document discusses different types of selection and looping statements in C programming including if-else, if-else if-else, switch case, while loop, do-while loop, and for loop. It provides examples of each statement type and highlights important aspects like counter initialization, conditional expressions, and incrementing/decrementing counters. The differences between while, do-while and for loops are also summarized.
The document discusses loops and switch case statements in C language programming. It provides sample codes and screenshots of outputs to explain steps. Functions like for loops, while loops and do-while loops are looping statements used to repeatedly execute a block of code until a condition is met. Switch case statements are used as alternatives to long if-else statements to compare a variable to multiple integral values. Break and continue keywords are used to control loop flow.
The document discusses arrays in C programming. It explains that arrays consist of contiguous memory locations and how to declare and initialize one-dimensional and multi-dimensional arrays. It provides examples of accessing array elements, finding the sum and transpose of 2D arrays, and multiplying two matrices using nested for loops. The document is a reference for working with different types of arrays through examples of common array operations in C.
The document discusses the if-else conditional statement in C programming. It provides the syntax and examples of using if-else statements to execute code conditionally based on whether an expression is true or false. This includes if-then statements with and without else blocks, multiway if-else statements, nested if statements, and examples checking the equality of variables and ranges of values.
The document discusses C programming concepts including control statements, loops, relational operators, data types, arrays, and functions. It provides code examples to demonstrate while, for, do-while loops, if/else statements, functions, arrays, and more. Various outputs are shown for the example code snippets.
The document discusses different types of control constructs in programming. There are two main types: decision making statements and looping statements. Decision making statements include if, if-else, and nested if-else statements which allow for conditional execution of code. Looping statements like for, while, and do-while loops allow code to repeat execution for a set number of iterations or as long as a condition remains true. Examples are provided for each type of control construct.
The document discusses different control structures in the C programming language including if-else statements, nested if statements, switch statements, break statements, and continue statements. It provides syntax examples and explanations for how each statement directs program flow. The key control structures covered are if-else for conditional execution, switch for multi-way decisions, break to exit loops, and continue to skip to the next loop iteration.
The document discusses different types of selection and looping statements in C programming including if-else, if-else if-else, switch case, while loop, do-while loop, and for loop. It provides examples of each statement type and highlights important aspects like counter initialization, conditional expressions, and incrementing/decrementing counters. The differences between while, do-while and for loops are also summarized.
The document discusses loops and switch case statements in C language programming. It provides sample codes and screenshots of outputs to explain steps. Functions like for loops, while loops and do-while loops are looping statements used to repeatedly execute a block of code until a condition is met. Switch case statements are used as alternatives to long if-else statements to compare a variable to multiple integral values. Break and continue keywords are used to control loop flow.
The document discusses arrays in C programming. It explains that arrays consist of contiguous memory locations and how to declare and initialize one-dimensional and multi-dimensional arrays. It provides examples of accessing array elements, finding the sum and transpose of 2D arrays, and multiplying two matrices using nested for loops. The document is a reference for working with different types of arrays through examples of common array operations in C.
The document discusses the if-else conditional statement in C programming. It provides the syntax and examples of using if-else statements to execute code conditionally based on whether an expression is true or false. This includes if-then statements with and without else blocks, multiway if-else statements, nested if statements, and examples checking the equality of variables and ranges of values.
I am telling about basic c such as variable,constant,operators,decison making,loops,switch,break and continue,function,call by value ,call by reference and recursion.In this pdf i give some practice problems too.
This document discusses different types of control structures in programming, including conditional and iterative structures. It describes if/else statements, switch statements, and different types of loops - while loops, do-while loops, and for loops. It explains how each structure executes code conditionally or iteratively and provides examples of their usage. The break and continue instructions are also covered, allowing loops to be exited or skipped prematurely.
C Programming Language Step by Step Part 5Rumman Ansari
The document discusses operators precedence and associativity in C programming language. It provides a table that lists C operators in order of precedence from highest to lowest. It also shows the associativity of each operator. The document then provides several code examples to demonstrate how operators precedence works in expressions evaluation in C.
The document discusses various control flow statements in C including if-then-else statements, switch statements, and loops. It provides examples of using if-then-else with one or multiple statements in the then and else blocks. Examples are given for while, do-while, and for loops showing the basic syntax and use of semicolons. The break and continue keywords are also discussed as ways to control loop execution.
This document discusses using the fprintf function in MATLAB to format output. It covers using fprintf to print to the command window, save data to a file, and read from a file. Examples are provided to demonstrate printing tables and plotting 2D graphs by looping through angle and sine values. Conversion characters and formatting options for fprintf are explained for displaying different data types like integers, floating point numbers, and strings.
In this module of python programming you will be learning about control statements. A control statement is a statement that determines whether other statements will be executed. An if statement decides whether to execute another statement, or decides which of two statements to execute. A loop decides how many times to execute another statement.
The document provides information on strings in C programming language. It discusses that strings are arrays of characters terminated by a null character. It shows examples of declaring and initializing strings, reading strings from users, and printing strings. It also provides examples of using standard string functions like strcpy(), strcat(), strlen() etc. Further examples demonstrate finding frequency of characters in a string, counting vowels, consonants, digits and whitespaces, and removing non-alphabet characters from a string.
The document discusses left recursion in grammars. It defines immediate and indirect left recursion. Immediate left recursion occurs when a nonterminal derives itself as the leftmost symbol. Indirect left recursion is more complex, involving multiple nonterminals. The general algorithm for removing immediate left recursion is described. It involves replacing the left recursive rule with a new production using a "tail" or "rest" nonterminal to capture the remaining symbols after the first nonterminal. Examples are given to illustrate the process of removing immediate left recursion from specific grammar rules.
The document contains 5 code snippets demonstrating the use of various C functions:
1) A program that calculates the sum of 1/n from n=1 to 10 using a cast to convert n to a float.
2) A program that gets a single character input and prints a message based on the input.
3) A program that tests if a character is a letter, digit or other character using isalpha and isdigit functions.
4) A program that converts a character to uppercase if lowercase and vice versa using islower, toupper, tolower.
5) A program that converts a character to lowercase if uppercase and vice versa using isupper, tolower, toupper.
This document discusses various concepts in data flow modeling and VHDL, including:
- Using <= to assign signals and define data flow
- Concurrent and process modeling
- Conditional signal assignment using when/else and with/select statements
- The unaffected value for conditional assignments
It also provides examples of modeling common logic functions like muxes, decoders, and tri-state buffers in VHDL using these concepts.
The document provides information about Python programming language:
- Python was created in the late 1980s and became widely popular in the early 2000s.
- It is a high-level, general-purpose, interpreted programming language that can be used for web, desktop, game development, data science, and more.
- Some key features of Python include dynamic typing, automatic memory management, and being multi-paradigm supporting object-oriented, imperative, functional programming styles.
This code checks if a number is prime or not. It takes user input for a number, then uses a while loop to check if it is divisible by any number from 2 to half the input number. If it is not divisible by any, it prints that the number is prime, otherwise it prints that it is not prime.
The document provides an introduction to the C programming language, covering basic concepts like variables, data types, input/output, conditional statements, loops, functions, pointers, structures, and file input/output. It includes code examples for each concept to demonstrate syntax and usage. The key elements covered are variables and basic data types, conditional logic with if/else statements, different loop structures like for, while, do-while, break and continue keywords, defining functions, pointers and reference/dereference operators, structs to group related data, and reading/writing files.
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...Artur Skowroński
Systemy typów kojarzą się większości programistów z czymś naprawdę ciężkim, mocno akademickim i trudnym do zrozumienia, dodatkowo nie przydatnym w codziennym życiu developera. Postaram się to zmienić, przynajmniej cześciowo. O ile sam nie wierzę że świetna znajomość systemów typów uczyni Cię znacznie bardziej produktywnym, to postaram się rozwiać mit że jest to coś trudnego i niezrozumiałego. W przystępny i interaktywny sposób przedstawię odrobinę “Computer Science” które może pomóc nam zrozumieć dlaczego niektóre rzeczy w używanych przez nas językach są zaprojektowane tak, a nie inaczej.
Przede wszystkim odpowiemy zaś sobie na prawdę - jak bardzo “Typed” TypeScript tak naprawdę jest?
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’aArtur Skowroński
Systemy typów kojarzą się większości programistów z czymś naprawdę ciężkim, mocno akademickim i trudnym do zrozumienia, dodatkowo nie przydatnym w codziennym życiu developera. Postaram się to zmienić, przynajmniej cześciowo. O ile sam nie wierzę że świetna znajomość systemów typów uczyni Cię znacznie bardziej produktywnym, to postaram się rozwiać mit że jest to coś trudnego i niezrozumiałego. W przystępny i interaktywny sposób przedstawię odrobinę “Computer Science” które może pomóc nam zrozumieć dlaczego niektóre rzeczy w używanych przez nas językach są zaprojektowane tak, a nie inaczej.
Przede wszystkim odpowiemy zaś sobie na prawdę - jak bardzo “Typed” TypeScript tak naprawdę jest?
This document discusses loops in C programming. It describes for, while, and do-while loops. The for loop allows code to be executed a specific number of times, specified with an initialization, condition, and increment statement. The while loop repeats code as long as a condition is true. The do-while loop is similar to the while loop, but the code is executed once before checking the condition. The document also discusses counter-controlled and sentinel-controlled loops, and provides examples of each loop type.
C programming uses basic elements like expressions, statements, blocks and functions. Expressions combine constants, variables and operators, while statements end with semicolons. A block of statements is treated as a single statement. Standard library functions include printf(), exit() and scanf(). Control structures like if-else, switch, for, while and do-while statements are used for decision making and looping.
This document contains review questions and solutions regarding decision making and branching concepts in C programming from Chapter 5. It includes questions on if/else statements, switch statements, logical operators, and evaluating expressions. Sample programs test various branching logic and output values of variables based on conditional expressions.
This document contains review questions and programming exercises related to decision making and looping in programming. It discusses for, while, do-while loops and their usage. Some key points:
- It provides true/false questions about loop control expressions, initialization, test conditions, etc. for various loop types.
- Programming exercises include problems to reverse digits of a number, calculate factorials, Fibonacci sequences, investment equations, and plotting functions using loops.
- Questions analyze sample code segments to determine number of loop iterations and find any errors.
- Exercises involve reading age data and counting people in specific age groups, printing patterns using loops, and converting integers to binary format.
The document provides information on various flow control statements in C including comments, if/else statements, for loops, while loops, do-while loops, switch statements, break, continue, and goto. It emphasizes using comments to explain why code is written a certain way rather than just what it does. It also provides examples of different loop and conditional structures and recommendations around formatting and style.
This document discusses filters and editing options for smartphone pictures, comparing filters built into apps like Instagram to more advanced editing controls available in other apps like vscocam that allow adjusting location, brightness, saturation and other effects. Examples are given of how a picture might be changed using these different options.
I am telling about basic c such as variable,constant,operators,decison making,loops,switch,break and continue,function,call by value ,call by reference and recursion.In this pdf i give some practice problems too.
This document discusses different types of control structures in programming, including conditional and iterative structures. It describes if/else statements, switch statements, and different types of loops - while loops, do-while loops, and for loops. It explains how each structure executes code conditionally or iteratively and provides examples of their usage. The break and continue instructions are also covered, allowing loops to be exited or skipped prematurely.
C Programming Language Step by Step Part 5Rumman Ansari
The document discusses operators precedence and associativity in C programming language. It provides a table that lists C operators in order of precedence from highest to lowest. It also shows the associativity of each operator. The document then provides several code examples to demonstrate how operators precedence works in expressions evaluation in C.
The document discusses various control flow statements in C including if-then-else statements, switch statements, and loops. It provides examples of using if-then-else with one or multiple statements in the then and else blocks. Examples are given for while, do-while, and for loops showing the basic syntax and use of semicolons. The break and continue keywords are also discussed as ways to control loop execution.
This document discusses using the fprintf function in MATLAB to format output. It covers using fprintf to print to the command window, save data to a file, and read from a file. Examples are provided to demonstrate printing tables and plotting 2D graphs by looping through angle and sine values. Conversion characters and formatting options for fprintf are explained for displaying different data types like integers, floating point numbers, and strings.
In this module of python programming you will be learning about control statements. A control statement is a statement that determines whether other statements will be executed. An if statement decides whether to execute another statement, or decides which of two statements to execute. A loop decides how many times to execute another statement.
The document provides information on strings in C programming language. It discusses that strings are arrays of characters terminated by a null character. It shows examples of declaring and initializing strings, reading strings from users, and printing strings. It also provides examples of using standard string functions like strcpy(), strcat(), strlen() etc. Further examples demonstrate finding frequency of characters in a string, counting vowels, consonants, digits and whitespaces, and removing non-alphabet characters from a string.
The document discusses left recursion in grammars. It defines immediate and indirect left recursion. Immediate left recursion occurs when a nonterminal derives itself as the leftmost symbol. Indirect left recursion is more complex, involving multiple nonterminals. The general algorithm for removing immediate left recursion is described. It involves replacing the left recursive rule with a new production using a "tail" or "rest" nonterminal to capture the remaining symbols after the first nonterminal. Examples are given to illustrate the process of removing immediate left recursion from specific grammar rules.
The document contains 5 code snippets demonstrating the use of various C functions:
1) A program that calculates the sum of 1/n from n=1 to 10 using a cast to convert n to a float.
2) A program that gets a single character input and prints a message based on the input.
3) A program that tests if a character is a letter, digit or other character using isalpha and isdigit functions.
4) A program that converts a character to uppercase if lowercase and vice versa using islower, toupper, tolower.
5) A program that converts a character to lowercase if uppercase and vice versa using isupper, tolower, toupper.
This document discusses various concepts in data flow modeling and VHDL, including:
- Using <= to assign signals and define data flow
- Concurrent and process modeling
- Conditional signal assignment using when/else and with/select statements
- The unaffected value for conditional assignments
It also provides examples of modeling common logic functions like muxes, decoders, and tri-state buffers in VHDL using these concepts.
The document provides information about Python programming language:
- Python was created in the late 1980s and became widely popular in the early 2000s.
- It is a high-level, general-purpose, interpreted programming language that can be used for web, desktop, game development, data science, and more.
- Some key features of Python include dynamic typing, automatic memory management, and being multi-paradigm supporting object-oriented, imperative, functional programming styles.
This code checks if a number is prime or not. It takes user input for a number, then uses a while loop to check if it is divisible by any number from 2 to half the input number. If it is not divisible by any, it prints that the number is prime, otherwise it prints that it is not prime.
The document provides an introduction to the C programming language, covering basic concepts like variables, data types, input/output, conditional statements, loops, functions, pointers, structures, and file input/output. It includes code examples for each concept to demonstrate syntax and usage. The key elements covered are variables and basic data types, conditional logic with if/else statements, different loop structures like for, while, do-while, break and continue keywords, defining functions, pointers and reference/dereference operators, structs to group related data, and reading/writing files.
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...Artur Skowroński
Systemy typów kojarzą się większości programistów z czymś naprawdę ciężkim, mocno akademickim i trudnym do zrozumienia, dodatkowo nie przydatnym w codziennym życiu developera. Postaram się to zmienić, przynajmniej cześciowo. O ile sam nie wierzę że świetna znajomość systemów typów uczyni Cię znacznie bardziej produktywnym, to postaram się rozwiać mit że jest to coś trudnego i niezrozumiałego. W przystępny i interaktywny sposób przedstawię odrobinę “Computer Science” które może pomóc nam zrozumieć dlaczego niektóre rzeczy w używanych przez nas językach są zaprojektowane tak, a nie inaczej.
Przede wszystkim odpowiemy zaś sobie na prawdę - jak bardzo “Typed” TypeScript tak naprawdę jest?
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’aArtur Skowroński
Systemy typów kojarzą się większości programistów z czymś naprawdę ciężkim, mocno akademickim i trudnym do zrozumienia, dodatkowo nie przydatnym w codziennym życiu developera. Postaram się to zmienić, przynajmniej cześciowo. O ile sam nie wierzę że świetna znajomość systemów typów uczyni Cię znacznie bardziej produktywnym, to postaram się rozwiać mit że jest to coś trudnego i niezrozumiałego. W przystępny i interaktywny sposób przedstawię odrobinę “Computer Science” które może pomóc nam zrozumieć dlaczego niektóre rzeczy w używanych przez nas językach są zaprojektowane tak, a nie inaczej.
Przede wszystkim odpowiemy zaś sobie na prawdę - jak bardzo “Typed” TypeScript tak naprawdę jest?
This document discusses loops in C programming. It describes for, while, and do-while loops. The for loop allows code to be executed a specific number of times, specified with an initialization, condition, and increment statement. The while loop repeats code as long as a condition is true. The do-while loop is similar to the while loop, but the code is executed once before checking the condition. The document also discusses counter-controlled and sentinel-controlled loops, and provides examples of each loop type.
C programming uses basic elements like expressions, statements, blocks and functions. Expressions combine constants, variables and operators, while statements end with semicolons. A block of statements is treated as a single statement. Standard library functions include printf(), exit() and scanf(). Control structures like if-else, switch, for, while and do-while statements are used for decision making and looping.
This document contains review questions and solutions regarding decision making and branching concepts in C programming from Chapter 5. It includes questions on if/else statements, switch statements, logical operators, and evaluating expressions. Sample programs test various branching logic and output values of variables based on conditional expressions.
This document contains review questions and programming exercises related to decision making and looping in programming. It discusses for, while, do-while loops and their usage. Some key points:
- It provides true/false questions about loop control expressions, initialization, test conditions, etc. for various loop types.
- Programming exercises include problems to reverse digits of a number, calculate factorials, Fibonacci sequences, investment equations, and plotting functions using loops.
- Questions analyze sample code segments to determine number of loop iterations and find any errors.
- Exercises involve reading age data and counting people in specific age groups, printing patterns using loops, and converting integers to binary format.
The document provides information on various flow control statements in C including comments, if/else statements, for loops, while loops, do-while loops, switch statements, break, continue, and goto. It emphasizes using comments to explain why code is written a certain way rather than just what it does. It also provides examples of different loop and conditional structures and recommendations around formatting and style.
This document discusses filters and editing options for smartphone pictures, comparing filters built into apps like Instagram to more advanced editing controls available in other apps like vscocam that allow adjusting location, brightness, saturation and other effects. Examples are given of how a picture might be changed using these different options.
C programming was created by Dennis Ritchie in 1972 to design and support the Unix operating system. It has only 27 original keywords and is easy to build compilers for, making it portable across many platforms. In 1983, ANSI formed a committee to standardize C as ANSI C. C is intended for programmers as a powerful, efficient yet structured language. A basic "Hello World" program is presented to demonstrate a simple C program structure.
The document discusses static and dynamic force analysis of mechanisms. It defines key terms like static equilibrium, inertia force, inertia torque, and D'Alembert's principle. It explains the conditions for a body to be in equilibrium under different force configurations. Dynamic force analysis considers inertia forces to determine input torque required. Equivalent masses and Klein's construction diagram are discussed for dynamic analysis of reciprocating engines. Correction couple and torque are also summarized.
1. Vibration is a periodic motion where the motion repeats itself after an interval of time. Energy is converted between potential and kinetic forms during vibration.
2. Forced vibration occurs when an external force causes an object to vibrate, while free vibration happens when an object vibrates on its own accord after an initial disturbance.
3. Resonance is a phenomenon where the frequency of an external force matches the natural frequency of a vibrating system, causing the amplitude of vibrations to become very high.
Air enters a combustion chamber with a mach number of 0.15. Sufficient heat is added to raise the stagnation temperature ratio to 3 and the final mach number is 0.8. To determine:
1) The entry mach number is 0.15
2) Due to heating, the static pressure decreases along the flow. The percentage loss in static pressure needs to be determined.
3) The properties of air (γ, Cp) are given to solve the problem.
The document discusses different types of control statements in C programming including decision control statements, iteration statements, and transfer statements. It provides details about if, if-else, switch, while, do-while, for loops. Decision control statements like if, if-else, switch allow altering the flow of execution based on certain conditions. Iteration statements like while, do-while, for are used to repeat a block of code until the given condition is true. They allow looping in a program.
Introduction to control structure in C Programming Language include decision making (if statement, if..else statement, if...else if...else statement, nested if...else statement, switch...case statement), Loop(for loop, while loop, do while loop, nested loop) and using keyword(break, continue and goto)
This document provides an introduction to Java programming concepts including what Java is, how to set up a Java development environment, basic Java code structure, variables, data types, arithmetic operations, conditional statements, loops, and getting user input. It covers elementary Java topics through examples and explanations. Key points include how to write, compile and run a simple Java program that prints "Hello World", how to declare and use variables of different data types, the different conditional statements (if, if-else, if-else-if-else), looping constructs (while, do-while, for), and taking user input using the Scanner class.
The document describes a modular C programming directory structure that contains subdirectories for different C programming concepts like arrays, functions, strings, structures, etc. Each subdirectory contains C source code files that demonstrate examples for the given concept. The document also provides brief introductions and explanations for basic C programming topics like variables, data types, operators, decision making statements, loops, functions and pointers.
The document introduces different types of loops in C programming including while, for, and do-while loops. It explains that loops allow repeated execution of a block of code until a certain condition is met. The key types of loops are pretest and post-test loops, which differ in when the loop condition is evaluated. It provides examples of implementing various loops in C and using concepts like initialization, updating, nesting, and break/continue statements.
The control statements enable us to specify the order in which the various instructions in a program are to be executed by the computer. They determine the flow of control in a program.
There are 4 types of control statements in C. They are:
a) Sequence control statements
b) Decision control statements or conditional statement
c) Case-control statements
d) Repetition or loop control statements
At the end of this lecture students should be able to;
Describe the looping structures in C programming language.
Practice the control flow of different looping structures in C programming language.
Practice the variants in control flow of different looping structures in C programming language.
Apply taught concepts for writing programs.
This document discusses control statements in C programming. It covers different types of control statements like decision structures (if, if-else, nested if), the switch statement, and repetition structures (for, while, continue loops). It provides examples of if, if-else, and switch-case statements. Key topics include branching based on conditions, logical and relational operators, compound statements, nested control structures, and avoiding common errors with switch statements.
this slide is for to understand the conditions which are applied in C++ programming language. I hope u would understand better by viewing this presentation.
C programming is a widely used programming language. The document provides an overview of key concepts in C programming including variables, data types, operators, decision and loop control statements, functions, pointers, arrays, strings, structures, and input/output functions. It also provides examples to illustrate concepts like arrays, strings, functions, pointers, and structures. The main function is the entry point for all C programs where code execution begins.
The document discusses various control structures in C including loops. It covers while, do-while, and for loops. It provides examples of using each loop type to calculate a grade point average (GPA) using both counter-controlled and sentinel-controlled repetition. Key differences between while, do-while and for loops are explained along with flowcharts demonstrating the logic flow for each. Common errors to avoid with loops in C are also listed.
computer programming Control Statements.pptxeaglesniper008
The document summarizes control statements in the C programming language. It discusses decision statements like if and switch statements. It also covers loop statements like for, do-while and while loops. The for loop is described as the most common loop in C. Examples are provided to illustrate if, switch and for statements. Key points covered include the syntax and flow of if-else, switch-case statements and for loops.
C lecture 4 nested loops and jumping statements slideshareGagan Deep
Nested Loops and Jumping Statements(Loop Control Statements), Goto statement in C, Return Statement in C Exit statement in C, For Loops with Nested Loops, While Loop with Nested Loop, Do-While Loop with Nested Loops, Break Statement, Continue Statement : visit us at : www.rozyph.com
4 operators, expressions & statementsMomenMostafa
This document discusses various C programming language concepts including operators, expressions, statements, data types, and type conversions. It provides examples of using unary and binary operators, increment/decrement operators, and the modulus operator. It also discusses operator precedence, expressions, statements, and how C handles type conversions between integers, floats, and characters both automatically and through explicit casting. Loops and conditional statements are demonstrated in examples converting seconds to minutes and counting down bottles of water.
The document discusses control statements in C programming. It covers various selection statements like if, if-else, switch as well as iteration statements like for, while, do-while loops. Nested loops and special control statements like break, continue and goto are also explained. Operators used in control statements like relational, logical, assignment operators are defined along with examples. The different types of loops and their usages to control program flow are demonstrated.
This document discusses various control structures in C programming including decision making statements (if, if-else, nested if-else, switch), loops (do-while, while, for), and unconditional branching (break, continue, goto). Examples are provided for each type of control structure to demonstrate their syntax and usage. Key control structures like if-else ladders and nested loops are explained through examples like finding the maximum of three numbers and printing patterns using nested for loops.
C for Loop
Loops are used in programming to execute a block of code repeatedly until a specified condition is met. In this tutorial, you will learn to create for loop in C programming.
C programming has three types of loops:
for loop
while loop
do...while loop
what are loop in general
what is loop in c language
uses of loop in c language
types of loop in c language
program of loop in c language
syantax of loop in c language
The document discusses various control structures in C programming including branching, looping, and nested control structures. It provides examples of if-else statements, if-else-if ladders, nested if-else statements, and switch statements. Key points covered include the syntax and flow of if-else, if-else-if, and switch statements as well as differences between switch and if-else structures. Sample programs demonstrate using control structures to check conditions, find largest of three numbers, and calculate grades.
2. Comments
Comments: /* This is a comment */
– Use them!
– Comments should explain:
special cases
the use of functions (parameters, return values, purpose)
special tricks or things that are not obvious
– explain WHY your code does things the what it does.
3. More on Comments
A bad comment:
…
i = i + 1; /* assign i+1 to the variable i */
……
A better comment:
…
i = i + 1; /* increment the loop counter */
…
4. C++ Comments
A bad comment:
…
i = i + 1; // assign i+1 to the variable i
……
A better comment:
…
i = i + 1; // increment the loop counter
…
5. C Statements
In the most general sense, a statement is a part of your
program that can be executed.
An expression is a statement.
a=a+1;
a--;
A function call is also a statement.A function call is also a statement.
printf("%d",a);
Other statements ……
C is a free form language, so you may type the
statements in any style you feel comfortable:
a=
a+
1;a--; line breaks can be anywhere
6. Compound Statements
Sequences of statements can be combined into one
with {...}
Much like Java:
{
printf ("Hello, ");
printf ("world! n");printf ("world! n");
}
The C compiler treats the collection of these
statements like they are a single statement.
7. C Statements
Some Suggestions
DO: stay consistent with how you use whitespace
DO: put block braces on their own line.
– This makes the code easier to read.
DO: line up block braces so that it is easy to find the
beginning and end of a block.
AVOID: spreading a single statement across multiple
lines if there is no need.
– Try to keep it on one line.
8. The if Statement (1)
Form 1:
if (expression)
statement1;
next statement;
Form 2:
if (expression)
statement1;
else
Execute statement1
if expression is non-zero
(i.e., it does not have to be exactly 1)
else
statement2;
next statement;
Form 3:
if (expression)
statement1;
else if (expression)
statement2;
else
statement3;
next statement;
9. The if Statement (2)
For Example:
#include <stdio.h>
int x,y;
int main ()
{
printf ("nInput an integer value for x: ");
scanf ("%d", &x);
printf ("nInput an integer value for y: ");
scanf ("%d",&y);
if (x==y)
printf ("x is equal to yn");
else if (x > y)
printf ("x is greater than yn");
else
printf ("x is smaller than yn");
return 0;
}
10. The for Statement (1)
The most important looping structure in C.
Generic Form:
for (initial ; condition ; increment )
statement
initial, condition, and increment are C expressions.
For loops are executed as follows:For loops are executed as follows:
1. initial is evaluated. Usually an assignment statement.
2. condition is evaluated. Usually a relational expression.
3. If condition is false (i.e. 0), fall out of the loop (go to step 6.)
4. If condition is true (i.e. nonzero), execute statement
5. Execute increment and go back to step 2.
6. Next statement
11. The for Statement (2)
For statement examples
#include <stdio.h>
int main () {
int count,x,y;
int ctd;
/* 1. simple counted for loop */
for (count =1; count <=20; count++)
/* 4. initialization outside of loop */
count = 1;
for ( ; count < 1000; count++)
printf("%d ", count);
/* 5. very little need be in the for */
count=1; ctd=1;
for ( ; ctd; ) {
printf("%d ", count);
for (count =1; count <=20; count++)
printf ("%dn", count);
/* 2. for loop counting backwards */
for (count = 100; count >0; count--) {
x*=count;
printf("count=%d x=%dn", count,x);
}
/* 3. for loop counting by 5's */
for (count=0; count<1000; count += 5)
y=y+count;
printf("%d ", count);
count++; ctd=count<1000;
}
/* 6. compound statements for
initialization and increment */
for (x=0, y=100; x<y; x++, y--) {
printf("%d %dn", x,y);
}
return 0;
}
12. The for Statement (3)
Nesting for Statements
– for statements (and any other C statement) can go inside the
loop of a for statement.
– For example:
#include <stdio.h>
int main( ) {int main( ) {
int rows=10, columns=20;
int r, c;
for ( r=rows ; r>0 ; r--)
{
for (c = columns; c>0; c--)
printf ("X");
printf ("n");
}
}
13. The while Statement
Generic Form
while (condition)
statement
Executes as expected:
1. condition is evaluated
2. If condition is false (i.e. 0), loop is exited (go to step 5)
3. If condition is true (i.e. nonzero), statement is executed
4. Go to step 14. Go to step 1
5. Next statement
Note:
– for ( ; condition ; ) is equivalent to while (condition)
stmt; stmt;
– for (exp1; exp2; exp3) stmt;
is equivalent to
exp1;
while(exp2) { stmt; exp3; }
14. The do ... while Loop (1)
Generic Form:
do
statement
while (condition);
Standard repeat until loop
Like a while loop, but with condition test at bottom.
Always executes at least once.Always executes at least once.
The semantics of do...while:
1. Execute statement
2. Evaluate condition
3. If condition is true go to step 1
4. Next statement
15. The do ... while Loop (2)
#include <stdio.h>
int get_menu_choice (void);
main()
{
int choice;
do
{
choice = get_menu_choice ();
/* simple function get_menu_choice */
int get_menu_choice (void)
{
int selection = 0;
do {
printf ("n");
printf ("n1 - Add a Record ");choice = get_menu_choice ();
printf ("You chose %dn",choice);
} while(choice!=4);
return 0;
}
printf ("n1 - Add a Record ");
printf ("n2 - Change a Record ");
printf ("n3 - Delete a Record ");
printf ("n4 - Quit ");
printf ("nnEnter a selection: ");
scanf ("%d", &selection);
} while ( selection<1 || selection>4);
return selection;
}
16. break and continue
The flow of control in any loop can be changed through the use
of the break and continue commands.
The break command exits the loop immediately.
– Useful for stopping on conditions not controlled in the loop condition.
– For example:
for (x=0; x<10000; x++) {
if ( x*x % 5==1) break;
... do some more work ...... do some more work ...
}
– Loop terminates if x*x % 5 == 1
The continue command causes the next iteration of the loop to
be started immediately.
– For example:
for (x=0; x<10000; x++) {
if (x*x % 5 == 1) continue;
printf( "%d ", 1/ (x*x % 5 – 1) );
}
– Don't execute loop when x*x % 5 == 1 (and avoid division by 0)
17. Example: for and break Together
const int mycard=3;
int guess;
for(;;)
{
printf("Guess my card:");
scanf("%d",&guess);
if(guess==mycard)
The notation for(;;) is used
to create an infinite for loop.
while(1) creates an infinite
while loop instead.
if(guess==mycard)
{
printf("Good guess!n");
break;
}
else
printf("Try again.n");
}
To get out of an infinite loop
like this one, we have to use
the break statement.
18. switch Statement
Switch statement is used to do “multiple choices”.
Generic form:
switch(expression)
{
case constant_expr1 : statements
case constant_expr2 : statements
…
case constant_exprk : statements
default : statements
}
1. expression is evaluated.
2. The program jumps to the corresponding constant_expr.
3. All statements after the constant_expr are executed until a
break (or goto, return) statement is encountered.
19. Example: switch Statement
int a;
printf("1. Open file..n");
printf("2. Save file.n");
printf("3. Save as..n");
printf("4. Quit.n");
printf("Your choice:");
scanf("%d", &a);
int a;
printf("1. Open file..n");
printf("2. Save file.n");
printf("3. Save as..n");
printf("4. Quit.n");
printf("Your choice:");
scanf("%d", &a);scanf("%d", &a);
if(a==1)
open_file();
else if(a==2)
save_file();
else if(a==3)
save_as();
else if(a==4) return 0;
else return 1;
scanf("%d", &a);
switch(a)
{
case 1: open_file();break;
case 2: save_file();break;
case 3: save_as();break;
case 4: return 0;
default: return 1;
}
20. Jumping Out of Nested Loops -- goto
The goto statement will jump to any point of your program.
Use only if it is absolutely necessary (never in this course)
for(;;)
{
……
while(…)
{
Never jump into a loop!
Never jump backward!{
switch(…)
{
……
case … : goto finished; /* finished is a label */
}
}
}
finished: /* Jumped out from the nested loops */
Never jump backward!