Tagged Questions
3
votes
2answers
82 views
In what situations can do-while be more efficient than while?
While vs. do-while
While and do-while are functionally equivalent when the blocks are empty, although while seems more natural:
do {} while (keepLooping());
while (keepLooping()) {}
One typical ...
3
votes
1answer
67 views
Java loop efficiency
I'm comparing the efficiency of nested for, while and do-while loops in Java, and I've come across some strange results that I need help understanding.
public class Loops {
public static void ...
0
votes
4answers
64 views
while loop is not working c++
Below is my code:
I am comparing user input data with existing 2D array. when user enters right user id the programe works fine, but when somebody enters wrong user id then programe crashes.
...
1
vote
4answers
61 views
Is there a way to avoid an empty else { } at the end?
I'm learning PHP and try to write a little dice loop. I wonder how to rewrite this in a more appropriate syntax:
<?php
$rollcount = 1;
do {
$v = rand(1, 6);
$w = rand(1, ...
0
votes
6answers
125 views
Why is ++i more efficient than i++? [duplicate]
According to the Google C++ Style Guide, "when the return value is ignored, the 'pre' form (++i) is never less efficient than the 'post' form (i++), and is often more efficient."
The guide goes on to ...
2
votes
1answer
72 views
Do while and while at the same time using ANTLR
Earlier I created this question asking how to create if/else statements using ANTLR 4. I got an excellent answer which also showed how to do while loops. I have implemented this in my language and I ...
0
votes
2answers
49 views
Switch doesn't accept input until the second attempt
I'm working on a project for a class and could use some help figuring out why my code is behaving the way it is. The assignment is to prompt a user to input two integers and select an arithmetic ...
0
votes
2answers
185 views
javascript lottery game using random numbers what is wrong with my logic
A simple straight forward game user enters 6 numbers and clicks button play, if all 6 numbers are matched they win lottery, I have set up the loop so that it keeps generating a random numbers until ...
0
votes
1answer
94 views
java while loop is not counting correctly
Imma java newbie so I try to learn things. I created a boolean method, it's a small craps game. I need this method is called from main method and keep playing while return value is true (game is won) ...
1
vote
4answers
147 views
Why do { } while(condition); needs semicolon at the end of it but while(condition) {} doesn't? [closed]
I always have problem with placing ; at the end of while or not placing it at the end of do while loops. So what's the reason? Why
int numItemsToProcess = 3;
while(numItemsToProcess > 0)
{
...
1
vote
2answers
107 views
Do-while statements beginner java max-min prompt
Hey guys I new to Java and on do-while statements, the question asks me to create a prompt that asks for a max and a min value, then it asks for another value between my max and min values. "The user ...
0
votes
5answers
134 views
Local variable name 'choice' cannot be declared because it would give different meaning to 'choice'
Sorry, I'm completely new to the programming world. I'm creating a console application that basically takes your input on whether you want to convert Celsius to Fahrenheit or vice-versa.
The ...
0
votes
2answers
36 views
while loop won't terminate even if i input a zero value for a and b
do
{
scanf("%d %d",&a,&b);
stcrnrArray[x] = a;
++a;
stcrnrArray[x] = b;
++b;
}
while((a != 0) && (b != 0));
while loop won't ...
-1
votes
2answers
93 views
User input only occurs once or never stops during loop
I am attempting a program that reads an unspecified number of integers, find sum, positive, negative and average. My issue is that either it will only run and allow one integer to be typed and then do ...
0
votes
0answers
30 views
Dividing the single row in multiple rows based on the calendar month of the year using while loop
I'm a SQL newbie. I have a single row with 3 months data(Feb-May) and cost. I want to divide that in 3 separate rows. Please see the attachement.
Original Data:
...