Found 34489 Articles for Programming

How to convert a Python for loop to while loop?

Pythonista
Updated on 11-Feb-2020 06:47:06

8K+ Views

Unlike while loop, for loop in Python doesn't need a counting variable to keep count of number of iterations. Hence, to convert a for loop into equivalent while loop, this fact must be taken into consideration.Following is a simple for loop that traverses over a rangefor x in range(5):      print (x) To convert into a while loop, we initialize a counting variable to 0 before the loop begins and increment it by 1 in every iteration as long as it is less than 5x=0 while x

Print Hello World without semicolon in C++

Syed Javed
Updated on 11-Feb-2020 06:57:23

974 Views

There are multiple ways to write a C++ program without semicolons. Note that doing this is very bad practice and should never be used in real code. This is presented just as informational content. The easiest way to write a C++ Program without Semicolons is using if statements. Almost all statements in C++ can be treated as expressions. So, if we place the statement inside an if statement with a blank pair of parentheses, we don’t have to end it with a semicolon anymore. ExampleLive Demo#include int main() {    if (std::cout > N) {}       if (std::cout

How to stop an infinite loop safely in Python?

Pythonista
Updated on 30-Jul-2019 22:30:21

288 Views

Infinite loop is the one that doesn't stop on its own. It happens when the looping condition continues to remain true forever. In such a case, the loop must be forcibly stopped by pressing ctrl-C to generate keyboard interrupt

How to prevent loops going into infinite mode in Python?

Naveen Singh
Updated on 23-Aug-2023 19:02:26

2K+ Views

In python the while loop needs to be controlled by making some provision inside the body of the loop to drive the condition mentioned in the beginning to false. This is usually done by keeping count of iterations. If the while loop condition never evaluates to False, then we will have an infinite loop, which is a loop that never stops automatically, in this case we need to interrupt externally. count=0 while condition: stmt1 stmt2 . . count=count+1 Example Let’s take an example and ... Read More

Putting semicolons after while and if statements in C++

Moumita
Updated on 11-Feb-2020 06:55:05

2K+ Views

When you have a statement like −while (expression);the while loop runs no matter if the expression is true or not. However, if you put −if (expression);the statement runs no matter if the expression is true or not. This is because the syntax for if and while is −if () // or while () So the is only executed if the evaluates to true. In while, it will enter an infinite loop.So the question what it executes. If there are not braces {} then the next statement is terminated by; even if that statement is EMPTY. Note ... Read More

Unary operators in C++

Rama Giri
Updated on 11-Feb-2020 06:53:36

231 Views

Unary operator are operators that act upon a single operand to produce a new value. The unary operators are as follows −Indirection operator (*) - It operates on a pointer variable, and returns an l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer.Address-of operator (&) - The unary address-of operator (&) takes the address of its operand. The operand of the address-of operator can be either a function designator or an l-value that designates an object that is not a bit field and is not declared with the register storage-class specifier.Unary plus operator (+) - ... Read More

Identifiers in C++

Manikanth Mani
Updated on 30-Jul-2019 22:30:21

5K+ Views

The C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C++.Here are some examples of acceptable identifiers −mohdPiyushabcmove_namea_123myname50_tempja23b9retVal

Reserved keywords in C++?

Arjun Thakur
Updated on 19-Jun-2020 05:31:50

9K+ Views

A reserved word is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a reserved word may have no meaning.There are a total of 95 reserved words in C++. The reserved words of C++ may be conveniently placed into several groups. In the first group, we put those that were also present in the C programming language and have been carried over into C++. There are 32 of these. There are another 30 reserved words that were not in ... Read More

Trigraphs in C++

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

200 Views

The ISO-646 character set does not have all the characters of the C syntax, therefore there are some systems with keyboards and displays that cannot deal with some characters. These characters can be constructed using a sequence of 3 characters called trigraphs. In C, before any other processing takes place, each occurrence of one of the following sequences of three characters (“trigraph sequences”) is replaced by the single character.trigraphreplacementtrigraphreplacementtrigraphreplacement??=#??([??}??’ˆ??!|??-˜They are there mostly for historical reasons. Nowadays, most modern keyboards for most languages allow access to all those characters, but this used to be a problem once with some European keyboards. ... Read More

C++ Operators with Precedence and Associativity

Anvi Jain
Updated on 30-Jul-2019 22:30:21

3K+ Views

Operator precedence determines the grouping of terms in an expression. The associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.Here, operators with the highest precedence appear ... Read More

Advertisements